Commit c2460c75 authored by Jason R. Coombs's avatar Jason R. Coombs

Extract method to save indentation

parent 885f0b17
...@@ -92,93 +92,96 @@ class Wheel(object): ...@@ -92,93 +92,96 @@ class Wheel(object):
def install_as_egg(self, destination_eggdir): def install_as_egg(self, destination_eggdir):
'''Install wheel as an egg directory.''' '''Install wheel as an egg directory.'''
with zipfile.ZipFile(self.filename) as zf: with zipfile.ZipFile(self.filename) as zf:
dist_basename = '%s-%s' % (self.project_name, self.version) self._install_as_egg(destination_eggdir, zf)
dist_info = self.get_dist_info(zf)
dist_data = '%s.data' % dist_basename def _install_as_egg(self, destination_eggdir, zf):
dist_basename = '%s-%s' % (self.project_name, self.version)
def get_metadata(name): dist_info = self.get_dist_info(zf)
with zf.open(posixpath.join(dist_info, name)) as fp: dist_data = '%s.data' % dist_basename
value = fp.read().decode('utf-8') if PY3 else fp.read()
return email.parser.Parser().parsestr(value) def get_metadata(name):
wheel_metadata = get_metadata('WHEEL') with zf.open(posixpath.join(dist_info, name)) as fp:
# Check wheel format version is supported. value = fp.read().decode('utf-8') if PY3 else fp.read()
wheel_version = parse_version(wheel_metadata.get('Wheel-Version')) return email.parser.Parser().parsestr(value)
wheel_v1 = ( wheel_metadata = get_metadata('WHEEL')
parse_version('1.0') <= wheel_version # Check wheel format version is supported.
< parse_version('2.0dev0') wheel_version = parse_version(wheel_metadata.get('Wheel-Version'))
) wheel_v1 = (
if not wheel_v1: parse_version('1.0') <= wheel_version
raise ValueError( < parse_version('2.0dev0')
'unsupported wheel format version: %s' % wheel_version) )
# Extract to target directory. if not wheel_v1:
os.mkdir(destination_eggdir) raise ValueError(
zf.extractall(destination_eggdir) 'unsupported wheel format version: %s' % wheel_version)
# Convert metadata. # Extract to target directory.
dist_info = os.path.join(destination_eggdir, dist_info) os.mkdir(destination_eggdir)
dist = Distribution.from_location( zf.extractall(destination_eggdir)
destination_eggdir, dist_info, # Convert metadata.
metadata=PathMetadata(destination_eggdir, dist_info) dist_info = os.path.join(destination_eggdir, dist_info)
) dist = Distribution.from_location(
destination_eggdir, dist_info,
# Note: Evaluate and strip markers now, metadata=PathMetadata(destination_eggdir, dist_info)
# as it's difficult to convert back from the syntax: )
# foobar; "linux" in sys_platform and extra == 'test'
def raw_req(req): # Note: Evaluate and strip markers now,
req.marker = None # as it's difficult to convert back from the syntax:
return str(req) # foobar; "linux" in sys_platform and extra == 'test'
install_requires = list(sorted(map(raw_req, dist.requires()))) def raw_req(req):
extras_require = { req.marker = None
extra: list(sorted( return str(req)
req install_requires = list(sorted(map(raw_req, dist.requires())))
for req in map(raw_req, dist.requires((extra,))) extras_require = {
if req not in install_requires extra: list(sorted(
)) req
for extra in dist.extras for req in map(raw_req, dist.requires((extra,)))
} if req not in install_requires
egg_info = os.path.join(destination_eggdir, 'EGG-INFO')
os.rename(dist_info, egg_info)
os.rename(os.path.join(egg_info, 'METADATA'),
os.path.join(egg_info, 'PKG-INFO'))
setup_dist = SetuptoolsDistribution(attrs=dict(
install_requires=install_requires,
extras_require=extras_require,
)) ))
write_requirements(setup_dist.get_command_obj('egg_info'), for extra in dist.extras
None, os.path.join(egg_info, 'requires.txt')) }
# Move data entries to their correct location. egg_info = os.path.join(destination_eggdir, 'EGG-INFO')
dist_data = os.path.join(destination_eggdir, dist_data) os.rename(dist_info, egg_info)
dist_data_scripts = os.path.join(dist_data, 'scripts') os.rename(os.path.join(egg_info, 'METADATA'),
if os.path.exists(dist_data_scripts): os.path.join(egg_info, 'PKG-INFO'))
egg_info_scripts = os.path.join(destination_eggdir, setup_dist = SetuptoolsDistribution(attrs=dict(
'EGG-INFO', 'scripts') install_requires=install_requires,
os.mkdir(egg_info_scripts) extras_require=extras_require,
for entry in os.listdir(dist_data_scripts): ))
# Remove bytecode, as it's not properly handled write_requirements(setup_dist.get_command_obj('egg_info'),
# during easy_install scripts install phase. None, os.path.join(egg_info, 'requires.txt'))
if entry.endswith('.pyc'): # Move data entries to their correct location.
os.unlink(os.path.join(dist_data_scripts, entry)) dist_data = os.path.join(destination_eggdir, dist_data)
else: dist_data_scripts = os.path.join(dist_data, 'scripts')
os.rename(os.path.join(dist_data_scripts, entry), if os.path.exists(dist_data_scripts):
os.path.join(egg_info_scripts, entry)) egg_info_scripts = os.path.join(destination_eggdir,
os.rmdir(dist_data_scripts) 'EGG-INFO', 'scripts')
for subdir in filter(os.path.exists, ( os.mkdir(egg_info_scripts)
os.path.join(dist_data, d) for entry in os.listdir(dist_data_scripts):
for d in ('data', 'headers', 'purelib', 'platlib') # Remove bytecode, as it's not properly handled
)): # during easy_install scripts install phase.
unpack(subdir, destination_eggdir) if entry.endswith('.pyc'):
if os.path.exists(dist_data): os.unlink(os.path.join(dist_data_scripts, entry))
os.rmdir(dist_data) else:
# Fix namespace packages. os.rename(os.path.join(dist_data_scripts, entry),
namespace_packages = os.path.join( os.path.join(egg_info_scripts, entry))
egg_info, 'namespace_packages.txt') os.rmdir(dist_data_scripts)
if os.path.exists(namespace_packages): for subdir in filter(os.path.exists, (
with open(namespace_packages) as fp: os.path.join(dist_data, d)
namespace_packages = fp.read().split() for d in ('data', 'headers', 'purelib', 'platlib')
for mod in namespace_packages: )):
mod_dir = os.path.join(destination_eggdir, *mod.split('.')) unpack(subdir, destination_eggdir)
mod_init = os.path.join(mod_dir, '__init__.py') if os.path.exists(dist_data):
if ( os.rmdir(dist_data)
os.path.exists(mod_dir) # Fix namespace packages.
and not os.path.exists(mod_init)): namespace_packages = os.path.join(
with open(mod_init, 'w') as fp: egg_info, 'namespace_packages.txt')
fp.write(NAMESPACE_PACKAGE_INIT) if os.path.exists(namespace_packages):
with open(namespace_packages) as fp:
namespace_packages = fp.read().split()
for mod in namespace_packages:
mod_dir = os.path.join(destination_eggdir, *mod.split('.'))
mod_init = os.path.join(mod_dir, '__init__.py')
if (
os.path.exists(mod_dir)
and not os.path.exists(mod_init)):
with open(mod_init, 'w') as fp:
fp.write(NAMESPACE_PACKAGE_INIT)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment