|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Merge all the .xml rulesets into a single "default.rulesets" file -- this |
| 4 | +# prevents inodes from wasting disk space, but more importantly, works around |
| 5 | +# the fact that zip does not perform well on a pile of small files. |
| 6 | + |
| 7 | +# currently a very literal translation of merge-rulesets.sh, but about five |
| 8 | +# times faster |
| 9 | + |
| 10 | +import os |
| 11 | +from glob import glob |
| 12 | +from subprocess import call |
| 13 | +import sys |
| 14 | +import traceback |
| 15 | +import re |
| 16 | + |
| 17 | +os.chdir("src") |
| 18 | +rulesets_fn="chrome/content/rules/default.rulesets" |
| 19 | +print "Creating ruleset library..." |
| 20 | + |
| 21 | +# Under git bash, sed -i issues errors and sets the file "read only". Thanks. |
| 22 | +if os.path.isfile(rulesets_fn): |
| 23 | + os.system("chmod u+w " + rulesets_fn) |
| 24 | + |
| 25 | +library = open(rulesets_fn,"w") |
| 26 | + |
| 27 | +# XXX TODO replace all sed commands with native Python |
| 28 | +#strip_oneline_comment = re.compile(r"<!--.*?-->") |
| 29 | + |
| 30 | +commit_id = os.environ["GIT_COMMIT_ID"] |
| 31 | +library.write('<rulesetlibrary gitcommitid="%s">' % commit_id) |
| 32 | +# Include the filename.xml as the "f" attribute |
| 33 | +for rfile in sorted(glob("chrome/content/rules/*.xml")): |
| 34 | + ruleset = open(rfile).read() |
| 35 | + fn=os.path.basename(rfile) |
| 36 | + ruleset = ruleset.replace("<ruleset", '<ruleset f="%s"' % fn, 1) |
| 37 | + library.write(ruleset) |
| 38 | +library.write("</rulesetlibrary>\n") |
| 39 | +library.close() |
| 40 | + |
| 41 | +print "Removing whitespaces and comments..." |
| 42 | + |
| 43 | +def rulesize(): |
| 44 | + return len(open(rulesets_fn).read()) |
| 45 | + |
| 46 | +crush = rulesize() |
| 47 | +sedcmd = ["sed", "-i", "-e", ":a", "-re"] |
| 48 | +call(sedcmd + [r"s/<!--.*?-->//g;/<!--/N;//ba", rulesets_fn]) |
| 49 | +call( ["sed", "-i", r":a;N;$!ba;s/\n//g;s/>[ ]*</></g;s/[ ]*to=/ to=/g;s/[ ]*from=/ from=/g;s/ \/>/\/>/g", rulesets_fn]) |
| 50 | +print "Crushed", crush, "bytes of rulesets into", rulesize() |
| 51 | + |
| 52 | +try: |
| 53 | + if 0 == call(["xmllint", "--noout", rulesets_fn]): |
| 54 | + print rulesets_fn, "passed XML validity test." |
| 55 | + else: |
| 56 | + print "ERROR:", rulesets_fn, "failed XML validity test!" |
| 57 | + sys.exit(1) |
| 58 | +except OSError, e: |
| 59 | + if "No such file or directory" not in traceback.format_exc(): |
| 60 | + raise |
| 61 | + print "WARNING: xmllint not present; validation of", rulesets_fn, " skipped." |
| 62 | + |
| 63 | +# We make default.rulesets at build time, but it shouldn't have a variable |
| 64 | +# timestamp |
| 65 | +call(["touch", "-r", "chrome/content/rules", rulesets_fn]) |
| 66 | + |
0 commit comments