2019-03-22 10:40:58 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2019-08-03 00:28:36 +00:00
|
|
|
import sys, os, io, re
|
2016-04-06 21:07:06 +00:00
|
|
|
from twisted.internet import reactor, protocol, task, defer
|
2016-04-06 18:29:48 +00:00
|
|
|
from twisted.python.procutils import which
|
|
|
|
from twisted.python import usage
|
|
|
|
|
|
|
|
# run the command with python's deprecation warnings turned on, capturing
|
|
|
|
# stderr. When done, scan stderr for warnings, write them to a separate
|
|
|
|
# logfile (so the buildbot can see them), and return rc=1 if there were any.
|
|
|
|
|
|
|
|
class Options(usage.Options):
|
|
|
|
optParameters = [
|
2016-04-06 21:07:06 +00:00
|
|
|
["warnings", None, None, "file to write warnings into at end of test run"],
|
2019-08-03 00:28:36 +00:00
|
|
|
["package", None, None, "Python package to which to restrict warning collection"]
|
2016-04-06 18:29:48 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def parseArgs(self, command, *args):
|
|
|
|
self["command"] = command
|
|
|
|
self["args"] = list(args)
|
|
|
|
|
|
|
|
description = """Run as:
|
2019-08-03 00:28:36 +00:00
|
|
|
PYTHONWARNINGS=default::DeprecationWarning python run-deprecations.py [--warnings=STDERRFILE] [--package=PYTHONPACKAGE ] COMMAND ARGS..
|
2016-04-06 18:29:48 +00:00
|
|
|
"""
|
|
|
|
|
2016-04-06 21:07:06 +00:00
|
|
|
class RunPP(protocol.ProcessProtocol):
|
|
|
|
def outReceived(self, data):
|
|
|
|
self.stdout.write(data)
|
|
|
|
sys.stdout.write(data)
|
|
|
|
def errReceived(self, data):
|
|
|
|
self.stderr.write(data)
|
|
|
|
sys.stderr.write(data)
|
|
|
|
def processEnded(self, reason):
|
|
|
|
signal = reason.value.signal
|
|
|
|
rc = reason.value.exitCode
|
|
|
|
self.d.callback((signal, rc))
|
|
|
|
|
2019-08-03 00:28:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2016-04-06 21:07:06 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run_command(main):
|
|
|
|
config = Options()
|
|
|
|
config.parseOptions()
|
|
|
|
|
|
|
|
command = config["command"]
|
|
|
|
if "/" in command:
|
|
|
|
# don't search
|
|
|
|
exe = command
|
|
|
|
else:
|
|
|
|
executables = which(command)
|
|
|
|
if not executables:
|
|
|
|
raise ValueError("unable to find '%s' in PATH (%s)" %
|
|
|
|
(command, os.environ.get("PATH")))
|
|
|
|
exe = executables[0]
|
|
|
|
|
|
|
|
pw = os.environ.get("PYTHONWARNINGS")
|
|
|
|
DDW = "default::DeprecationWarning"
|
|
|
|
if pw != DDW:
|
2019-03-22 10:40:58 +00:00
|
|
|
print("note: $PYTHONWARNINGS is '%s', not the expected %s" % (pw, DDW))
|
2016-04-06 21:07:06 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
pp = RunPP()
|
|
|
|
pp.d = defer.Deferred()
|
|
|
|
pp.stdout = io.BytesIO()
|
|
|
|
pp.stderr = io.BytesIO()
|
|
|
|
reactor.spawnProcess(pp, exe, [exe] + config["args"], env=None)
|
|
|
|
(signal, rc) = yield pp.d
|
|
|
|
|
2019-08-03 00:28:36 +00:00
|
|
|
match = make_matcher(config)
|
|
|
|
|
2016-04-06 23:33:43 +00:00
|
|
|
# maintain ordering, but ignore duplicates (for some reason, either the
|
|
|
|
# 'warnings' module or twisted.python.deprecate isn't quashing them)
|
|
|
|
already = set()
|
2016-04-06 21:07:06 +00:00
|
|
|
warnings = []
|
2016-04-06 23:33:43 +00:00
|
|
|
def add(line):
|
|
|
|
if line in already:
|
|
|
|
return
|
|
|
|
already.add(line)
|
|
|
|
warnings.append(line)
|
2016-04-06 21:07:06 +00:00
|
|
|
|
|
|
|
pp.stdout.seek(0)
|
|
|
|
for line in pp.stdout.readlines():
|
2019-08-03 00:28:36 +00:00
|
|
|
if match(line):
|
2016-04-06 23:33:43 +00:00
|
|
|
add(line) # includes newline
|
2016-04-06 21:07:06 +00:00
|
|
|
|
|
|
|
pp.stderr.seek(0)
|
|
|
|
for line in pp.stderr.readlines():
|
2019-08-03 00:28:36 +00:00
|
|
|
if match(line):
|
2016-04-06 23:33:43 +00:00
|
|
|
add(line)
|
2016-04-06 21:07:06 +00:00
|
|
|
|
|
|
|
if warnings:
|
|
|
|
if config["warnings"]:
|
|
|
|
with open(config["warnings"], "wb") as f:
|
2019-03-22 10:40:58 +00:00
|
|
|
print("".join(warnings), file=f)
|
|
|
|
print("ERROR: %d deprecation warnings found" % len(warnings))
|
2016-04-06 21:07:06 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2019-03-22 10:40:58 +00:00
|
|
|
print("no deprecation warnings")
|
2016-04-06 21:07:06 +00:00
|
|
|
if signal:
|
|
|
|
sys.exit(signal)
|
|
|
|
sys.exit(rc)
|
|
|
|
|
|
|
|
|
|
|
|
task.react(run_command)
|