This repository has been archived by the owner on Oct 24, 2020. It is now read-only.
forked from EFForg/https-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-json.py
executable file
·69 lines (53 loc) · 2.11 KB
/
make-json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python2.7
#
# Builds a JSON DB containing all the rulesets, indexed by target.
# The JSON DB is used by the Firefox addon.
#
import locale
import json
import os
import sys
import collections
from lxml import etree
from ruleset_filenames_validate import validate_filenames
# Explicitly set locale so sorting order for filenames is consistent.
# This is important for deterministic builds.
# https://trac.torproject.org/projects/tor/ticket/11630#comment:20
# It's also helpful to ensure consistency for the lowercase check below.
locale.setlocale(locale.LC_ALL, 'C')
json_path = os.path.join(os.path.dirname(__file__), '../pkg/rulesets.json')
json_output = {
"rulesetStrings": [],
"targets": collections.defaultdict(list)
}
parser = etree.XMLParser(remove_blank_text=True)
# Precompile xpath expressions that get run repeatedly.
xpath_host = etree.XPath("/ruleset/target/@host")
xpath_ruleset = etree.XPath("/ruleset")
for fi in validate_filenames():
try:
tree = etree.parse(fi, parser)
except Exception as oops:
print("%s failed XML validity: %s\n" % (fi, oops))
sys.exit(1)
# Remove comments to save space.
etree.strip_tags(tree, etree.Comment)
targets = xpath_host(tree)
if not targets:
print('File %s has no targets' % fi)
sys.exit(1)
# Strip out the target tags. These aren't necessary in the DB because
# targets are looked up in the target table, which has a foreign key
# pointing into the ruleset table.
etree.strip_tags(tree, 'target')
etree.strip_tags(tree, 'test')
# Store the filename in the `f' attribute so "view source XML" for rules in
# FF version can find it.
xpath_ruleset(tree)[0].attrib["f"] = os.path.basename(fi).decode(encoding="UTF-8")
for target in targets:
# id is the current length of the rules list - i.e. the offset at which
# this rule will be added in the list.
json_output["targets"][target].append(len(json_output["rulesetStrings"]))
json_output["rulesetStrings"].append(etree.tostring(tree))
with open(json_path, 'w') as f:
f.write(json.dumps(json_output))