From f84c28b5e5357e9fdb9a739fd3577542603b5ac2 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Fri, 17 Sep 2021 14:28:03 +0300 Subject: [PATCH] Move exraction code into extract_wheel_metadata() This will be useful for backfill scripts. --- warehouse/forklift/legacy.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/warehouse/forklift/legacy.py b/warehouse/forklift/legacy.py index e5e7fba6437f..45aafb3f3bb4 100644 --- a/warehouse/forklift/legacy.py +++ b/warehouse/forklift/legacy.py @@ -762,6 +762,21 @@ def _is_duplicate_file(db_session, filename, hashes): return None +def extract_wheel_metadata(path): + """ + Extract METADATA file and return it as a content. The name of the + .whl file is used to find the corresponding .dist-info dir. + + See https://www.python.org/dev/peps/pep-0658/#specification + """ + global _wheel_file_re + filename = os.path.basepath(path) + namever = _wheel_file_re.match(filename).group("namever") + metafile = namever + ".dist-info/METADATA" + with zipfile.ZipFile(path) as zfp: + return zfp.read(metafile) + + @view_config( route_name="forklift.legacy.file_upload", uses_session=True, @@ -1331,12 +1346,9 @@ def file_upload(request): "Binary wheel '{filename}' has an unsupported " "platform tag '{plat}'.".format(filename=filename, plat=plat), ) - # Extract .metadata file - # https://www.python.org/dev/peps/pep-0658/#specification - with zipfile.ZipFile(temporary_filename) as zfp: - metafile = wheel_info.group("namever") + ".dist-info/METADATA" - with open(temporary_filename + ".metadata", "wb") as fp: - fp.write(zfp.read(metafile)) + wheel_metadata = extract_wheel_metadata(temporary_filename) + with open(temporary_filename + ".metadata", "wb") as fp: + fp.write(wheel_metadata) else: has_wheel_metadata = False