]> jfr.im git - irc/quakenet/qwebirc.git/blob - bin/compile.py
Add (forced) dependency checking.
[irc/quakenet/qwebirc.git] / bin / compile.py
1 #!/usr/bin/env python
2 import dependencies
3 dependencies.vcheck()
4
5 import pages, os, subprocess, pagegen, shutil, sys
6
7 COPYRIGHT = open("js/copyright.js", "rb").read()
8
9 class MinifyException(Exception):
10 pass
11
12 def jarit(src):
13 try:
14 p = subprocess.Popen(["java", "-jar", "bin/yuicompressor-2.3.5.jar", src], stdout=subprocess.PIPE)
15 except Exception, e:
16 if hasattr(e, "errno") and e.errno == 2:
17 raise MinifyException, "unable to run java"
18 raise
19 data = p.communicate()[0]
20 if p.wait() != 0:
21 raise MinifyException, "an error occured"
22 return data
23
24 JAVA_WARNING_SURPRESSED = False
25 def jmerge_files(prefix, suffix, output, files, *args):
26 global COPYRIGHT
27 output = output + "." + suffix
28 o = os.path.join(prefix, "compiled", output)
29 merge_files(o, files, *args)
30
31 # cough hack
32 try:
33 compiled = jarit(o)
34 except MinifyException, e:
35 global JAVA_WARNING_SURPRESSED
36 if not JAVA_WARNING_SURPRESSED:
37 JAVA_WARNING_SURPRESSED = True
38 print >>sys.stderr, "warning: minify: %s (not minifying -- javascript will be HUGE)." % e
39 try:
40 f = open(o, "rb")
41 compiled = f.read()
42 finally:
43 f.close()
44
45 os.unlink(o)
46
47 f = open(os.path.join(prefix, "static", suffix, output), "wb")
48 f.write(COPYRIGHT)
49 f.write(compiled)
50 f.close()
51
52 def merge_files(output, files, root_path=lambda x: x):
53 f = open(output, "wb")
54
55 for x in files:
56 f2 = open(root_path(x), "rb")
57 f.write(f2.read())
58 f2.close()
59 f.close()
60
61 def main(outputdir=".", produce_debug=True):
62 ID = pagegen.gethgid()
63
64 pagegen.main(outputdir, produce_debug=produce_debug)
65
66 coutputdir = os.path.join(outputdir, "compiled")
67 try:
68 os.mkdir(coutputdir)
69 except:
70 pass
71
72 try:
73 os.mkdir(os.path.join(outputdir, "static", "css"))
74 except:
75 pass
76
77 #jmerge_files(outputdir, "js", "qwebirc", pages.DEBUG_BASE, lambda x: os.path.join("js", x + ".js"))
78
79 for uiname, value in pages.UIs.items():
80 csssrc = pagegen.csslist(uiname, True)
81 jmerge_files(outputdir, "css", uiname + "-" + ID, csssrc)
82 shutil.copy2(os.path.join(outputdir, "static", "css", uiname + "-" + ID + ".css"), os.path.join(outputdir, "static", "css", uiname + ".css"))
83 #jmerge_files(outputdir, "js", uiname, value["uifiles"], lambda x: os.path.join("js", "ui", "frontends", x + ".js"))
84
85 alljs = []
86 for y in pages.JS_BASE:
87 alljs.append(os.path.join("static", "js", y + ".js"))
88 for y in value.get("buildextra", []):
89 alljs.append(os.path.join("static", "js", "%s.js" % y))
90 for y in pages.DEBUG_BASE:
91 alljs.append(os.path.join("js", y + ".js"))
92 for y in value["uifiles"]:
93 alljs.append(os.path.join("js", "ui", "frontends", y + ".js"))
94 jmerge_files(outputdir, "js", uiname + "-" + ID, alljs)
95
96 os.rmdir(coutputdir)
97
98 f = open(".compiled", "w")
99 f.close()
100
101 def has_compiled():
102 try:
103 f = open(".compiled", "r")
104 f.close()
105 return True
106 except:
107 pass
108
109 try:
110 f = open(os.path.join("bin", ".compiled"), "r")
111 f.close()
112 return True
113 except:
114 pass
115
116 return False
117
118 def vcheck():
119 if has_compiled():
120 return
121
122 print >>sys.stderr, "error: not yet compiled, run compile.py first."
123 sys.exit(1)
124
125 if __name__ == "__main__":
126 main()
127