Skip to content

Commit

Permalink
Normalize distribution name with pkg_resources
Browse files Browse the repository at this point in the history
`pypa/installer` is used to parse Wheel metadata, but does not currently
provide a method for normalizing distribution names:

- pypa/installer#97

`pypa/pkg_resources` provides `Requirement.parse` which returns an instance
of `Requirement` where `.key` is the canonical distribution name per PEP 503.

The `Requirement` class can also parse `extras`, but it returns a normalized
form that I believe could break the installation of the extras.
  • Loading branch information
mattoberle committed Jun 7, 2022
1 parent 8f23ee9 commit 248e653
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
3 changes: 1 addition & 2 deletions python/pip_install/extract_wheels/lib/bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
annotation,
namespace_pkgs,
purelib,
requirements,
wheel,
)

Expand Down Expand Up @@ -384,7 +383,7 @@ def extract_wheel(
if not enable_implicit_namespace_pkgs:
setup_namespace_pkg_compatibility(directory)

extras_requested = extras.get(requirements.sanitise_requirement(whl.name), set())
extras_requested = extras[whl.name] if whl.name in extras else set()
whl_deps = sorted(whl.dependencies(extras_requested))

if incremental:
Expand Down
11 changes: 3 additions & 8 deletions python/pip_install/extract_wheels/lib/requirements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from typing import Dict, Optional, Set, Tuple

import pkg_resources


def parse_extras(requirements_path: str) -> Dict[str, Set[str]]:
"""Parse over the requirements.txt file to find extras requested.
Expand All @@ -23,13 +25,6 @@ def parse_extras(requirements_path: str) -> Dict[str, Set[str]]:
return extras_requested


def sanitise_requirement(requirement: str) -> str:
"""Given a requirement string, returns a normalized version of the requirement name.
https://peps.python.org/pep-0503/#normalized-names
"""
return re.sub(r"[-_.]+", "-", requirement).lower()


def _parse_requirement_for_extra(
requirement: str,
) -> Tuple[Optional[str], Optional[Set[str]]]:
Expand All @@ -45,7 +40,7 @@ def _parse_requirement_for_extra(
matches = extras_pattern.match(requirement)
if matches:
return (
sanitise_requirement(matches.group(1)),
pkg_resources.Requirement.parse(matches.group(1)).key,
{extra.strip() for extra in matches.group(2).split(",")},
)

Expand Down
3 changes: 2 additions & 1 deletion python/pip_install/extract_wheels/lib/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def path(self) -> str:
@property
def name(self) -> str:
# TODO Also available as installer.sources.WheelSource.distribution
return str(self.metadata['Name'])
name = str(self.metadata['Name'])
return pkg_resources.Requirement.parse(name).key

@property
def metadata(self) -> email.message.Message:
Expand Down

0 comments on commit 248e653

Please sign in to comment.