From 856b3782eabf257b786e90a47b874bfc24543843 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 2 Aug 2019 18:28:36 -0600 Subject: [PATCH] add a --package option to run-deprecations so we can restrict to our own errors --- misc/build_helpers/run-deprecations.py | 31 ++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/misc/build_helpers/run-deprecations.py b/misc/build_helpers/run-deprecations.py index 6c76bcd69..1c7b10c4b 100644 --- a/misc/build_helpers/run-deprecations.py +++ b/misc/build_helpers/run-deprecations.py @@ -1,6 +1,6 @@ from __future__ import print_function -import sys, os, io +import sys, os, io, re from twisted.internet import reactor, protocol, task, defer from twisted.python.procutils import which from twisted.python import usage @@ -12,6 +12,7 @@ from twisted.python import usage class Options(usage.Options): optParameters = [ ["warnings", None, None, "file to write warnings into at end of test run"], + ["package", None, None, "Python package to which to restrict warning collection"] ] def parseArgs(self, command, *args): @@ -19,7 +20,7 @@ class Options(usage.Options): self["args"] = list(args) description = """Run as: -PYTHONWARNINGS=default::DeprecationWarning python run-deprecations.py [--warnings=STDERRFILE] COMMAND ARGS.. +PYTHONWARNINGS=default::DeprecationWarning python run-deprecations.py [--warnings=STDERRFILE] [--package=PYTHONPACKAGE ] COMMAND ARGS.. """ class RunPP(protocol.ProcessProtocol): @@ -34,6 +35,26 @@ class RunPP(protocol.ProcessProtocol): rc = reason.value.exitCode self.d.callback((signal, rc)) + +def make_matcher(options): + # A deprecation warning line looks something like this: + # + # somepath/foo/bar/baz.py:43: DeprecationWarning: Foo is deprecated, try bar instead. + # + # Sadly there is no guarantee warnings begin at the beginning of a line + # since they are written to output without coordination with whatever + # other Python code is running in the process. + pattern = r".*\.py[oc]?:\d+:" # (Pending)?DeprecationWarning: .*" + if options["package"]: + pattern = r".*/{}/".format( + re.escape(options["package"]), + ) + pattern + expression = re.compile(pattern) + def match(line): + return expression.match(line) is not None + return match + + @defer.inlineCallbacks def run_command(main): config = Options() @@ -63,6 +84,8 @@ def run_command(main): reactor.spawnProcess(pp, exe, [exe] + config["args"], env=None) (signal, rc) = yield pp.d + match = make_matcher(config) + # maintain ordering, but ignore duplicates (for some reason, either the # 'warnings' module or twisted.python.deprecate isn't quashing them) already = set() @@ -75,12 +98,12 @@ def run_command(main): pp.stdout.seek(0) for line in pp.stdout.readlines(): - if "DeprecationWarning" in line: + if match(line): add(line) # includes newline pp.stderr.seek(0) for line in pp.stderr.readlines(): - if "DeprecationWarning" in line: + if match(line): add(line) if warnings: