diff --git a/install_files/ansible-base/roles/build-securedrop-app-code-deb-pkg/files/hash_built_wheels.py b/install_files/ansible-base/roles/build-securedrop-app-code-deb-pkg/files/hash_built_wheels.py index b9556f1e5ec..3fcb058ab28 100644 --- a/install_files/ansible-base/roles/build-securedrop-app-code-deb-pkg/files/hash_built_wheels.py +++ b/install_files/ansible-base/roles/build-securedrop-app-code-deb-pkg/files/hash_built_wheels.py @@ -3,8 +3,6 @@ import glob import hashlib import os -import re -import sys REQUIREMENTS_FILE = os.environ['SD_REQUIREMENTS'] WHEELHOUSE = os.environ['SD_WHEELHOUSE'] @@ -12,11 +10,12 @@ def main(): - f = open(LOCK_FILE, "r") - lockfile = f.readlines() - f.close() + + with open(LOCK_FILE, "r") as f: + lockfile = f.readlines() + # remove newlines - lockfile = list(map(lambda x: x.strip(), lockfile)) + lockfile = [x.strip() for x in lockfile] new_reqs = "" for line in lockfile: @@ -25,9 +24,8 @@ def main(): package_string = "{}-{}".format(package.replace("-", "_"), version) new_reqs += "{}=={}".format(package, version) new_reqs += " --hash=sha256:{}\n".format(get_hash_for_package(package_string)) - reqs_file = open(REQUIREMENTS_FILE, "w") - reqs_file.write(new_reqs) - reqs_file.close() + with open(REQUIREMENTS_FILE, "w") as reqs_file: + reqs_file.write(new_reqs) def get_hash_for_package(package): @@ -38,11 +36,10 @@ def get_hash_for_package(package): def get_wheel(name): result = glob.glob(name + "*.whl") - if(len(result) == 1): - return result[0] - else: - print("There are several wheels matching package name", file=sys.stderr) # noqa - sys.exit(1) + if len(result) != 1: + msg = "There are several wheels matching package {}".format(name) + raise Exception(msg) + return result[0] def sha256_file(path):