]> jfr.im git - irc/quakenet/qwebirc.git/blob - bin/compile.py
Bump mootools to 1.2.5.
[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, time
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.4.2.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, **kwargs):
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 try:
46 os.unlink(o)
47 except:
48 time.sleep(1) # windows sucks
49 os.unlink(o)
50
51 f = open(os.path.join(prefix, "static", suffix, output), "wb")
52 f.write(COPYRIGHT)
53
54 if kwargs.get("file_prefix"):
55 f.write(kwargs.get("file_prefix"))
56
57 f.write(compiled)
58 f.close()
59
60 def merge_files(output, files, root_path=lambda x: x):
61 f = open(output, "wb")
62
63 for x in files:
64 f2 = open(root_path(x), "rb")
65 f.write(f2.read() + "\n")
66 f2.close()
67 f.close()
68
69 def main(outputdir=".", produce_debug=True):
70 ID = pagegen.gethgid()
71
72 pagegen.main(outputdir, produce_debug=produce_debug)
73
74 coutputdir = os.path.join(outputdir, "compiled")
75 try:
76 os.mkdir(coutputdir)
77 except:
78 pass
79
80 try:
81 os.mkdir(os.path.join(outputdir, "static", "css"))
82 except:
83 pass
84
85 #jmerge_files(outputdir, "js", "qwebirc", pages.DEBUG_BASE, lambda x: os.path.join("js", x + ".js"))
86
87 for uiname, value in pages.UIs.items():
88 csssrc = pagegen.csslist(uiname, True)
89 jmerge_files(outputdir, "css", uiname + "-" + ID, csssrc)
90 shutil.copy2(os.path.join(outputdir, "static", "css", uiname + "-" + ID + ".css"), os.path.join(outputdir, "static", "css", uiname + ".css"))
91
92 mcssname = os.path.join("css", uiname + ".mcss")
93 if os.path.exists(mcssname):
94 mcssdest = os.path.join(outputdir, "static", "css", uiname + ".mcss")
95 shutil.copy2(mcssname, mcssdest)
96 shutil.copy2(mcssdest, os.path.join(outputdir, "static", "css", uiname + "-" + ID + ".mcss"))
97
98 #jmerge_files(outputdir, "js", uiname, value["uifiles"], lambda x: os.path.join("js", "ui", "frontends", x + ".js"))
99
100 alljs = []
101 for y in pages.JS_BASE:
102 alljs.append(os.path.join("static", "js", y + ".js"))
103 for y in value.get("buildextra", []):
104 alljs.append(os.path.join("static", "js", "%s.js" % y))
105 for y in pages.DEBUG_BASE:
106 alljs.append(os.path.join("js", y + ".js"))
107 for y in value["uifiles"]:
108 alljs.append(os.path.join("js", "ui", "frontends", y + ".js"))
109 jmerge_files(outputdir, "js", uiname + "-" + ID, alljs, file_prefix="QWEBIRC_BUILD=\"" + ID + "\";\n")
110
111 os.rmdir(coutputdir)
112
113 f = open(".compiled", "w")
114 f.close()
115
116 def has_compiled():
117 try:
118 f = open(".compiled", "r")
119 f.close()
120 return True
121 except:
122 pass
123
124 try:
125 f = open(os.path.join("bin", ".compiled"), "r")
126 f.close()
127 return True
128 except:
129 pass
130
131 return False
132
133 def vcheck():
134 if has_compiled():
135 return
136
137 print >>sys.stderr, "error: not yet compiled, run compile.py first."
138 sys.exit(1)
139
140 if __name__ == "__main__":
141 main()
142