installer.py 5.2 KB

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