From 4fa92ee3029fafcb15c381aae7f129a5f6145e9e Mon Sep 17 00:00:00 2001 From: Roman Mohr Date: Thu, 15 Nov 2018 17:52:19 +0100 Subject: [PATCH] Check pipe errors when extracting RPMs --- rpm/install_rpms.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/rpm/install_rpms.py b/rpm/install_rpms.py index 59dee63..9e003cf 100644 --- a/rpm/install_rpms.py +++ b/rpm/install_rpms.py @@ -5,7 +5,6 @@ import argparse from os import path - parser = argparse.ArgumentParser( description='Install RPMs and update the RPM database inside the docker image') @@ -18,6 +17,11 @@ parser.add_argument('--output', action='store', required=True, help=('target archive')) + +class RPMError(Exception): + pass + + def main(): args = parser.parse_args() dirpath = tempfile.mkdtemp() @@ -35,19 +39,28 @@ def main(): # Register the RPMs in the database for rpm in args.rpm: - subprocess.check_call(["rpm", "--nosignature", "--dbpath", rpmdb, "-i", "-v", "--ignoresize", "--nodeps", "--noscripts" ,"--notriggers" ,"--excludepath", "/", rpm]) + subprocess.check_call( + ["rpm", "--nosignature", "--dbpath", rpmdb, "-i", "-v", "--ignoresize", "--nodeps", "--noscripts", + "--notriggers", "--excludepath", "/", rpm]) # Extract the rpms into the shared folder for rpm in args.rpm: p1 = subprocess.Popen(["rpm2cpio", rpm], stdout=subprocess.PIPE) - p2 = subprocess.Popen(["cpio", "-i", "-d", "-m", "-v"], stdin=p1.stdout, stdout=subprocess.PIPE, cwd=dirpath) + p2 = subprocess.Popen(["cpio", "-i", "-d", "-m", "-v"], stdin=p1.stdout, stdout=subprocess.PIPE, + cwd=dirpath) p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits. p2.communicate() + if p1.returncode and p1.returncode != 0: + print(p1.returncode) + raise RPMError("rpm2cpio exited with a non-zero exit code: %d" % p1.returncode) + if p2.returncode and p2.returncode != 0: + raise RPMError("cpio exited with a non-zero exit code: %d" % p2.returncode) with tarfile.open(args.output, "a") as tar: tar.add(dirpath, arcname="/") finally: shutil.rmtree(dirpath) + if __name__ == '__main__': - main() \ No newline at end of file + main()