diff --git a/s3pypi/core.py b/s3pypi/core.py index 2ef1f95..59ab88e 100644 --- a/s3pypi/core.py +++ b/s3pypi/core.py @@ -1,4 +1,3 @@ -import email import logging import re from contextlib import suppress @@ -7,7 +6,6 @@ from operator import attrgetter from pathlib import Path from typing import List -from zipfile import ZipFile import boto3 @@ -19,8 +17,6 @@ log = logging.getLogger(__prog__) -PackageMetadata = email.message.Message - @dataclass class Config: @@ -76,19 +72,20 @@ def upload_packages( def parse_distribution(path: Path) -> Distribution: + dist = parse_distribution_name(path.name) + dist.local_path = path + return dist + + +def parse_distribution_name(filename: str) -> Distribution: extensions = (".whl", ".tar.gz", ".tar.bz2", ".tar.xz", ".zip") - ext = next((ext for ext in extensions if path.name.endswith(ext)), "") + ext = next((ext for ext in extensions if filename.endswith(ext)), "") if not ext: - raise S3PyPiError(f"Unknown file type: {path}") - - if ext == ".whl": - meta = extract_wheel_metadata(path) - name, version = meta["Name"], meta["Version"] - else: - name, version = path.name[: -len(ext)].rsplit("-", 1) + raise S3PyPiError(f"Unknown file type: {filename}") - return Distribution(name, version, path) + name, version = filename[: -len(ext)].split("-", 2)[:2] + return Distribution(name, version, local_path=Path()) def parse_distributions(paths: List[Path]) -> List[Distribution]: @@ -110,26 +107,16 @@ def parse_distributions(paths: List[Path]) -> List[Distribution]: return dists -def extract_wheel_metadata(path: Path) -> PackageMetadata: - with ZipFile(path, "r") as whl: - try: - text = next( - whl.open(fname).read().decode() - for fname in whl.namelist() - if fname.endswith("METADATA") - ) - except StopIteration: - raise S3PyPiError(f"No wheel metadata found in {path}") from None - - return email.message_from_string(text) - - def delete_package(cfg: Config, name: str, version: str) -> None: storage = S3Storage(cfg.s3) directory = normalize_package_name(name) with storage.locked_index(directory) as index: - filenames = [f for f in index.filenames if f.split("-", 2)[1] == version] + filenames = [ + filename + for filename in index.filenames + if parse_distribution_name(filename).version == version + ] if not filenames: raise S3PyPiError(f"Package not found: {name} {version}") diff --git a/tests/data/dists/hello_world-0.1.0.tar.gz b/tests/data/dists/hello_world-0.1.0.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index d4ed2e2..beac012 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -127,6 +127,7 @@ def assert_pkg_exists(pkg: str, filename: str): for deleted_key in [ "hello-world/", "hello-world/hello_world-0.1.0-py3-none-any.whl", + "hello-world/hello_world-0.1.0.tar.gz", ]: with pytest.raises(s3_bucket.meta.client.exceptions.NoSuchKey): s3_bucket.Object(deleted_key).get() diff --git a/tests/unit/test_core.py b/tests/unit/test_core.py index 9becca5..8586027 100644 --- a/tests/unit/test_core.py +++ b/tests/unit/test_core.py @@ -21,14 +21,14 @@ def test_normalize_package_name(name, normalized): "dist", [ core.Distribution( - name="hello-world", + name="hello_world", version="0.1.0", - local_path=Path("dist/hello-world-0.1.0.tar.gz"), + local_path=Path("dist/hello_world-0.1.0.tar.gz"), ), core.Distribution( - name="foo-bar", + name="foo_bar", version="1.2.3", - local_path=Path("dist/foo-bar-1.2.3.zip"), + local_path=Path("dist/foo_bar-1.2.3.zip"), ), ], )