From b5ae0bdca36cd24637cba756663a78a38bc97cfd Mon Sep 17 00:00:00 2001 From: Dan Streetman Date: Mon, 11 Feb 2019 14:30:01 -0500 Subject: [PATCH] simplify subprocess usage --- submittodebian | 19 ++++++++----------- ubuntu-iso | 10 +++++----- ubuntutools/builder.py | 8 +++----- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/submittodebian b/submittodebian index 86fa45f..2599ff4 100755 --- a/submittodebian +++ b/submittodebian @@ -27,7 +27,9 @@ import os import re import shutil import sys -from subprocess import call, check_call, Popen, PIPE + +from subprocess import call, check_call, run, Popen, PIPE, DEVNULL + from tempfile import mkdtemp from debian.changelog import Changelog @@ -85,9 +87,8 @@ def gen_debdiff(tmpdir, changelog): debdiff = os.path.join(tmpdir, '%s_%s.debdiff' % (pkg, newver)) - devnull = open('/dev/null', 'w') diff_cmd = ['bzr', 'diff', '-r', 'tag:' + str(oldver)] - if call(diff_cmd, stdout=devnull, stderr=devnull) == 1: + if call(diff_cmd, stdout=DEVNULL, stderr=DEVNULL) == 1: print("Extracting bzr diff between %s and %s" % (oldver, newver)) else: if oldver.epoch is not None: @@ -104,14 +105,10 @@ def gen_debdiff(tmpdir, changelog): print("Generating debdiff between %s and %s" % (oldver, newver)) diff_cmd = ['debdiff', olddsc, newdsc] - diff = Popen(diff_cmd, stdout=PIPE) - debdiff_f = open(debdiff, 'w') - filterdiff = Popen(['filterdiff', '-x', '*changelog*'], - stdin=diff.stdout, stdout=debdiff_f) - diff.stdout.close() - filterdiff.wait() - debdiff_f.close() - devnull.close() + with Popen(diff_cmd, stdout=PIPE, encoding='utf-8') as diff: + with open(debdiff, 'w', encoding='utf-8') as debdiff_f: + run(['filterdiff', '-x', '*changelog*'], + stdin=diff.stdout, stdout=debdiff_f, encoding='utf-8') return debdiff diff --git a/ubuntu-iso b/ubuntu-iso index dc52bac..fd56a66 100755 --- a/ubuntu-iso +++ b/ubuntu-iso @@ -27,15 +27,15 @@ import sys def extract(iso, path): command = ['isoinfo', '-R', '-i', iso, '-x', path] - pipe = subprocess.Popen(command, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, encoding='utf-8') - stdout, stderr = pipe.communicate() + pipe = subprocess.run(command, encoding='utf-8', + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) if pipe.returncode != 0: - sys.stderr.write(stderr) + sys.stderr.write(pipe.stderr) sys.exit(pipe.returncode) - return stdout + return pipe.stdout def main(): diff --git a/ubuntutools/builder.py b/ubuntutools/builder.py index 4c201d9..8ab478f 100644 --- a/ubuntutools/builder.py +++ b/ubuntutools/builder.py @@ -34,8 +34,7 @@ class Builder(object): def __init__(self, name): self.name = name cmd = ["dpkg-architecture", "-qDEB_BUILD_ARCH_CPU"] - self.architecture = subprocess.check_output( - cmd, encoding='utf-8').strip() + self.architecture = subprocess.check_output(cmd, encoding='utf-8').strip() def _build_failure(self, returncode, dsc_file): if returncode != 0: @@ -124,9 +123,8 @@ class Sbuild(Builder): def update(self, dist): cmd = ["schroot", "--list"] Logger.command(cmd) - process = subprocess.Popen( - cmd, stdout=subprocess.PIPE, encoding='utf-8') - chroots, _ = process.communicate()[0].strip().split() + process = subprocess.run(cmd, stdout=subprocess.PIPE, encoding='utf-8') + chroots, _ = process.stdout.strip().split() if process.returncode != 0: return process.returncode