From 60ae86411fb4727dd29baacb60f1d7d78a1c0942 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 2 Oct 2016 13:21:50 +0000 Subject: Add `execute` function (gd_diff.py). * gd_diff.py (execute): New function. * tests/test_gd_diff.py (test_execute_success, test_execute_cmderror) (test_execute_raise_exception): Tests for `execute` function. --- gd_diff.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'gd_diff.py') diff --git a/gd_diff.py b/gd_diff.py index 97cf175..3c704e9 100644 --- a/gd_diff.py +++ b/gd_diff.py @@ -6,8 +6,12 @@ # gns-deb-diff is under the Public Domain. See # +import shlex import sys +from subprocess import run, PIPE + + # list of recognized fields. field_list = [ "Change-Type", @@ -28,6 +32,23 @@ def read_file(fpath): return f.read() +def execute(cmd, out=None, err=None): + """Run `cmd`. Returns an instance of `subprocess.CompletedProcess` + + `cmd` must be a string containing the command to run. + """ + cmd = shlex.split(cmd) + + try: + completed_process = run(cmd, stdout=out, stderr=err) + except (FileNotFoundError, OSError, ValueError) as e: + print("Error running '%s'\n Error Info:\n %r" % (cmd[0], e), + file=sys.stderr) + sys.exit(1) + + return completed_process + + def get_packages(pkgs_file): """Return an iterator contaning of package names from `pkgs_file`. -- cgit v1.2.3