|
3 | 3 |
|
4 | 4 | import argparse |
5 | 5 | import glob |
6 | | -import hashlib |
7 | 6 | import os |
8 | 7 |
|
9 | 8 | from lxml import etree |
10 | 9 |
|
| 10 | +# commandline arguments parsing (nobody use it, though) |
11 | 11 | parser = argparse.ArgumentParser(description="Validate rulesets against relaxng schema.xml") |
12 | 12 | parser.add_argument("--source_dir", default="src/chrome/content/rules") |
13 | | -parser.add_argument("--https2https_whitelist", default="utils/ruleset-whitelist.csv") |
14 | 13 |
|
15 | 14 | args = parser.parse_args() |
16 | 15 |
|
| 16 | +# XML ruleset files |
17 | 17 | files = glob.glob(os.path.join(args.source_dir, "*.xml")) |
18 | 18 |
|
19 | | -relaxng_doc = etree.parse("test/validations/relaxng/schema.xml") |
| 19 | +# read the schema file |
| 20 | +relaxng_doc = etree.parse('test/validations/relaxng/schema.xml') |
20 | 21 | relaxng = etree.RelaxNG(relaxng_doc) |
21 | 22 |
|
22 | | -relaxng_doc_https2https = etree.parse("test/validations/relaxng/schema_https2https.xml") |
23 | | -relaxng_https2https = etree.RelaxNG(relaxng_doc_https2https) |
24 | | - |
25 | | -https2https_whitelist = {} |
26 | | - |
27 | | -with open(args.https2https_whitelist) as f: |
28 | | - f.readline() |
29 | | - for line in f: |
30 | | - fileHash, _, _, fileSkip, fileName = line.strip().split(",") |
31 | | - if fileSkip == "1": |
32 | | - https2https_whitelist[fileName] = fileHash |
33 | | - |
34 | 23 | exit_code = 0 |
35 | 24 |
|
36 | 25 | print("Validation of rulesets against relaxng schema.xml begins...") |
37 | 26 |
|
38 | 27 | for filename in sorted(files): |
39 | 28 | tree = etree.parse(filename) |
40 | 29 |
|
41 | | - basename = os.path.basename(filename) |
42 | | - |
43 | | - ruleset_relaxng = relaxng |
44 | | - |
45 | | - if basename in https2https_whitelist: |
46 | | - with open(filename, "rb") as file: |
47 | | - if hashlib.sha256(file.read()).hexdigest() == https2https_whitelist[basename]: |
48 | | - ruleset_relaxng = relaxng_https2https |
49 | | - |
50 | | - if not ruleset_relaxng.validate(tree): |
| 30 | + if not relaxng.validate(tree): |
51 | 31 | exit_code = 1 |
52 | | - e = ruleset_relaxng.error_log.last_error |
| 32 | + e = relaxng.error_log.last_error |
53 | 33 | print("{} {}:{}:{}: {}".format(e.level_name, e.filename, e.line, e.column, e.message)) |
54 | 34 |
|
55 | 35 | if exit_code == 0: |
56 | | - print("Validation of rulesets against relaxng schema.xml succeeded.") |
| 36 | + message = "Validation of rulesets against relaxng schema.xml succeeded." |
57 | 37 | else: |
58 | | - print("Validation of rulesets against relaxng schema.xml failed.\n\n" \ |
59 | | - "Two very common reasons for this are the following:\n" \ |
60 | | - " - missing caret (^) in \"from\" attribute: it should be \"^http:\" and not \"http:\"\n" \ |
61 | | - " - missing trailing slashes in \"from\" or \"to\" when specifying full hostnames: \n" \ |
62 | | - " it should be \"https://eff.org/\" and not \"https://eff.org\"\n") |
| 38 | + message = "\nTwo very common reasons for this are the following:\n" \ |
| 39 | + " - missing caret (^) in 'from' attribute: it should be \"^http:\" and not \"http:\"\n" \ |
| 40 | + " - missing trailing slashes in 'from' or 'to' when specifying full hostnames: \n" \ |
| 41 | + " it should be \"https://eff.org/\" and not \"https://eff.org\"\n\n" \ |
| 42 | + "Validation of rulesets against relaxng schema.xml failed." |
63 | 43 |
|
| 44 | +print(message) |
64 | 45 | exit(exit_code) |
0 commit comments