|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from distutils import log |
| 6 | +from distutils.errors import DistutilsError |
| 7 | + |
| 8 | +import pkg_resources |
| 9 | +from setuptools.command.easy_install import easy_install |
| 10 | +from setuptools.wheel import Wheel |
| 11 | + |
| 12 | +from .py31compat import TemporaryDirectory |
| 13 | + |
| 14 | + |
| 15 | +def _legacy_fetch_build_egg(dist, req): |
| 16 | + """Fetch an egg needed for building. |
| 17 | +
|
| 18 | + Legacy path using EasyInstall. |
| 19 | + """ |
| 20 | + tmp_dist = dist.__class__({'script_args': ['easy_install']}) |
| 21 | + opts = tmp_dist.get_option_dict('easy_install') |
| 22 | + opts.clear() |
| 23 | + opts.update( |
| 24 | + (k, v) |
| 25 | + for k, v in dist.get_option_dict('easy_install').items() |
| 26 | + if k in ( |
| 27 | + # don't use any other settings |
| 28 | + 'find_links', 'site_dirs', 'index_url', |
| 29 | + 'optimize', 'site_dirs', 'allow_hosts', |
| 30 | + )) |
| 31 | + if dist.dependency_links: |
| 32 | + links = dist.dependency_links[:] |
| 33 | + if 'find_links' in opts: |
| 34 | + links = opts['find_links'][1] + links |
| 35 | + opts['find_links'] = ('setup', links) |
| 36 | + install_dir = dist.get_egg_cache_dir() |
| 37 | + cmd = easy_install( |
| 38 | + tmp_dist, args=["x"], install_dir=install_dir, |
| 39 | + exclude_scripts=True, |
| 40 | + always_copy=False, build_directory=None, editable=False, |
| 41 | + upgrade=False, multi_version=True, no_report=True, user=False |
| 42 | + ) |
| 43 | + cmd.ensure_finalized() |
| 44 | + return cmd.easy_install(req) |
| 45 | + |
| 46 | + |
| 47 | +def fetch_build_egg(dist, req): |
| 48 | + """Fetch an egg needed for building. |
| 49 | +
|
| 50 | + Use pip/wheel to fetch/build a wheel.""" |
| 51 | + # Check pip is available. |
| 52 | + try: |
| 53 | + pkg_resources.get_distribution('pip') |
| 54 | + except pkg_resources.DistributionNotFound: |
| 55 | + dist.announce( |
| 56 | + 'WARNING: The pip package is not available, falling back ' |
| 57 | + 'to EasyInstall for handling setup_requires/test_requires; ' |
| 58 | + 'this is deprecated and will be removed in a future version.' |
| 59 | + , log.WARN |
| 60 | + ) |
| 61 | + return _legacy_fetch_build_egg(dist, req) |
| 62 | + # Warn if wheel is not. |
| 63 | + try: |
| 64 | + pkg_resources.get_distribution('wheel') |
| 65 | + except pkg_resources.DistributionNotFound: |
| 66 | + dist.announce('WARNING: The wheel package is not available.', log.WARN) |
| 67 | + if not isinstance(req, pkg_resources.Requirement): |
| 68 | + req = pkg_resources.Requirement.parse(req) |
| 69 | + # Take easy_install options into account, but do not override relevant |
| 70 | + # pip environment variables (like PIP_INDEX_URL or PIP_QUIET); they'll |
| 71 | + # take precedence. |
| 72 | + opts = dist.get_option_dict('easy_install') |
| 73 | + if 'allow_hosts' in opts: |
| 74 | + raise DistutilsError('the `allow-hosts` option is not supported ' |
| 75 | + 'when using pip to install requirements.') |
| 76 | + if 'PIP_QUIET' in os.environ or 'PIP_VERBOSE' in os.environ: |
| 77 | + quiet = False |
| 78 | + else: |
| 79 | + quiet = True |
| 80 | + if 'PIP_INDEX_URL' in os.environ: |
| 81 | + index_url = None |
| 82 | + elif 'index_url' in opts: |
| 83 | + index_url = opts['index_url'][1] |
| 84 | + else: |
| 85 | + index_url = None |
| 86 | + if 'find_links' in opts: |
| 87 | + find_links = opts['find_links'][1][:] |
| 88 | + else: |
| 89 | + find_links = [] |
| 90 | + if dist.dependency_links: |
| 91 | + find_links.extend(dist.dependency_links) |
| 92 | + eggs_dir = os.path.realpath(dist.get_egg_cache_dir()) |
| 93 | + environment = pkg_resources.Environment() |
| 94 | + for egg_dist in pkg_resources.find_distributions(eggs_dir): |
| 95 | + if egg_dist in req and environment.can_add(egg_dist): |
| 96 | + return egg_dist |
| 97 | + with TemporaryDirectory() as tmpdir: |
| 98 | + cmd = [ |
| 99 | + sys.executable, '-m', 'pip', |
| 100 | + '--disable-pip-version-check', |
| 101 | + 'wheel', '--no-deps', |
| 102 | + '-w', tmpdir, |
| 103 | + ] |
| 104 | + if quiet: |
| 105 | + cmd.append('--quiet') |
| 106 | + if index_url is not None: |
| 107 | + cmd.extend(('--index-url', index_url)) |
| 108 | + if find_links is not None: |
| 109 | + for link in find_links: |
| 110 | + cmd.extend(('--find-links', link)) |
| 111 | + # If requirement is a PEP 508 direct URL, directly pass |
| 112 | + # the URL to pip, as `req @ url` does not work on the |
| 113 | + # command line. |
| 114 | + if req.url: |
| 115 | + cmd.append(req.url) |
| 116 | + else: |
| 117 | + cmd.append(str(req)) |
| 118 | + try: |
| 119 | + subprocess.check_call(cmd) |
| 120 | + except subprocess.CalledProcessError as e: |
| 121 | + raise DistutilsError(str(e)) |
| 122 | + wheel = Wheel(glob.glob(os.path.join(tmpdir, '*.whl'))[0]) |
| 123 | + dist_location = os.path.join(eggs_dir, wheel.egg_name()) |
| 124 | + wheel.install_as_egg(dist_location) |
| 125 | + dist_metadata = pkg_resources.PathMetadata( |
| 126 | + dist_location, os.path.join(dist_location, 'EGG-INFO')) |
| 127 | + dist = pkg_resources.Distribution.from_filename( |
| 128 | + dist_location, metadata=dist_metadata) |
| 129 | + return dist |
0 commit comments