annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/setuptools/installer.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 import glob
jpayne@68 2 import os
jpayne@68 3 import subprocess
jpayne@68 4 import sys
jpayne@68 5 import tempfile
jpayne@68 6 from functools import partial
jpayne@68 7
jpayne@68 8 from . import _reqs
jpayne@68 9 from ._reqs import _StrOrIter
jpayne@68 10 from .warnings import SetuptoolsDeprecationWarning
jpayne@68 11 from .wheel import Wheel
jpayne@68 12
jpayne@68 13 from distutils import log
jpayne@68 14 from distutils.errors import DistutilsError
jpayne@68 15
jpayne@68 16
jpayne@68 17 def _fixup_find_links(find_links):
jpayne@68 18 """Ensure find-links option end-up being a list of strings."""
jpayne@68 19 if isinstance(find_links, str):
jpayne@68 20 return find_links.split()
jpayne@68 21 assert isinstance(find_links, (tuple, list))
jpayne@68 22 return find_links
jpayne@68 23
jpayne@68 24
jpayne@68 25 def fetch_build_egg(dist, req):
jpayne@68 26 """Fetch an egg needed for building.
jpayne@68 27
jpayne@68 28 Use pip/wheel to fetch/build a wheel."""
jpayne@68 29 _DeprecatedInstaller.emit()
jpayne@68 30 _warn_wheel_not_available(dist)
jpayne@68 31 return _fetch_build_egg_no_warn(dist, req)
jpayne@68 32
jpayne@68 33
jpayne@68 34 def _fetch_build_eggs(dist, requires: _StrOrIter):
jpayne@68 35 import pkg_resources # Delay import to avoid unnecessary side-effects
jpayne@68 36
jpayne@68 37 _DeprecatedInstaller.emit(stacklevel=3)
jpayne@68 38 _warn_wheel_not_available(dist)
jpayne@68 39
jpayne@68 40 resolved_dists = pkg_resources.working_set.resolve(
jpayne@68 41 _reqs.parse(requires, pkg_resources.Requirement), # required for compatibility
jpayne@68 42 installer=partial(_fetch_build_egg_no_warn, dist), # avoid warning twice
jpayne@68 43 replace_conflicting=True,
jpayne@68 44 )
jpayne@68 45 for dist in resolved_dists:
jpayne@68 46 pkg_resources.working_set.add(dist, replace=True)
jpayne@68 47 return resolved_dists
jpayne@68 48
jpayne@68 49
jpayne@68 50 def _fetch_build_egg_no_warn(dist, req): # noqa: C901 # is too complex (16) # FIXME
jpayne@68 51 import pkg_resources # Delay import to avoid unnecessary side-effects
jpayne@68 52
jpayne@68 53 # Ignore environment markers; if supplied, it is required.
jpayne@68 54 req = strip_marker(req)
jpayne@68 55 # Take easy_install options into account, but do not override relevant
jpayne@68 56 # pip environment variables (like PIP_INDEX_URL or PIP_QUIET); they'll
jpayne@68 57 # take precedence.
jpayne@68 58 opts = dist.get_option_dict('easy_install')
jpayne@68 59 if 'allow_hosts' in opts:
jpayne@68 60 raise DistutilsError(
jpayne@68 61 'the `allow-hosts` option is not supported '
jpayne@68 62 'when using pip to install requirements.'
jpayne@68 63 )
jpayne@68 64 quiet = 'PIP_QUIET' not in os.environ and 'PIP_VERBOSE' not in os.environ
jpayne@68 65 if 'PIP_INDEX_URL' in os.environ:
jpayne@68 66 index_url = None
jpayne@68 67 elif 'index_url' in opts:
jpayne@68 68 index_url = opts['index_url'][1]
jpayne@68 69 else:
jpayne@68 70 index_url = None
jpayne@68 71 find_links = (
jpayne@68 72 _fixup_find_links(opts['find_links'][1])[:] if 'find_links' in opts else []
jpayne@68 73 )
jpayne@68 74 if dist.dependency_links:
jpayne@68 75 find_links.extend(dist.dependency_links)
jpayne@68 76 eggs_dir = os.path.realpath(dist.get_egg_cache_dir())
jpayne@68 77 environment = pkg_resources.Environment()
jpayne@68 78 for egg_dist in pkg_resources.find_distributions(eggs_dir):
jpayne@68 79 if egg_dist in req and environment.can_add(egg_dist):
jpayne@68 80 return egg_dist
jpayne@68 81 with tempfile.TemporaryDirectory() as tmpdir:
jpayne@68 82 cmd = [
jpayne@68 83 sys.executable,
jpayne@68 84 '-m',
jpayne@68 85 'pip',
jpayne@68 86 '--disable-pip-version-check',
jpayne@68 87 'wheel',
jpayne@68 88 '--no-deps',
jpayne@68 89 '-w',
jpayne@68 90 tmpdir,
jpayne@68 91 ]
jpayne@68 92 if quiet:
jpayne@68 93 cmd.append('--quiet')
jpayne@68 94 if index_url is not None:
jpayne@68 95 cmd.extend(('--index-url', index_url))
jpayne@68 96 for link in find_links or []:
jpayne@68 97 cmd.extend(('--find-links', link))
jpayne@68 98 # If requirement is a PEP 508 direct URL, directly pass
jpayne@68 99 # the URL to pip, as `req @ url` does not work on the
jpayne@68 100 # command line.
jpayne@68 101 cmd.append(req.url or str(req))
jpayne@68 102 try:
jpayne@68 103 subprocess.check_call(cmd)
jpayne@68 104 except subprocess.CalledProcessError as e:
jpayne@68 105 raise DistutilsError(str(e)) from e
jpayne@68 106 wheel = Wheel(glob.glob(os.path.join(tmpdir, '*.whl'))[0])
jpayne@68 107 dist_location = os.path.join(eggs_dir, wheel.egg_name())
jpayne@68 108 wheel.install_as_egg(dist_location)
jpayne@68 109 dist_metadata = pkg_resources.PathMetadata(
jpayne@68 110 dist_location, os.path.join(dist_location, 'EGG-INFO')
jpayne@68 111 )
jpayne@68 112 return pkg_resources.Distribution.from_filename(
jpayne@68 113 dist_location, metadata=dist_metadata
jpayne@68 114 )
jpayne@68 115
jpayne@68 116
jpayne@68 117 def strip_marker(req):
jpayne@68 118 """
jpayne@68 119 Return a new requirement without the environment marker to avoid
jpayne@68 120 calling pip with something like `babel; extra == "i18n"`, which
jpayne@68 121 would always be ignored.
jpayne@68 122 """
jpayne@68 123 import pkg_resources # Delay import to avoid unnecessary side-effects
jpayne@68 124
jpayne@68 125 # create a copy to avoid mutating the input
jpayne@68 126 req = pkg_resources.Requirement.parse(str(req))
jpayne@68 127 req.marker = None
jpayne@68 128 return req
jpayne@68 129
jpayne@68 130
jpayne@68 131 def _warn_wheel_not_available(dist):
jpayne@68 132 import pkg_resources # Delay import to avoid unnecessary side-effects
jpayne@68 133
jpayne@68 134 try:
jpayne@68 135 pkg_resources.get_distribution('wheel')
jpayne@68 136 except pkg_resources.DistributionNotFound:
jpayne@68 137 dist.announce('WARNING: The wheel package is not available.', log.WARN)
jpayne@68 138
jpayne@68 139
jpayne@68 140 class _DeprecatedInstaller(SetuptoolsDeprecationWarning):
jpayne@68 141 _SUMMARY = "setuptools.installer and fetch_build_eggs are deprecated."
jpayne@68 142 _DETAILS = """
jpayne@68 143 Requirements should be satisfied by a PEP 517 installer.
jpayne@68 144 If you are using pip, you can try `pip install --use-pep517`.
jpayne@68 145 """
jpayne@68 146 # _DUE_DATE not decided yet