From 7f410aaff211a1d84f63e7ddf63b0a8badebea0a Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 25 Feb 2015 17:46:08 +0100 Subject: [PATCH] package-installs: work with Python < 2.7 subprocess.check_output() has been introduced in Python 2.7, so the script will fail when trying to install stuff in guests with Python 2.6 and older (like RHEL 6 / CentOS 6, for example). Thus gracefully fallback to subprocess.Popen() when subprocess.check_output() is not available. Change-Id: I335148397932177810f095a942b993b249991107 Closes-Bug: #1415240 --- .../package-installs/bin/package-installs-v2 | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/elements/package-installs/bin/package-installs-v2 b/elements/package-installs/bin/package-installs-v2 index 5bb51f252..d8567753f 100755 --- a/elements/package-installs/bin/package-installs-v2 +++ b/elements/package-installs/bin/package-installs-v2 @@ -20,6 +20,20 @@ import subprocess import sys +def process_output(cmdline): + # Try to execute subprocess.check_output(), which is available + # in Python 2.7+, gracefully falling back to subprocess.Popen + # in older Python versions. + try: + return subprocess.check_output(cmdline) + except AttributeError: + proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE) + out = proc.communicate()[0] + if proc.returncode: + raise subprocess.CalledProcessError(proc.returncode, cmdline, out) + return out + + def main(): parser = argparse.ArgumentParser( description="Install or uninstall packages for a specific phase based" @@ -54,7 +68,7 @@ def main(): pkg_map_args.append(pkg) try: - map_output = subprocess.check_output( + map_output = process_output( pkg_map_args) except subprocess.CalledProcessError as e: if e.returncode == 1: @@ -78,7 +92,7 @@ def main(): print(" ".join(install_args)) else: try: - subprocess.check_output(install_args) + process_output(install_args) except subprocess.CalledProcessError as e: print("install failed with error %s" % e.output) sys.exit(1)