From 6fb0571f5cf6390eb35bfed163e499d1161222b6 Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Thu, 30 Sep 2021 16:36:19 +0530 Subject: [PATCH] waf: add support for subprocess.run for python2 --- wscript | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/wscript b/wscript index e6b7e5e2cc..fb06031cc0 100644 --- a/wscript +++ b/wscript @@ -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