]> jfr.im git - irc/quakenet/qwebirc.git/blame - bin/compile.py
write QWEBIRC_DEBUG to debug pages
[irc/quakenet/qwebirc.git] / bin / compile.py
CommitLineData
15295989 1#!/usr/bin/env python
a409b906
CP
2import dependencies
3dependencies.vcheck()
4
982a98fa 5import pages, os, subprocess, pagegen, shutil, sys, time
15295989
CP
6
7COPYRIGHT = open("js/copyright.js", "rb").read()
8
a21fb915
CP
9class MinifyException(Exception):
10 pass
11
15295989 12def jarit(src):
a21fb915 13 try:
97fd8c3a 14 p = subprocess.Popen(["java", "-jar", "bin/yuicompressor-2.4.2.jar", src], stdout=subprocess.PIPE, shell=True)
a21fb915
CP
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
15295989 23
a21fb915 24JAVA_WARNING_SURPRESSED = False
4dd199c3 25def jmerge_files(prefix, suffix, output, files, *args, **kwargs):
15295989 26 global COPYRIGHT
60ce7bee 27 output = output + "." + suffix
15295989
CP
28 o = os.path.join(prefix, "compiled", output)
29 merge_files(o, files, *args)
a21fb915
CP
30
31 # cough hack
a21fb915
CP
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
b567a4f4
CP
39 try:
40 f = open(o, "rb")
41 compiled = f.read()
42 finally:
43 f.close()
a21fb915 44
0fab628b
CP
45 try:
46 os.unlink(o)
47 except:
48 time.sleep(1) # windows sucks
49 os.unlink(o)
b567a4f4 50
60ce7bee 51 f = open(os.path.join(prefix, "static", suffix, output), "wb")
15295989 52 f.write(COPYRIGHT)
97fd8c3a 53 f.write("QWEBIRC_DEBUG = false;")
4dd199c3
CP
54
55 if kwargs.get("file_prefix"):
56 f.write(kwargs.get("file_prefix"))
57
15295989
CP
58 f.write(compiled)
59 f.close()
60
61def merge_files(output, files, root_path=lambda x: x):
62 f = open(output, "wb")
63
64 for x in files:
65 f2 = open(root_path(x), "rb")
87fbc08a 66 f.write(f2.read() + "\n")
15295989
CP
67 f2.close()
68 f.close()
69
1e9c8714 70def main(outputdir=".", produce_debug=True):
7c4c581c
CP
71 ID = pagegen.gethgid()
72
60ce7bee 73 pagegen.main(outputdir, produce_debug=produce_debug)
15295989
CP
74
75 coutputdir = os.path.join(outputdir, "compiled")
76 try:
77 os.mkdir(coutputdir)
78 except:
79 pass
60ce7bee
CP
80
81 try:
82 os.mkdir(os.path.join(outputdir, "static", "css"))
83 except:
84 pass
15295989 85
60ce7bee 86 #jmerge_files(outputdir, "js", "qwebirc", pages.DEBUG_BASE, lambda x: os.path.join("js", x + ".js"))
15295989
CP
87
88 for uiname, value in pages.UIs.items():
60ce7bee 89 csssrc = pagegen.csslist(uiname, True)
7c4c581c 90 jmerge_files(outputdir, "css", uiname + "-" + ID, csssrc)
c3d261fc 91 shutil.copy2(os.path.join(outputdir, "static", "css", uiname + "-" + ID + ".css"), os.path.join(outputdir, "static", "css", uiname + ".css"))
4dd199c3
CP
92
93 mcssname = os.path.join("css", uiname + ".mcss")
94 if os.path.exists(mcssname):
95 mcssdest = os.path.join(outputdir, "static", "css", uiname + ".mcss")
96 shutil.copy2(mcssname, mcssdest)
97 shutil.copy2(mcssdest, os.path.join(outputdir, "static", "css", uiname + "-" + ID + ".mcss"))
98
60ce7bee
CP
99 #jmerge_files(outputdir, "js", uiname, value["uifiles"], lambda x: os.path.join("js", "ui", "frontends", x + ".js"))
100
101 alljs = []
102 for y in pages.JS_BASE:
103 alljs.append(os.path.join("static", "js", y + ".js"))
104 for y in value.get("buildextra", []):
105 alljs.append(os.path.join("static", "js", "%s.js" % y))
106 for y in pages.DEBUG_BASE:
107 alljs.append(os.path.join("js", y + ".js"))
108 for y in value["uifiles"]:
109 alljs.append(os.path.join("js", "ui", "frontends", y + ".js"))
4dd199c3 110 jmerge_files(outputdir, "js", uiname + "-" + ID, alljs, file_prefix="QWEBIRC_BUILD=\"" + ID + "\";\n")
97fd8c3a 111
15295989
CP
112 os.rmdir(coutputdir)
113
a409b906
CP
114 f = open(".compiled", "w")
115 f.close()
116
117def has_compiled():
118 try:
119 f = open(".compiled", "r")
120 f.close()
121 return True
122 except:
123 pass
124
125 try:
126 f = open(os.path.join("bin", ".compiled"), "r")
127 f.close()
128 return True
129 except:
130 pass
131
132 return False
133
134def vcheck():
135 if has_compiled():
136 return
137
138 print >>sys.stderr, "error: not yet compiled, run compile.py first."
139 sys.exit(1)
140
15295989
CP
141if __name__ == "__main__":
142 main()
143