Skip to content

Commit

Permalink
Support PEP-503 name normalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Jan 8, 2019
1 parent 812a34f commit b19957d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
12 changes: 6 additions & 6 deletions pex/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from pex.base import maybe_requirement
from pex.link import Link
from pex.pep425tags import get_supported
from pex.third_party.pkg_resources import EGG_NAME, parse_version, safe_name, safe_version
from pex.util import Memoizer
from pex.third_party.pkg_resources import EGG_NAME, parse_version, safe_version
from pex.util import Memoizer, normalized_name


class Package(Link):
Expand Down Expand Up @@ -80,8 +80,8 @@ def satisfies(self, requirement, allow_prereleases=None):
:returns: True if the package matches the requirement, otherwise False
"""
requirement = maybe_requirement(requirement)
link_name = safe_name(self.name).lower()
if link_name != requirement.key:
link_name = normalized_name(self.name)
if link_name != normalized_name(requirement.key):
return False

# NB: If we upgrade to setuptools>=34 the SpecifierSet used here (requirement.specifier) will
Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(self, url, **kw):

@property
def name(self):
return safe_name(self._name)
return normalized_name(self._name)

@property
def raw_version(self):
Expand Down Expand Up @@ -202,7 +202,7 @@ def __hash__(self):

@property
def name(self):
return safe_name(self._name)
return normalized_name(self._name)

@property
def raw_version(self):
Expand Down
3 changes: 2 additions & 1 deletion pex/resolvable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pex.package import Package
from pex.resolver_options import ResolverOptionsBuilder, ResolverOptionsInterface
from pex.third_party.pkg_resources import Requirement, safe_extra
from pex.util import normalized_name

# Extract extras as specified per "declaring extras":
# https://pythonhosted.org/setuptools/setuptools.html
Expand Down Expand Up @@ -228,7 +229,7 @@ def packages(self):

@property
def name(self):
return self.requirement.key
return normalized_name(self.requirement.key)

@property
def exact(self):
Expand Down
5 changes: 2 additions & 3 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
from pex.platforms import Platform
from pex.resolvable import ResolvableRequirement, resolvables_from_iterable
from pex.resolver_options import ResolverOptionsBuilder
from pex.third_party.pkg_resources import safe_name
from pex.tracer import TRACER
from pex.util import DistributionHelper
from pex.util import DistributionHelper, normalized_name


@contextmanager
Expand Down Expand Up @@ -78,7 +77,7 @@ def merge(self, other):
class _ResolvableSet(object):
@classmethod
def normalize(cls, name):
return safe_name(name).lower()
return normalized_name(name)

def __init__(self, tuples=None):
# A list of _ResolvedPackages
Expand Down
6 changes: 3 additions & 3 deletions pex/resolver_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from pex.iterator import Iterator
from pex.package import EggPackage, SourcePackage, WheelPackage
from pex.sorter import Sorter
from pex.third_party.pkg_resources import safe_name
from pex.translator import ChainedTranslator, EggTranslator, SourceTranslator, WheelTranslator
from pex.util import normalized_name


class ResolverOptionsInterface(object):
Expand Down Expand Up @@ -92,11 +92,11 @@ def allow_all_external(self):
return self

def allow_external(self, key):
self._allow_external.add(safe_name(key).lower())
self._allow_external.add(normalized_name(key))
return self

def allow_unverified(self, key):
self._allow_unverified.add(safe_name(key).lower())
self._allow_unverified.add(normalized_name(key))
return self

def use_wheel(self):
Expand Down
12 changes: 12 additions & 0 deletions pex/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import contextlib
import os
import re
import shutil
import tempfile
import uuid
Expand Down Expand Up @@ -264,3 +265,14 @@ def merge_split(*paths):
"""
filtered_paths = filter(None, paths)
return [p for p in ':'.join(filtered_paths).split(':') if p]


def normalized_name(name):
"""Return a PEP-503 normalized version of the given name.
See: https://www.python.org/dev/peps/pep-0503/#normalized-names
:param str name: The distribution name to normalize.
:rtype: str
"""
return re.sub(r"[-_.]+", "-", name).lower()
5 changes: 3 additions & 2 deletions tests/test_resolvable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from pex.resolver_options import ResolverOptionsBuilder
from pex.testing import make_source_dir
from pex.util import normalized_name

try:
from unittest import mock
Expand Down Expand Up @@ -89,11 +90,11 @@ def test_resolvable_directory():

with make_source_dir(name='my_project') as td:
rdir = ResolvableDirectory.from_string(td, builder, interpreter)
assert rdir.name == pkg_resources.safe_name('my_project')
assert rdir.name == normalized_name('my_project')
assert rdir.extras() == []

rdir = ResolvableDirectory.from_string(td + '[extra1,extra2]', builder, interpreter)
assert rdir.name == pkg_resources.safe_name('my_project')
assert rdir.name == normalized_name('my_project')
assert rdir.extras() == ['extra1', 'extra2']


Expand Down

0 comments on commit b19957d

Please sign in to comment.