Skip to content

Commit

Permalink
Use canonicalize_name to look for .dist-info in wheel files
Browse files Browse the repository at this point in the history
Fixes issue pypa#1350
  • Loading branch information
Arnon Yaari committed May 14, 2018
1 parent 4ec8524 commit 4253507
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions setuptools/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import email
import itertools
import os
import posixpath
import re
import zipfile

from pkg_resources import Distribution, PathMetadata, parse_version
from setuptools.extern.packaging.utils import canonicalize_name
from setuptools.extern.six import PY3
from setuptools import Distribution as SetuptoolsDistribution
from setuptools import pep425tags
Expand Down Expand Up @@ -77,22 +79,39 @@ def egg_name(self):
platform=(None if self.platform == 'any' else get_platform()),
).egg_name() + '.egg'

def verify_wheel(self, zf):

def get_distinfo():
# find the name of the .dist-info dir in the wheel file
for member in zf.namelist():
dirname = posixpath.dirname(member)
if (dirname.endswith('.dist-info') and
canonicalize_name(dirname).startswith(
canonicalize_name(self.name))):
return dirname
raise ValueError("unsupported wheel format. .dist-info not found")

def get_metadata(name):
# open and parse a file from ".dist-info" in the wheel file
with zf.open(posixpath.join(get_distinfo(), name)) as fp:
value = fp.read().decode('utf-8') if PY3 else fp.read()
return email.parser.Parser().parsestr(value)

# Check wheel format version is supported.
wheel_metadata = get_metadata('WHEEL')
wheel_version = parse_version(wheel_metadata.get('Wheel-Version'))
if (wheel_version < parse_version('1.0') or
wheel_version >= parse_version('2.0dev0')):
raise ValueError(
'unsupported wheel format version: %s' % wheel_version)

def install_as_egg(self, destination_eggdir):
'''Install wheel as an egg directory.'''
with zipfile.ZipFile(self.filename) as zf:
dist_basename = '%s-%s' % (self.project_name, self.version)
dist_info = '%s.dist-info' % dist_basename
dist_data = '%s.data' % dist_basename
def get_metadata(name):
with zf.open('%s/%s' % (dist_info, name)) as fp:
value = fp.read().decode('utf-8') if PY3 else fp.read()
return email.parser.Parser().parsestr(value)
wheel_metadata = get_metadata('WHEEL')
dist_metadata = get_metadata('METADATA')
# Check wheel format version is supported.
wheel_version = parse_version(wheel_metadata.get('Wheel-Version'))
if not parse_version('1.0') <= wheel_version < parse_version('2.0dev0'):
raise ValueError('unsupported wheel format version: %s' % wheel_version)
self.verify_wheel(zf)
# Extract to target directory.
os.mkdir(destination_eggdir)
zf.extractall(destination_eggdir)
Expand Down

0 comments on commit 4253507

Please sign in to comment.