6 from pprint
import pprint
7 from time
import gmtime
, strftime
8 from os
.path
import expanduser
17 gitdir
="~/cgibuild/opentx"
18 pidfile
="~/cgibuild/opentxbuild.pid"
19 outdir
="~/Sites/builds/"
20 builddir
="~/cgibuild/"
23 print ("Content-Type: text/html\n")
25 print ("""<!DOCTYPE html>
28 <h1>OpenTX 2.3 Companion build...</h1>
31 form
= cgi
.FieldStorage()
33 buildscript
= "build-companion-release.sh"
35 suffix
= form
.getfirst("suffix")
36 if not re
.match("^[a-zA-Z0-9]*$", suffix
):
37 status("Invalid suffix specified", True)
38 if suffix
and not suffix
.startswith("rc"):
39 buildscript
= "build-companion-nightly.sh"
41 if "branch" not in form
or not re
.match("^[a-z_\\-.A-Z0-9/]*$",form
.getfirst("branch")):
42 status ("No branch specified or invalid branch\n", True)
44 branch
= form
.getfirst("branch")
46 status ("Branch %s - Suffix %s" %(branch
, suffix
))
47 status ("Trying to get lock")
50 logfile
= open(os
.path
.join(expanduser(outdir
),strftime("build-%Y-%m-%d %H:%M:%S.log", gmtime())),"w")
51 run_cmd(["git", "fetch"], gitdir
, logfile
)
52 run_cmd(["git", "checkout", "origin/%s" % branch
], gitdir
, logfile
)
53 run_cmd(["git", "reset", "--hard"], gitdir
, logfile
)
55 buildcmd
= [os
.path
.join(expanduser(gitdir
), "tools", buildscript
), \
60 buildcmd
.append(suffix
)
63 run_cmd(buildcmd
, builddir
, logfile
)
66 def run_cmd(cmd
, wd
, logfile
):
67 env
= os
.environ
.copy()
68 env
['PATH'] = env
['PATH'] + ":/usr/local/bin/"
69 env
['PYTHONPATH']="/usr/local/lib/python2.7/site-packages"
70 env
['HOME']=expanduser("~")
73 status("Running '[%s]'" % ", ".join(cmd
))
74 p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
, cwd
=expanduser(wd
),env
=env
, bufsize
=00)
80 sys
.stdout
.write(line
)
87 if(p
.returncode
!= 0):
88 status( "failed with status %d" % p
.returncode
, True)
91 def getlock_or_exit():
93 pid_file
= expanduser(pidfile
)
94 fp
= open(pid_file
, 'w')
96 fcntl
.lockf(fp
, fcntl
.LOCK_EX | fcntl
.LOCK_NB
)
97 fp
.write("%s" % (os
.getpid()))
99 # another instance is running
101 status("could get lock, another instance running? %s " % str(e
), True)
103 def status(msg
, exit
=False):
104 print ("<p><b>%s - %s</b></p>\n" % (strftime("%Y-%m-%d %H:%M:%S", gmtime()),msg
))
108 if __name__
=="__main__":