Path: blob/main/test/lib/python3.9/site-packages/setuptools/installer.py
4798 views
import glob1import os2import subprocess3import sys4import tempfile5import warnings6from distutils import log7from distutils.errors import DistutilsError89import pkg_resources10from setuptools.wheel import Wheel11from ._deprecation_warning import SetuptoolsDeprecationWarning121314def _fixup_find_links(find_links):15"""Ensure find-links option end-up being a list of strings."""16if isinstance(find_links, str):17return find_links.split()18assert isinstance(find_links, (tuple, list))19return find_links202122def fetch_build_egg(dist, req): # noqa: C901 # is too complex (16) # FIXME23"""Fetch an egg needed for building.2425Use pip/wheel to fetch/build a wheel."""26warnings.warn(27"setuptools.installer is deprecated. Requirements should "28"be satisfied by a PEP 517 installer.",29SetuptoolsDeprecationWarning,30)31# Warn if wheel is not available32try:33pkg_resources.get_distribution('wheel')34except pkg_resources.DistributionNotFound:35dist.announce('WARNING: The wheel package is not available.', log.WARN)36# Ignore environment markers; if supplied, it is required.37req = strip_marker(req)38# Take easy_install options into account, but do not override relevant39# pip environment variables (like PIP_INDEX_URL or PIP_QUIET); they'll40# take precedence.41opts = dist.get_option_dict('easy_install')42if 'allow_hosts' in opts:43raise DistutilsError('the `allow-hosts` option is not supported '44'when using pip to install requirements.')45quiet = 'PIP_QUIET' not in os.environ and 'PIP_VERBOSE' not in os.environ46if 'PIP_INDEX_URL' in os.environ:47index_url = None48elif 'index_url' in opts:49index_url = opts['index_url'][1]50else:51index_url = None52find_links = (53_fixup_find_links(opts['find_links'][1])[:] if 'find_links' in opts54else []55)56if dist.dependency_links:57find_links.extend(dist.dependency_links)58eggs_dir = os.path.realpath(dist.get_egg_cache_dir())59environment = pkg_resources.Environment()60for egg_dist in pkg_resources.find_distributions(eggs_dir):61if egg_dist in req and environment.can_add(egg_dist):62return egg_dist63with tempfile.TemporaryDirectory() as tmpdir:64cmd = [65sys.executable, '-m', 'pip',66'--disable-pip-version-check',67'wheel', '--no-deps',68'-w', tmpdir,69]70if quiet:71cmd.append('--quiet')72if index_url is not None:73cmd.extend(('--index-url', index_url))74for link in find_links or []:75cmd.extend(('--find-links', link))76# If requirement is a PEP 508 direct URL, directly pass77# the URL to pip, as `req @ url` does not work on the78# command line.79cmd.append(req.url or str(req))80try:81subprocess.check_call(cmd)82except subprocess.CalledProcessError as e:83raise DistutilsError(str(e)) from e84wheel = Wheel(glob.glob(os.path.join(tmpdir, '*.whl'))[0])85dist_location = os.path.join(eggs_dir, wheel.egg_name())86wheel.install_as_egg(dist_location)87dist_metadata = pkg_resources.PathMetadata(88dist_location, os.path.join(dist_location, 'EGG-INFO'))89dist = pkg_resources.Distribution.from_filename(90dist_location, metadata=dist_metadata)91return dist929394def strip_marker(req):95"""96Return a new requirement without the environment marker to avoid97calling pip with something like `babel; extra == "i18n"`, which98would always be ignored.99"""100# create a copy to avoid mutating the input101req = pkg_resources.Requirement.parse(str(req))102req.marker = None103return req104105106