subprocess: cleanup subprocess calls
v2 cover subprocess: cleanup subprocess calls
This is the beginning of an effort to move toward a unified way of using the subprocess module. The end goal is somday create a fork of the subprocess.run command intro'd in python 3.5.
The biggest hurdle in these efforts is in testing some of the more obscure code paths that are touched when changing these subprocess calls. With more time, many of the Popen calls that still exist could be moved to either check_output or sub.run.
Stephano Cetola (2):
subprocess: remove strings and migrate to direct arrays
subprocess: remove Popen in favor of check_output
meta/classes/buildstats.bbclass | 6 ++++- meta/classes/spdx.bbclass | 11 ++++---- meta/lib/oe/distro_check.py | 2 +- meta/lib/oe/package.py | 13 ++++----- meta/lib/oe/package_manager.py | 59 ++++++++++++++++++++--------------------- 5 files changed, 47 insertions(+), 44 deletions(-)
Signature · 1 line
2.10.0
PATCH v2 1/2 subprocess: remove strings and migrate to direct arrays
When using subprocess call and check_output, it is better to use arrays rather than strings when possible to avoid whitespace and quoting problems.
[ YOCTO #9342 ]
--- meta/lib/oe/distro_check.py | 2 +- meta/lib/oe/package.py | 13 +++++----- meta/lib/oe/package_manager.py | 59 +++++++++++++++++++++--------------------- 3 files changed, 37 insertions(+), 37 deletions(-)
meta/lib/oe/distro_check.py +1 -1
@@ -359,7 +359,7 @@ def create_log_file(d, logname): slogfile = os.path.join(logpath, logname) if os.path.exists(slogfile): os.remove(slogfile) - subprocess.call("touch %s" % logfile, shell=True) + subprocess.call(["touch", logfile]) os.symlink(logfile, slogfile) d.setVar('LOG_FILE', logfile) return logfile
meta/lib/oe/package.py +7 -6
@@ -18,23 +18,24 @@ def runstrip(arg): newmode = origmode | stat.S_IWRITE | stat.S_IREAD os.chmod(file, newmode) - extraflags = "" + stripcmd = [strip] # kernel module if elftype & 16: - extraflags = "--strip-debug --remove-section=.comment --remove-section=.note --preserve-dates" + stripcmd.extend(["--strip-debug", "--remove-section=.comment", + "--remove-section=.note", "--preserve-dates"]) # .so and shared library elif ".so" in file and elftype & 8: - extraflags = "--remove-section=.comment --remove-section=.note --strip-unneeded" + stripcmd.extend(["--remove-section=.comment", "--remove-section=.note", "--strip-unneeded"]) # shared or executable: elif elftype & 8 or elftype & 4: - extraflags = "--remove-section=.comment --remove-section=.note" + stripcmd.extend(["--remove-section=.comment", "--remove-section=.note"]) - stripcmd = "'%s' %s '%s'" % (strip, extraflags, file) + stripcmd.append(file) bb.debug(1, "runstrip: %s" % stripcmd) try: - output = subprocess.check_output(stripcmd, stderr=subprocess.STDOUT, shell=True) + output = subprocess.check_output(stripcmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: bb.error("runstrip: '%s' strip command failed with %s (%s)" % (stripcmd, e.returncode, e.output))
meta/lib/oe/package_manager.py +29 -30
124 lines not on this page · raw message
Signature · 1 line
2.10.0
PATCH v2 2/2 subprocess: remove Popen in favor of check_output
In reply to Stephano CetolaThis begins moving away from the deprecated subprocess calls in an effort to eventually move to some more global abstraction using the run convenience method provided in python 3.5.
[ YOCTO #9342 ]
--- meta/classes/buildstats.bbclass | 6 +++++- meta/classes/spdx.bbclass | 11 +++++------ 2 files changed, 10 insertions(+), 7 deletions(-)
meta/classes/buildstats.bbclass +5 -1
@@ -163,7 +163,11 @@ python run_buildstats () { bs = os.path.join(bsdir, "build_stats") with open(bs, "a") as f: rootfs = d.getVar('IMAGE_ROOTFS', True) - rootfs_size = subprocess.Popen(["du", "-sh", rootfs], stdout=subprocess.PIPE).stdout.read() + try: + rootfs_size = subprocess.check_output(["du", "-sh", rootfs], + stderr=subprocess.STDOUT).decode('utf-8') + except subprocess.CalledProcessError as e: + bb.error("Failed to get rootfs size: %s" % e.output) f.write("Uncompressed Rootfs size: %s" % rootfs_size) elif isinstance(e, bb.build.TaskFailed):
meta/classes/spdx.bbclass +5 -6
@@ -219,14 +219,13 @@ def hash_string(data): def run_fossology(foss_command, full_spdx): import string, re import subprocess - - p = subprocess.Popen(foss_command.split(), - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - foss_output, foss_error = p.communicate() - if p.returncode != 0: + + try: + foss_output = subprocess.check_output(foss_command.split(), + stderr=subprocess.STDOUT).decode('utf-8') + except subprocess.CalledProcessError as e: return None - foss_output = unicode(foss_output, "utf-8") foss_output = string.replace(foss_output, '\r', '') # Package info
Signature · 1 line
2.10.0