Skip to content

Commit

Permalink
Normalize dirnames when looking for wheel data dirs
Browse files Browse the repository at this point in the history
Fixes aws#1356
Fixes aws#1288
  • Loading branch information
jamesls committed Feb 26, 2020
1 parent 32b824e commit ddf9a2e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
23 changes: 19 additions & 4 deletions chalice/deploy/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,20 @@ def data_dir(self):
# The directory format is {distribution}-{version}.data
return '%s-%s.data' % (self._name, self._version)

def _normalize_name(self, name):
# type: (str) -> str
# Taken directly from PEP 503
return re.sub(r"[-_.]+", "-", name).lower()
def matches_data_dir(self, dirname):
# type: (str) -> bool
"""Checks if a directory name matches the data_dir of a package.
This will normalize the directory name and perform a case-insensitive
match against the package name's data dir.
"""
if not self.dist_type == 'wheel':
return False
name, version = dirname.split('-')[:2]
comparison_data_dir = '%s-%s' % (self._normalize_name(name), version)
comparison_data_dir += dirname[len(comparison_data_dir):]
return self.data_dir == comparison_data_dir

@property
def identifier(self):
Expand Down Expand Up @@ -603,6 +613,11 @@ def _calculate_name_and_version(self):
normalized_name = self._normalize_name(name)
return normalized_name, version

def _normalize_name(self, name):
# type: (str) -> str
# Taken directly from PEP 503
return re.sub(r"[-_.]+", "-", name).lower()


class SDistMetadataFetcher(object):
"""This is the "correct" way to get name and version from an sdist."""
Expand Down
24 changes: 23 additions & 1 deletion tests/functional/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,28 @@ def test_can_expand_purelib_whl(self, tmpdir, pip_runner):
for req in reqs:
assert req in installed_packages

def test_can_normalize_dirname_for_purelib_whl(self, tmpdir, pip_runner):
reqs = ['foo']
pip, runner = pip_runner
appdir, builder = self._make_appdir_and_dependency_builder(
reqs, tmpdir, runner)
requirements_file = os.path.join(appdir, 'requirements.txt')
pip.packages_to_download(
expected_args=['-r', requirements_file, '--dest', mock.ANY],
packages=[
'foo-1.2-cp36-cp36m-manylinux1_x86_64.whl'
],
whl_contents=['Foo-1.2.data/purelib/foo/']
)

site_packages = os.path.join(appdir, '.chalice.', 'site-packages')
builder.build_site_packages('cp36m', requirements_file, site_packages)
installed_packages = os.listdir(site_packages)

pip.validate()
for req in reqs:
assert req in installed_packages

def test_can_expand_platlib_whl(self, tmpdir, pip_runner):
reqs = ['foo']
pip, runner = pip_runner
Expand All @@ -380,7 +402,7 @@ def test_can_expand_platlib_whl(self, tmpdir, pip_runner):
packages=[
'foo-1.2-cp36-cp36m-manylinux1_x86_64.whl'
],
whl_contents=['foo-1.2.data/platlib/foo/']
whl_contents=['Foo-1.2.data/platlib/foo/']
)

site_packages = os.path.join(appdir, '.chalice.', 'site-packages')
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/deploy/test_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ def test_can_read_packages_with_period_in_name(self):
pkg = Package('', 'foo.bar-2.0-py3-none-any.whl')
assert pkg.identifier == 'foo-bar==2.0'

def test_can_normalize_data_dir(self):
pkg = Package('', 'Foobar-2.0-py3-none-any.whl')
assert pkg.data_dir == 'foobar-2.0.data'

def test_can_normalize_dirname_comparisons(self):
pkg = Package('', 'Foobar-2.0-py3-none-any.whl')
assert pkg.matches_data_dir('Foobar-2.0.data')
assert pkg.matches_data_dir('foobar-2.0.data')
assert not pkg.matches_data_dir('other-2.0.data')
assert not pkg.matches_data_dir('foobar-2.0.datastuff')
assert not pkg.matches_data_dir('foobar-2.0')


class TestPipRunner(object):
def test_does_propagate_env_vars(self, pip_factory):
Expand Down

0 comments on commit ddf9a2e

Please sign in to comment.