Skip to content

Commit 3d811ee

Browse files
author
Seth Schoen
committed
make trivial-validate support --quiet (no warnings, only failures)
1 parent 40e2421 commit 3d811ee

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

utils/trivial-validate.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
sys.stderr.write("** Please install libxml2 and lxml to permit validation!\n")
1111
sys.exit(0)
1212

13-
longargs, args = getopt.gnu_getopt(sys.argv[1:], "", ["ignoredups=", "dupdir="])
13+
longargs, args = getopt.gnu_getopt(sys.argv[1:], "", ["ignoredups=", "quiet", "dupdir="])
1414

1515
ignoredups = [re.compile(val) for opt, val in longargs if opt == "--ignoredups"]
1616
dupdir = [val for opt, val in longargs if opt == "--dupdir"]
17+
quiet = any(opt == "--quiet" for opt, val in longargs)
18+
19+
def warn(s):
20+
if not quiet: sys.stdout.write("warning: %s\n" % s)
21+
22+
def fail(s):
23+
sys.stdout.write("failure: %s\n" % s)
1724

1825
def test_not_anchored(tree):
1926
# Rules not anchored to the beginning of a line.
@@ -43,8 +50,8 @@ def test_missing_to(tree):
4350
"""Rule is missing a 'to' value."""
4451
for rule in tree.xpath("/ruleset/rule"):
4552
if not rule.get("to"):
46-
sys.stdout.write("warning: 'to' attribute missing in %s. " %fi)
47-
sys.stdout.write("Misplaced end or misnamed element?\n")
53+
warn("'to' attribute missing in %s. " %fi)
54+
warn("Misplaced end or misnamed element?")
4855
return False
4956
return True
5057

@@ -93,11 +100,9 @@ def test_unencrypted_to(tree):
93100
if to[:6] != "https:" and to[:5] != "http:":
94101
return False
95102
elif to[:5] == "http:" and downgrade:
96-
sys.stdout.write("warning: downgrade rule in %s redirects " % fi)
97-
sys.stdout.write("to http.\n")
103+
warn("downgrade rule in %s redirects to http." % fi)
98104
elif to[:5] == "http:":
99-
sys.stdout.write("error: rule in %s redirects to http and " % fi)
100-
sys.stdout.write("downgrade attribute not specified.\n")
105+
fail("non-downgrade rule in %s redirects to http." % fi)
101106
return False
102107
return True
103108

@@ -202,35 +207,35 @@ def nomes_all(where=sys.argv[1:]):
202207
tree = etree.parse(fi)
203208
if fi[-4:] != ".xml":
204209
if tree.xpath("/ruleset"):
205-
sys.stdout.write("warning: ruleset in file without .xml extension: %s\n" % fi)
210+
warn("ruleset in file without .xml extension: %s" % fi)
206211
else:
207212
continue
208213
seen_file = True
209214
except Exception as oops:
210215
if fi[-4:] != ".xml":
211216
continue
212217
failure = 1
213-
sys.stdout.write("%s failed XML validity: %s\n" % (fi, oops))
218+
fail("%s failed XML validity: %s\n" % (fi, oops))
214219
if failure or not tree.xpath("/ruleset"):
215220
continue
216221
if not test_ruleset_name(tree):
217222
failure = 1
218-
sys.stdout.write("failure: unnamed ruleset: %s\n" % fi)
223+
fail("unnamed ruleset: %s" % fi)
219224
continue
220225
ruleset_name = tree.xpath("/ruleset/@name")[0]
221226
if ruleset_name in all_names:
222227
failure = 1
223-
sys.stdout.write("failure: duplicate ruleset name %s\n" % ruleset_name)
228+
fail("duplicate ruleset name %s" % ruleset_name)
224229
all_names.add(ruleset_name)
225230
for test in tests:
226231
if not test(tree):
227232
failure = 1
228-
sys.stdout.write("failure: %s failed test: %s\n" % (fi, test.__doc__))
233+
fail("%s failed test: %s" % (fi, test.__doc__))
229234
for target in tree.xpath("/ruleset/target/@host"):
230235
if target in all_targets and not any(ign.search(target) for ign in ignoredups):
231236
# suppress warning about duplicate targets if an --ignoredups
232237
# pattern matches target
233-
sys.stdout.write("warning: %s has duplicate target: %s\n" % (fi, target))
238+
warn("%s has duplicate target: %s" % (fi, target))
234239
all_targets.add(target)
235240

236241
if not seen_file:

0 commit comments

Comments
 (0)