|
1 | 1 | #!/usr/bin/env python2.7 |
2 | 2 | # |
3 | | -# Builds an sqlite DB and a JSON DB containing all the rulesets, indexed by target. |
4 | | -# The sqlite DB is used by trivial-validate.py. The JSON DB is used by the |
5 | | -# Firefox addon. |
| 3 | +# Builds a JSON DB containing all the rulesets, indexed by target. |
| 4 | +# The JSON DB is used by the Firefox addon. |
6 | 5 | # |
7 | 6 |
|
8 | 7 | import glob |
|
24 | 23 | locale.setlocale(locale.LC_ALL, 'C') |
25 | 24 |
|
26 | 25 | json_path = os.path.join(os.path.dirname(__file__), '../pkg/rulesets.json') |
27 | | -# Removing the file before we create it avoids some non-determinism. |
28 | | -db_path = os.path.join(os.path.dirname(__file__), '../pkg/rulesets.unvalidated.sqlite') |
29 | | -if os.path.isfile(db_path): |
30 | | - os.remove(db_path) |
31 | | -conn = sqlite3.connect(db_path) |
32 | | -c = conn.cursor() |
33 | | -c.execute('''DROP TABLE IF EXISTS rulesets''') |
34 | | -c.execute('''CREATE TABLE rulesets |
35 | | - (id INTEGER PRIMARY KEY, |
36 | | - contents TEXT)''') |
37 | | -c.execute('''DROP TABLE IF EXISTS targets''') |
38 | | -c.execute('''CREATE TABLE targets |
39 | | - (host TEXT, |
40 | | - ruleset_id INTEGER)''') |
41 | 26 |
|
42 | 27 | json_output = { |
43 | 28 | "rulesetStrings": [], |
|
96 | 81 | # FF version can find it. |
97 | 82 | xpath_ruleset(tree)[0].attrib["f"] = os.path.basename(fi).decode(encoding="UTF-8") |
98 | 83 |
|
99 | | - c.execute('''INSERT INTO rulesets (contents) VALUES(?)''', (etree.tostring(tree),)) |
100 | | - ruleset_id = c.lastrowid |
101 | 84 | for target in targets: |
102 | | - c.execute('''INSERT INTO targets (host, ruleset_id) VALUES(?, ?)''', (target, ruleset_id)) |
103 | 85 | # id is the current length of the rules list - i.e. the offset at which |
104 | 86 | # this rule will be added in the list. |
105 | 87 | json_output["targets"][target].append(len(json_output["rulesetStrings"])) |
106 | 88 | json_output["rulesetStrings"].append(etree.tostring(tree)) |
107 | 89 |
|
108 | | -conn.commit() |
109 | | -conn.execute("VACUUM") |
110 | | -conn.close() |
111 | | - |
112 | 90 | with open(json_path, 'w') as f: |
113 | 91 | f.write(json.dumps(json_output)) |
0 commit comments