Skip to content

Commit

Permalink
Fix python_version metadata extraction for bdist_egg distributions
Browse files Browse the repository at this point in the history
Fixes #1190.
  • Loading branch information
dnicolodi committed Dec 4, 2024
1 parent a723876 commit b7d5b68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/1192.bug.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix python_version metadata extraction for bdist_egg distributions.
3 changes: 2 additions & 1 deletion tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,5 @@ def test_malformed_from_file(monkeypatch):

def test_package_from_egg():
filename = "tests/fixtures/twine-3.3.0-py3.9.egg"
package_file.PackageFile.from_filename(filename, comment=None)
p = package_file.PackageFile.from_filename(filename, comment=None)
assert p.python_version == "3.9"
20 changes: 18 additions & 2 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@
".zip": "sdist",
}

# Match the regualr expression used by ``pkg_resources``, extended to
# include the file extension and the end-of-string anchor.
egg_filename_re = re.compile(
r"""
(?P<name>[^-]+) (
-(?P<ver>[^-]+) (
-py(?P<pyver>[^-]+) (
-(?P<plat>.+)
)?
)?
)?\.egg$
""",
re.VERBOSE | re.IGNORECASE,
)

MetadataValue = Union[Optional[str], Sequence[str], Tuple[str, bytes]]

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -159,8 +174,9 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":

py_version: Optional[str]
if dtype == "bdist_egg":
(dist,) = importlib_metadata.Distribution.discover(path=[filename])
py_version = dist.metadata["Version"]
m = egg_filename_re.match(os.path.basename(filename))
assert m is not None, "for mypy"
py_version = m.group("pyver")
elif dtype == "bdist_wheel":
py_version = cast(wheel.Wheel, meta).py_version
elif dtype == "bdist_wininst":
Expand Down

0 comments on commit b7d5b68

Please sign in to comment.