Skip to content

Commit

Permalink
Add support to .utils.parse_sdist_filename() for sdists with .zip ext…
Browse files Browse the repository at this point in the history
…ension (#428)
  • Loading branch information
lkollar committed May 6, 2021
1 parent c32c969 commit 8c78c35
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
~~~~~~~~~~~~

* `packaging` is now only compatible with Python 3.6 and above.
* Add support for zip files in ``parse_sdist_filename`` (:issue:`429`)

20.9 - 2021-01-29
~~~~~~~~~~~~~~~~~
Expand Down
11 changes: 8 additions & 3 deletions packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,19 @@ def parse_wheel_filename(


def parse_sdist_filename(filename: str) -> Tuple[NormalizedName, Version]:
if not filename.endswith(".tar.gz"):
if filename.endswith(".tar.gz"):
file_stem = filename[: -len(".tar.gz")]
elif filename.endswith(".zip"):
file_stem = filename[: -len(".zip")]
else:
raise InvalidSdistFilename(
f"Invalid sdist filename (extension must be '.tar.gz'): {filename}"
f"Invalid sdist filename (extension must be '.tar.gz' or '.zip'):"
f" {filename}"
)

# We are requiring a PEP 440 version, which cannot contain dashes,
# so we split on the last dash.
name_part, sep, version_part = filename[:-7].rpartition("-")
name_part, sep, version_part = file_stem.rpartition("-")
if not sep:
raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")

Expand Down
5 changes: 3 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ def test_parse_wheel_invalid_filename(filename):


@pytest.mark.parametrize(
("filename", "name", "version"), [("foo-1.0.tar.gz", "foo", Version("1.0"))]
("filename", "name", "version"),
[("foo-1.0.tar.gz", "foo", Version("1.0")), ("foo-1.0.zip", "foo", Version("1.0"))],
)
def test_parse_sdist_filename(filename, name, version):
assert parse_sdist_filename(filename) == (name, version)


@pytest.mark.parametrize(("filename"), [("foo-1.0.zip"), ("foo1.0.tar.gz")])
@pytest.mark.parametrize(("filename"), [("foo-1.0.xz"), ("foo1.0.tar.gz")])
def test_parse_sdist_invalid_filename(filename):
with pytest.raises(InvalidSdistFilename):
parse_sdist_filename(filename)

0 comments on commit 8c78c35

Please sign in to comment.