]> jfr.im git - irc/quakenet/qwebirc.git/blob - bin/compile.py
Now we don't *need* java or hg, though both are still useful.
[irc/quakenet/qwebirc.git] / bin / compile.py
1 #!/usr/bin/env python
2 import pages, os, subprocess, pagegen, shutil, sys
3
4 COPYRIGHT = open("js/copyright.js", "rb").read()
5
6 class MinifyException(Exception):
7 pass
8
9 def jarit(src):
10 try:
11 p = subprocess.Popen(["java", "-jar", "bin/yuicompressor-2.3.5.jar", src], stdout=subprocess.PIPE)
12 except Exception, e:
13 if hasattr(e, "errno") and e.errno == 2:
14 raise MinifyException, "unable to run java"
15 raise
16 data = p.communicate()[0]
17 if p.wait() != 0:
18 raise MinifyException, "an error occured"
19 return data
20
21 JAVA_WARNING_SURPRESSED = False
22 def jmerge_files(prefix, suffix, output, files, *args):
23 global COPYRIGHT
24 output = output + "." + suffix
25 o = os.path.join(prefix, "compiled", output)
26 merge_files(o, files, *args)
27
28 # cough hack
29 compiled = open(o, "rb").read()
30 try:
31 compiled = jarit(o)
32 except MinifyException, e:
33 global JAVA_WARNING_SURPRESSED
34 if not JAVA_WARNING_SURPRESSED:
35 JAVA_WARNING_SURPRESSED = True
36 print >>sys.stderr, "warning: minify: %s (not minifying -- javascript will be HUGE)." % e
37
38 os.unlink(o)
39 f = open(os.path.join(prefix, "static", suffix, output), "wb")
40 f.write(COPYRIGHT)
41 f.write(compiled)
42 f.close()
43
44 def merge_files(output, files, root_path=lambda x: x):
45 f = open(output, "wb")
46
47 for x in files:
48 f2 = open(root_path(x), "rb")
49 f.write(f2.read())
50 f2.close()
51 f.close()
52
53 def main(outputdir=".", produce_debug=True):
54 ID = pagegen.gethgid()
55
56 pagegen.main(outputdir, produce_debug=produce_debug)
57
58 coutputdir = os.path.join(outputdir, "compiled")
59 try:
60 os.mkdir(coutputdir)
61 except:
62 pass
63
64 try:
65 os.mkdir(os.path.join(outputdir, "static", "css"))
66 except:
67 pass
68
69 #jmerge_files(outputdir, "js", "qwebirc", pages.DEBUG_BASE, lambda x: os.path.join("js", x + ".js"))
70
71 for uiname, value in pages.UIs.items():
72 csssrc = pagegen.csslist(uiname, True)
73 jmerge_files(outputdir, "css", uiname + "-" + ID, csssrc)
74 shutil.copy2(os.path.join(outputdir, "static", "css", uiname + "-" + ID + ".css"), os.path.join(outputdir, "static", "css", uiname + ".css"))
75 #jmerge_files(outputdir, "js", uiname, value["uifiles"], lambda x: os.path.join("js", "ui", "frontends", x + ".js"))
76
77 alljs = []
78 for y in pages.JS_BASE:
79 alljs.append(os.path.join("static", "js", y + ".js"))
80 for y in value.get("buildextra", []):
81 alljs.append(os.path.join("static", "js", "%s.js" % y))
82 for y in pages.DEBUG_BASE:
83 alljs.append(os.path.join("js", y + ".js"))
84 for y in value["uifiles"]:
85 alljs.append(os.path.join("js", "ui", "frontends", y + ".js"))
86 jmerge_files(outputdir, "js", uiname + "-" + ID, alljs)
87
88 os.rmdir(coutputdir)
89
90 if __name__ == "__main__":
91 main()
92