Skip to content

Commit 4e57677

Browse files
committed
Merged in 'origin/master' with changes from schoen.
1 parent 13f5328 commit 4e57677

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

hooks/precommit

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ if [ "$CHANGED_RULESETS" ]; then
3939
continue
4040
fi
4141

42-
# Errors will go to stderr.
43-
./utils/trivial-validate.py $FILE > /dev/null
42+
./utils/trivial-validate.py --quiet $FILE
4443
RESULT=$?
4544

46-
if [ $RESULT -eq 1 ]; then
45+
if [ $RESULT -ne 0 ]; then
4746
echo "$(date -R): Failure encountered during ruleset validation."
4847
exit $RESULT
4948
fi

utils/trivial-validate.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
default="",
2323
help="Duplicate directory."
2424
)
25+
parser.add_argument('--quiet', action="store_true",
26+
default=false, help="Surpress debug output."
27+
)
2528
parser.add_argument('ruleset', metavar='XML directory', type=str, nargs="*",
2629
default="src/chrome/content/rules",
2730
help='Directory of XML files to validate.')
@@ -30,6 +33,13 @@
3033

3134
ignoredups = [re.compile(val) for val in args.ignoredups]
3235
dupdir = [val for val in args.dupdir]
36+
quiet = args.quiet
37+
38+
def warn(s):
39+
if not quiet: sys.stdout.write("warning: %s\n" % s)
40+
41+
def fail(s):
42+
sys.stdout.write("failure: %s\n" % s)
3343

3444
def test_not_anchored(tree):
3545
# Rules not anchored to the beginning of a line.
@@ -59,8 +69,8 @@ def test_missing_to(tree):
5969
"""Rule is missing a 'to' value."""
6070
for rule in tree.xpath("/ruleset/rule"):
6171
if not rule.get("to"):
62-
sys.stdout.write("warning: 'to' attribute missing in %s. " %fi)
63-
sys.stdout.write("Misplaced end or misnamed element?\n")
72+
warn("'to' attribute missing in %s. " %fi)
73+
warn("Misplaced end or misnamed element?")
6474
return False
6575
return True
6676

@@ -109,11 +119,9 @@ def test_unencrypted_to(tree):
109119
if to[:6] != "https:" and to[:5] != "http:":
110120
return False
111121
elif to[:5] == "http:" and downgrade:
112-
sys.stdout.write("warning: downgrade rule in %s redirects " % fi)
113-
sys.stdout.write("to http.\n")
122+
warn("downgrade rule in %s redirects to http." % fi)
114123
elif to[:5] == "http:":
115-
sys.stdout.write("error: rule in %s redirects to http and " % fi)
116-
sys.stdout.write("downgrade attribute not specified.\n")
124+
fail("non-downgrade rule in %s redirects to http." % fi)
117125
return False
118126
return True
119127

@@ -218,35 +226,35 @@ def nomes_all(where=sys.argv[1:]):
218226
tree = etree.parse(fi)
219227
if fi[-4:] != ".xml":
220228
if tree.xpath("/ruleset"):
221-
sys.stdout.write("warning: ruleset in file without .xml extension: %s\n" % fi)
229+
warn("ruleset in file without .xml extension: %s" % fi)
222230
else:
223231
continue
224232
seen_file = True
225233
except Exception as oops:
226234
if fi[-4:] != ".xml":
227235
continue
228236
failure = 1
229-
sys.stderr.write("%s failed XML validity: %s\n" % (fi, oops))
237+
fail("%s failed XML validity: %s\n" % (fi, oops))
230238
if failure or not tree.xpath("/ruleset"):
231239
continue
232240
if not test_ruleset_name(tree):
233241
failure = 1
234-
sys.stderr.write("failure: unnamed ruleset: %s\n" % fi)
242+
fail("unnamed ruleset: %s" % fi)
235243
continue
236244
ruleset_name = tree.xpath("/ruleset/@name")[0]
237245
if ruleset_name in all_names:
238246
failure = 1
239-
sys.stdout.write("failure: duplicate ruleset name %s\n" % ruleset_name)
247+
fail("duplicate ruleset name %s" % ruleset_name)
240248
all_names.add(ruleset_name)
241249
for test in tests:
242250
if not test(tree):
243251
failure = 1
244-
sys.stderr.write("failure: %s failed test: %s\n" % (fi, test.__doc__))
252+
fail("%s failed test: %s" % (fi, test.__doc__))
245253
for target in tree.xpath("/ruleset/target/@host"):
246254
if target in all_targets and not any(ign.search(target) for ign in ignoredups):
247255
# suppress warning about duplicate targets if an --ignoredups
248256
# pattern matches target
249-
sys.stdout.write("warning: %s has duplicate target: %s\n" % (fi, target))
257+
warn("%s has duplicate target: %s" % (fi, target))
250258
all_targets.add(target)
251259

252260
if not seen_file:

0 commit comments

Comments
 (0)