waf: add support for subprocess.run for python2

This commit is contained in:
bugobliterator 2021-09-30 16:36:19 +05:30 committed by Andrew Tridgell
parent 0b7a913b30
commit 6fb0571f5c
1 changed files with 42 additions and 0 deletions

42
wscript
View File

@ -17,6 +17,48 @@ import boards
from waflib import Build, ConfigSet, Configure, Context, Utils
from waflib.Configure import conf
# Ref: https://stackoverflow.com/questions/40590192/getting-an-error-attributeerror-module-object-has-no-attribute-run-while
try:
from subprocess import CompletedProcess
except ImportError:
# Python 2
class CompletedProcess:
def __init__(self, args, returncode, stdout=None, stderr=None):
self.args = args
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
def check_returncode(self):
if self.returncode != 0:
err = subprocess.CalledProcessError(self.returncode, self.args, output=self.stdout)
raise err
return self.returncode
def sp_run(*popenargs, **kwargs):
input = kwargs.pop("input", None)
check = kwargs.pop("handle", False)
if input is not None:
if 'stdin' in kwargs:
raise ValueError('stdin and input arguments may not both be used.')
kwargs['stdin'] = subprocess.PIPE
process = subprocess.Popen(*popenargs, **kwargs)
try:
outs, errs = process.communicate(input)
except:
process.kill()
process.wait()
raise
returncode = process.poll()
if check and returncode:
raise subprocess.CalledProcessError(returncode, popenargs, output=outs)
return CompletedProcess(popenargs, returncode, stdout=outs, stderr=errs)
subprocess.run = sp_run
# ^ This monkey patch allows it work on Python 2 or 3 the same way
# TODO: implement a command 'waf help' that shows the basic tasks a
# developer might want to do: e.g. how to configure a board, compile a
# vehicle, compile all the examples, add a new example. Should fit in