Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/setuptools/wheel.py
7762 views
"""Wheels support."""12from distutils.util import get_platform3from distutils import log4import email5import itertools6import os7import posixpath8import re9import zipfile1011import pkg_resources12import setuptools13from pkg_resources import parse_version14from setuptools.extern.packaging.tags import sys_tags15from setuptools.extern.packaging.utils import canonicalize_name16from setuptools.extern.six import PY317from setuptools.command.egg_info import write_requirements181920__metaclass__ = type212223WHEEL_NAME = re.compile(24r"""^(?P<project_name>.+?)-(?P<version>\d.*?)25((-(?P<build>\d.*?))?-(?P<py_version>.+?)-(?P<abi>.+?)-(?P<platform>.+?)26)\.whl$""",27re.VERBOSE).match2829NAMESPACE_PACKAGE_INIT = \30"__import__('pkg_resources').declare_namespace(__name__)\n"313233def unpack(src_dir, dst_dir):34'''Move everything under `src_dir` to `dst_dir`, and delete the former.'''35for dirpath, dirnames, filenames in os.walk(src_dir):36subdir = os.path.relpath(dirpath, src_dir)37for f in filenames:38src = os.path.join(dirpath, f)39dst = os.path.join(dst_dir, subdir, f)40os.renames(src, dst)41for n, d in reversed(list(enumerate(dirnames))):42src = os.path.join(dirpath, d)43dst = os.path.join(dst_dir, subdir, d)44if not os.path.exists(dst):45# Directory does not exist in destination,46# rename it and prune it from os.walk list.47os.renames(src, dst)48del dirnames[n]49# Cleanup.50for dirpath, dirnames, filenames in os.walk(src_dir, topdown=True):51assert not filenames52os.rmdir(dirpath)535455class Wheel:5657def __init__(self, filename):58match = WHEEL_NAME(os.path.basename(filename))59if match is None:60raise ValueError('invalid wheel name: %r' % filename)61self.filename = filename62for k, v in match.groupdict().items():63setattr(self, k, v)6465def tags(self):66'''List tags (py_version, abi, platform) supported by this wheel.'''67return itertools.product(68self.py_version.split('.'),69self.abi.split('.'),70self.platform.split('.'),71)7273def is_compatible(self):74'''Is the wheel is compatible with the current platform?'''75supported_tags = set(76(t.interpreter, t.abi, t.platform) for t in sys_tags())77return next((True for t in self.tags() if t in supported_tags), False)7879def egg_name(self):80return pkg_resources.Distribution(81project_name=self.project_name, version=self.version,82platform=(None if self.platform == 'any' else get_platform()),83).egg_name() + '.egg'8485def get_dist_info(self, zf):86# find the correct name of the .dist-info dir in the wheel file87for member in zf.namelist():88dirname = posixpath.dirname(member)89if (dirname.endswith('.dist-info') and90canonicalize_name(dirname).startswith(91canonicalize_name(self.project_name))):92return dirname93raise ValueError("unsupported wheel format. .dist-info not found")9495def install_as_egg(self, destination_eggdir):96'''Install wheel as an egg directory.'''97with zipfile.ZipFile(self.filename) as zf:98self._install_as_egg(destination_eggdir, zf)99100def _install_as_egg(self, destination_eggdir, zf):101dist_basename = '%s-%s' % (self.project_name, self.version)102dist_info = self.get_dist_info(zf)103dist_data = '%s.data' % dist_basename104egg_info = os.path.join(destination_eggdir, 'EGG-INFO')105106self._convert_metadata(zf, destination_eggdir, dist_info, egg_info)107self._move_data_entries(destination_eggdir, dist_data)108self._fix_namespace_packages(egg_info, destination_eggdir)109110@staticmethod111def _convert_metadata(zf, destination_eggdir, dist_info, egg_info):112def get_metadata(name):113with zf.open(posixpath.join(dist_info, name)) as fp:114value = fp.read().decode('utf-8') if PY3 else fp.read()115return email.parser.Parser().parsestr(value)116117wheel_metadata = get_metadata('WHEEL')118# Check wheel format version is supported.119wheel_version = parse_version(wheel_metadata.get('Wheel-Version'))120wheel_v1 = (121parse_version('1.0') <= wheel_version < parse_version('2.0dev0')122)123if not wheel_v1:124raise ValueError(125'unsupported wheel format version: %s' % wheel_version)126# Extract to target directory.127os.mkdir(destination_eggdir)128zf.extractall(destination_eggdir)129# Convert metadata.130dist_info = os.path.join(destination_eggdir, dist_info)131dist = pkg_resources.Distribution.from_location(132destination_eggdir, dist_info,133metadata=pkg_resources.PathMetadata(destination_eggdir, dist_info),134)135136# Note: Evaluate and strip markers now,137# as it's difficult to convert back from the syntax:138# foobar; "linux" in sys_platform and extra == 'test'139def raw_req(req):140req.marker = None141return str(req)142install_requires = list(sorted(map(raw_req, dist.requires())))143extras_require = {144extra: sorted(145req146for req in map(raw_req, dist.requires((extra,)))147if req not in install_requires148)149for extra in dist.extras150}151os.rename(dist_info, egg_info)152os.rename(153os.path.join(egg_info, 'METADATA'),154os.path.join(egg_info, 'PKG-INFO'),155)156setup_dist = setuptools.Distribution(157attrs=dict(158install_requires=install_requires,159extras_require=extras_require,160),161)162# Temporarily disable info traces.163log_threshold = log._global_log.threshold164log.set_threshold(log.WARN)165try:166write_requirements(167setup_dist.get_command_obj('egg_info'),168None,169os.path.join(egg_info, 'requires.txt'),170)171finally:172log.set_threshold(log_threshold)173174@staticmethod175def _move_data_entries(destination_eggdir, dist_data):176"""Move data entries to their correct location."""177dist_data = os.path.join(destination_eggdir, dist_data)178dist_data_scripts = os.path.join(dist_data, 'scripts')179if os.path.exists(dist_data_scripts):180egg_info_scripts = os.path.join(181destination_eggdir, 'EGG-INFO', 'scripts')182os.mkdir(egg_info_scripts)183for entry in os.listdir(dist_data_scripts):184# Remove bytecode, as it's not properly handled185# during easy_install scripts install phase.186if entry.endswith('.pyc'):187os.unlink(os.path.join(dist_data_scripts, entry))188else:189os.rename(190os.path.join(dist_data_scripts, entry),191os.path.join(egg_info_scripts, entry),192)193os.rmdir(dist_data_scripts)194for subdir in filter(os.path.exists, (195os.path.join(dist_data, d)196for d in ('data', 'headers', 'purelib', 'platlib')197)):198unpack(subdir, destination_eggdir)199if os.path.exists(dist_data):200os.rmdir(dist_data)201202@staticmethod203def _fix_namespace_packages(egg_info, destination_eggdir):204namespace_packages = os.path.join(205egg_info, 'namespace_packages.txt')206if os.path.exists(namespace_packages):207with open(namespace_packages) as fp:208namespace_packages = fp.read().split()209for mod in namespace_packages:210mod_dir = os.path.join(destination_eggdir, *mod.split('.'))211mod_init = os.path.join(mod_dir, '__init__.py')212if not os.path.exists(mod_dir):213os.mkdir(mod_dir)214if not os.path.exists(mod_init):215with open(mod_init, 'w') as fp:216fp.write(NAMESPACE_PACKAGE_INIT)217218219