Constant used
[opentx.git] / tools / build-opentx.py
blob98fc0e02afbcc41d2edd98b90b1a23c4c751b22a
1 #! /usr/bin/python
2 import cgitb
3 #cgitb.enable()
6 from pprint import pprint
7 from time import gmtime, strftime
8 from os.path import expanduser
9 import os
10 import os.path
11 import cgi
12 import sys
13 import re
14 import fcntl
15 import subprocess
17 gitdir="~/cgibuild/opentx"
18 pidfile="~/cgibuild/opentxbuild.pid"
19 outdir="~/Sites/builds/"
20 builddir="~/cgibuild/"
22 def main():
23 print ("Content-Type: text/html\n")
25 print ("""<!DOCTYPE html>
26 <html>
27 <body>
28 <h1>OpenTX 2.3 Companion build...</h1>
29 """)
31 form = cgi.FieldStorage()
32 suffix = None
33 buildscript = "build-companion-release.sh"
34 if 'suffix' in form:
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")
48 getlock_or_exit()
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), \
56 "-j4",
57 expanduser(gitdir), \
58 expanduser(outdir)]
59 if suffix:
60 buildcmd.append(suffix)
63 run_cmd(buildcmd, builddir, logfile)
64 status("Finished")
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)
76 print("<pre>\n")
77 sys.stdout.flush()
79 for line in p.stdout:
80 sys.stdout.write(line)
81 logfile.write(line)
82 if '\n' in line:
83 sys.stdout.flush()
85 print("</pre>\n")
86 p.wait()
87 if(p.returncode != 0):
88 status( "failed with status %d" % p.returncode, True)
91 def getlock_or_exit():
92 global fp
93 pid_file = expanduser(pidfile)
94 fp = open(pid_file, 'w')
95 try:
96 fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
97 fp.write("%s" % (os.getpid()))
98 except IOError as e:
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))
105 if exit:
106 sys.exit(0)
108 if __name__=="__main__":
109 main()