You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

831 lines
31 KiB

4 years ago
  1. """
  2. Support for installing and building the "wheel" binary package format.
  3. """
  4. from __future__ import absolute_import
  5. import collections
  6. import compileall
  7. import csv
  8. import hashlib
  9. import logging
  10. import os.path
  11. import re
  12. import shutil
  13. import stat
  14. import sys
  15. import warnings
  16. from base64 import urlsafe_b64encode
  17. from email.parser import Parser
  18. from pip._vendor import pkg_resources
  19. from pip._vendor.distlib.scripts import ScriptMaker
  20. from pip._vendor.packaging.utils import canonicalize_name
  21. from pip._vendor.six import StringIO
  22. from pip._internal import pep425tags
  23. from pip._internal.download import path_to_url, unpack_url
  24. from pip._internal.exceptions import (
  25. InstallationError, InvalidWheelFilename, UnsupportedWheel,
  26. )
  27. from pip._internal.locations import (
  28. PIP_DELETE_MARKER_FILENAME, distutils_scheme,
  29. )
  30. from pip._internal.utils.logging import indent_log
  31. from pip._internal.utils.misc import (
  32. call_subprocess, captured_stdout, ensure_dir, read_chunks,
  33. )
  34. from pip._internal.utils.setuptools_build import SETUPTOOLS_SHIM
  35. from pip._internal.utils.temp_dir import TempDirectory
  36. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  37. from pip._internal.utils.ui import open_spinner
  38. if MYPY_CHECK_RUNNING:
  39. from typing import Dict, List, Optional # noqa: F401
  40. wheel_ext = '.whl'
  41. VERSION_COMPATIBLE = (1, 0)
  42. logger = logging.getLogger(__name__)
  43. def rehash(path, blocksize=1 << 20):
  44. """Return (hash, length) for path using hashlib.sha256()"""
  45. h = hashlib.sha256()
  46. length = 0
  47. with open(path, 'rb') as f:
  48. for block in read_chunks(f, size=blocksize):
  49. length += len(block)
  50. h.update(block)
  51. digest = 'sha256=' + urlsafe_b64encode(
  52. h.digest()
  53. ).decode('latin1').rstrip('=')
  54. return (digest, length)
  55. def open_for_csv(name, mode):
  56. if sys.version_info[0] < 3:
  57. nl = {}
  58. bin = 'b'
  59. else:
  60. nl = {'newline': ''}
  61. bin = ''
  62. return open(name, mode + bin, **nl)
  63. def fix_script(path):
  64. """Replace #!python with #!/path/to/python
  65. Return True if file was changed."""
  66. # XXX RECORD hashes will need to be updated
  67. if os.path.isfile(path):
  68. with open(path, 'rb') as script:
  69. firstline = script.readline()
  70. if not firstline.startswith(b'#!python'):
  71. return False
  72. exename = sys.executable.encode(sys.getfilesystemencoding())
  73. firstline = b'#!' + exename + os.linesep.encode("ascii")
  74. rest = script.read()
  75. with open(path, 'wb') as script:
  76. script.write(firstline)
  77. script.write(rest)
  78. return True
  79. dist_info_re = re.compile(r"""^(?P<namever>(?P<name>.+?)(-(?P<ver>.+?))?)
  80. \.dist-info$""", re.VERBOSE)
  81. def root_is_purelib(name, wheeldir):
  82. """
  83. Return True if the extracted wheel in wheeldir should go into purelib.
  84. """
  85. name_folded = name.replace("-", "_")
  86. for item in os.listdir(wheeldir):
  87. match = dist_info_re.match(item)
  88. if match and match.group('name') == name_folded:
  89. with open(os.path.join(wheeldir, item, 'WHEEL')) as wheel:
  90. for line in wheel:
  91. line = line.lower().rstrip()
  92. if line == "root-is-purelib: true":
  93. return True
  94. return False
  95. def get_entrypoints(filename):
  96. if not os.path.exists(filename):
  97. return {}, {}
  98. # This is done because you can pass a string to entry_points wrappers which
  99. # means that they may or may not be valid INI files. The attempt here is to
  100. # strip leading and trailing whitespace in order to make them valid INI
  101. # files.
  102. with open(filename) as fp:
  103. data = StringIO()
  104. for line in fp:
  105. data.write(line.strip())
  106. data.write("\n")
  107. data.seek(0)
  108. # get the entry points and then the script names
  109. entry_points = pkg_resources.EntryPoint.parse_map(data)
  110. console = entry_points.get('console_scripts', {})
  111. gui = entry_points.get('gui_scripts', {})
  112. def _split_ep(s):
  113. """get the string representation of EntryPoint, remove space and split
  114. on '='"""
  115. return str(s).replace(" ", "").split("=")
  116. # convert the EntryPoint objects into strings with module:function
  117. console = dict(_split_ep(v) for v in console.values())
  118. gui = dict(_split_ep(v) for v in gui.values())
  119. return console, gui
  120. def message_about_scripts_not_on_PATH(scripts):
  121. # type: (List[str]) -> Optional[str]
  122. """Determine if any scripts are not on PATH and format a warning.
  123. Returns a warning message if one or more scripts are not on PATH,
  124. otherwise None.
  125. """
  126. if not scripts:
  127. return None
  128. # Group scripts by the path they were installed in
  129. grouped_by_dir = collections.defaultdict(set) # type: Dict[str, set]
  130. for destfile in scripts:
  131. parent_dir = os.path.dirname(destfile)
  132. script_name = os.path.basename(destfile)
  133. grouped_by_dir[parent_dir].add(script_name)
  134. # We don't want to warn for directories that are on PATH.
  135. not_warn_dirs = [
  136. os.path.normcase(i).rstrip(os.sep) for i in
  137. os.environ.get("PATH", "").split(os.pathsep)
  138. ]
  139. # If an executable sits with sys.executable, we don't warn for it.
  140. # This covers the case of venv invocations without activating the venv.
  141. not_warn_dirs.append(os.path.normcase(os.path.dirname(sys.executable)))
  142. warn_for = {
  143. parent_dir: scripts for parent_dir, scripts in grouped_by_dir.items()
  144. if os.path.normcase(parent_dir) not in not_warn_dirs
  145. }
  146. if not warn_for:
  147. return None
  148. # Format a message
  149. msg_lines = []
  150. for parent_dir, scripts in warn_for.items():
  151. scripts = sorted(scripts)
  152. if len(scripts) == 1:
  153. start_text = "script {} is".format(scripts[0])
  154. else:
  155. start_text = "scripts {} are".format(
  156. ", ".join(scripts[:-1]) + " and " + scripts[-1]
  157. )
  158. msg_lines.append(
  159. "The {} installed in '{}' which is not on PATH."
  160. .format(start_text, parent_dir)
  161. )
  162. last_line_fmt = (
  163. "Consider adding {} to PATH or, if you prefer "
  164. "to suppress this warning, use --no-warn-script-location."
  165. )
  166. if len(msg_lines) == 1:
  167. msg_lines.append(last_line_fmt.format("this directory"))
  168. else:
  169. msg_lines.append(last_line_fmt.format("these directories"))
  170. # Returns the formatted multiline message
  171. return "\n".join(msg_lines)
  172. def move_wheel_files(name, req, wheeldir, user=False, home=None, root=None,
  173. pycompile=True, scheme=None, isolated=False, prefix=None,
  174. warn_script_location=True):
  175. """Install a wheel"""
  176. if not scheme:
  177. scheme = distutils_scheme(
  178. name, user=user, home=home, root=root, isolated=isolated,
  179. prefix=prefix,
  180. )
  181. if root_is_purelib(name, wheeldir):
  182. lib_dir = scheme['purelib']
  183. else:
  184. lib_dir = scheme['platlib']
  185. info_dir = []
  186. data_dirs = []
  187. source = wheeldir.rstrip(os.path.sep) + os.path.sep
  188. # Record details of the files moved
  189. # installed = files copied from the wheel to the destination
  190. # changed = files changed while installing (scripts #! line typically)
  191. # generated = files newly generated during the install (script wrappers)
  192. installed = {}
  193. changed = set()
  194. generated = []
  195. # Compile all of the pyc files that we're going to be installing
  196. if pycompile:
  197. with captured_stdout() as stdout:
  198. with warnings.catch_warnings():
  199. warnings.filterwarnings('ignore')
  200. compileall.compile_dir(source, force=True, quiet=True)
  201. logger.debug(stdout.getvalue())
  202. def normpath(src, p):
  203. return os.path.relpath(src, p).replace(os.path.sep, '/')
  204. def record_installed(srcfile, destfile, modified=False):
  205. """Map archive RECORD paths to installation RECORD paths."""
  206. oldpath = normpath(srcfile, wheeldir)
  207. newpath = normpath(destfile, lib_dir)
  208. installed[oldpath] = newpath
  209. if modified:
  210. changed.add(destfile)
  211. def clobber(source, dest, is_base, fixer=None, filter=None):
  212. ensure_dir(dest) # common for the 'include' path
  213. for dir, subdirs, files in os.walk(source):
  214. basedir = dir[len(source):].lstrip(os.path.sep)
  215. destdir = os.path.join(dest, basedir)
  216. if is_base and basedir.split(os.path.sep, 1)[0].endswith('.data'):
  217. continue
  218. for s in subdirs:
  219. destsubdir = os.path.join(dest, basedir, s)
  220. if is_base and basedir == '' and destsubdir.endswith('.data'):
  221. data_dirs.append(s)
  222. continue
  223. elif (is_base and
  224. s.endswith('.dist-info') and
  225. canonicalize_name(s).startswith(
  226. canonicalize_name(req.name))):
  227. assert not info_dir, ('Multiple .dist-info directories: ' +
  228. destsubdir + ', ' +
  229. ', '.join(info_dir))
  230. info_dir.append(destsubdir)
  231. for f in files:
  232. # Skip unwanted files
  233. if filter and filter(f):
  234. continue
  235. srcfile = os.path.join(dir, f)
  236. destfile = os.path.join(dest, basedir, f)
  237. # directory creation is lazy and after the file filtering above
  238. # to ensure we don't install empty dirs; empty dirs can't be
  239. # uninstalled.
  240. ensure_dir(destdir)
  241. # copyfile (called below) truncates the destination if it
  242. # exists and then writes the new contents. This is fine in most
  243. # cases, but can cause a segfault if pip has loaded a shared
  244. # object (e.g. from pyopenssl through its vendored urllib3)
  245. # Since the shared object is mmap'd an attempt to call a
  246. # symbol in it will then cause a segfault. Unlinking the file
  247. # allows writing of new contents while allowing the process to
  248. # continue to use the old copy.
  249. if os.path.exists(destfile):
  250. os.unlink(destfile)
  251. # We use copyfile (not move, copy, or copy2) to be extra sure
  252. # that we are not moving directories over (copyfile fails for
  253. # directories) as well as to ensure that we are not copying
  254. # over any metadata because we want more control over what
  255. # metadata we actually copy over.
  256. shutil.copyfile(srcfile, destfile)
  257. # Copy over the metadata for the file, currently this only
  258. # includes the atime and mtime.
  259. st = os.stat(srcfile)
  260. if hasattr(os, "utime"):
  261. os.utime(destfile, (st.st_atime, st.st_mtime))
  262. # If our file is executable, then make our destination file
  263. # executable.
  264. if os.access(srcfile, os.X_OK):
  265. st = os.stat(srcfile)
  266. permissions = (
  267. st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
  268. )
  269. os.chmod(destfile, permissions)
  270. changed = False
  271. if fixer:
  272. changed = fixer(destfile)
  273. record_installed(srcfile, destfile, changed)
  274. clobber(source, lib_dir, True)
  275. assert info_dir, "%s .dist-info directory not found" % req
  276. # Get the defined entry points
  277. ep_file = os.path.join(info_dir[0], 'entry_points.txt')
  278. console, gui = get_entrypoints(ep_file)
  279. def is_entrypoint_wrapper(name):
  280. # EP, EP.exe and EP-script.py are scripts generated for
  281. # entry point EP by setuptools
  282. if name.lower().endswith('.exe'):
  283. matchname = name[:-4]
  284. elif name.lower().endswith('-script.py'):
  285. matchname = name[:-10]
  286. elif name.lower().endswith(".pya"):
  287. matchname = name[:-4]
  288. else:
  289. matchname = name
  290. # Ignore setuptools-generated scripts
  291. return (matchname in console or matchname in gui)
  292. for datadir in data_dirs:
  293. fixer = None
  294. filter = None
  295. for subdir in os.listdir(os.path.join(wheeldir, datadir)):
  296. fixer = None
  297. if subdir == 'scripts':
  298. fixer = fix_script
  299. filter = is_entrypoint_wrapper
  300. source = os.path.join(wheeldir, datadir, subdir)
  301. dest = scheme[subdir]
  302. clobber(source, dest, False, fixer=fixer, filter=filter)
  303. maker = ScriptMaker(None, scheme['scripts'])
  304. # Ensure old scripts are overwritten.
  305. # See https://github.com/pypa/pip/issues/1800
  306. maker.clobber = True
  307. # Ensure we don't generate any variants for scripts because this is almost
  308. # never what somebody wants.
  309. # See https://bitbucket.org/pypa/distlib/issue/35/
  310. maker.variants = {''}
  311. # This is required because otherwise distlib creates scripts that are not
  312. # executable.
  313. # See https://bitbucket.org/pypa/distlib/issue/32/
  314. maker.set_mode = True
  315. # Simplify the script and fix the fact that the default script swallows
  316. # every single stack trace.
  317. # See https://bitbucket.org/pypa/distlib/issue/34/
  318. # See https://bitbucket.org/pypa/distlib/issue/33/
  319. def _get_script_text(entry):
  320. if entry.suffix is None:
  321. raise InstallationError(
  322. "Invalid script entry point: %s for req: %s - A callable "
  323. "suffix is required. Cf https://packaging.python.org/en/"
  324. "latest/distributing.html#console-scripts for more "
  325. "information." % (entry, req)
  326. )
  327. return maker.script_template % {
  328. "module": entry.prefix,
  329. "import_name": entry.suffix.split(".")[0],
  330. "func": entry.suffix,
  331. }
  332. maker._get_script_text = _get_script_text
  333. maker.script_template = r"""# -*- coding: utf-8 -*-
  334. import re
  335. import sys
  336. from %(module)s import %(import_name)s
  337. if __name__ == '__main__':
  338. sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
  339. sys.exit(%(func)s())
  340. """
  341. # Special case pip and setuptools to generate versioned wrappers
  342. #
  343. # The issue is that some projects (specifically, pip and setuptools) use
  344. # code in setup.py to create "versioned" entry points - pip2.7 on Python
  345. # 2.7, pip3.3 on Python 3.3, etc. But these entry points are baked into
  346. # the wheel metadata at build time, and so if the wheel is installed with
  347. # a *different* version of Python the entry points will be wrong. The
  348. # correct fix for this is to enhance the metadata to be able to describe
  349. # such versioned entry points, but that won't happen till Metadata 2.0 is
  350. # available.
  351. # In the meantime, projects using versioned entry points will either have
  352. # incorrect versioned entry points, or they will not be able to distribute
  353. # "universal" wheels (i.e., they will need a wheel per Python version).
  354. #
  355. # Because setuptools and pip are bundled with _ensurepip and virtualenv,
  356. # we need to use universal wheels. So, as a stopgap until Metadata 2.0, we
  357. # override the versioned entry points in the wheel and generate the
  358. # correct ones. This code is purely a short-term measure until Metadata 2.0
  359. # is available.
  360. #
  361. # To add the level of hack in this section of code, in order to support
  362. # ensurepip this code will look for an ``ENSUREPIP_OPTIONS`` environment
  363. # variable which will control which version scripts get installed.
  364. #
  365. # ENSUREPIP_OPTIONS=altinstall
  366. # - Only pipX.Y and easy_install-X.Y will be generated and installed
  367. # ENSUREPIP_OPTIONS=install
  368. # - pipX.Y, pipX, easy_install-X.Y will be generated and installed. Note
  369. # that this option is technically if ENSUREPIP_OPTIONS is set and is
  370. # not altinstall
  371. # DEFAULT
  372. # - The default behavior is to install pip, pipX, pipX.Y, easy_install
  373. # and easy_install-X.Y.
  374. pip_script = console.pop('pip', None)
  375. if pip_script:
  376. if "ENSUREPIP_OPTIONS" not in os.environ:
  377. spec = 'pip = ' + pip_script
  378. generated.extend(maker.make(spec))
  379. if os.environ.get("ENSUREPIP_OPTIONS", "") != "altinstall":
  380. spec = 'pip%s = %s' % (sys.version[:1], pip_script)
  381. generated.extend(maker.make(spec))
  382. spec = 'pip%s = %s' % (sys.version[:3], pip_script)
  383. generated.extend(maker.make(spec))
  384. # Delete any other versioned pip entry points
  385. pip_ep = [k for k in console if re.match(r'pip(\d(\.\d)?)?$', k)]
  386. for k in pip_ep:
  387. del console[k]
  388. easy_install_script = console.pop('easy_install', None)
  389. if easy_install_script:
  390. if "ENSUREPIP_OPTIONS" not in os.environ:
  391. spec = 'easy_install = ' + easy_install_script
  392. generated.extend(maker.make(spec))
  393. spec = 'easy_install-%s = %s' % (sys.version[:3], easy_install_script)
  394. generated.extend(maker.make(spec))
  395. # Delete any other versioned easy_install entry points
  396. easy_install_ep = [
  397. k for k in console if re.match(r'easy_install(-\d\.\d)?$', k)
  398. ]
  399. for k in easy_install_ep:
  400. del console[k]
  401. # Generate the console and GUI entry points specified in the wheel
  402. if len(console) > 0:
  403. generated_console_scripts = maker.make_multiple(
  404. ['%s = %s' % kv for kv in console.items()]
  405. )
  406. generated.extend(generated_console_scripts)
  407. if warn_script_location:
  408. msg = message_about_scripts_not_on_PATH(generated_console_scripts)
  409. if msg is not None:
  410. logger.warning(msg)
  411. if len(gui) > 0:
  412. generated.extend(
  413. maker.make_multiple(
  414. ['%s = %s' % kv for kv in gui.items()],
  415. {'gui': True}
  416. )
  417. )
  418. # Record pip as the installer
  419. installer = os.path.join(info_dir[0], 'INSTALLER')
  420. temp_installer = os.path.join(info_dir[0], 'INSTALLER.pip')
  421. with open(temp_installer, 'wb') as installer_file:
  422. installer_file.write(b'pip\n')
  423. shutil.move(temp_installer, installer)
  424. generated.append(installer)
  425. # Record details of all files installed
  426. record = os.path.join(info_dir[0], 'RECORD')
  427. temp_record = os.path.join(info_dir[0], 'RECORD.pip')
  428. with open_for_csv(record, 'r') as record_in:
  429. with open_for_csv(temp_record, 'w+') as record_out:
  430. reader = csv.reader(record_in)
  431. writer = csv.writer(record_out)
  432. outrows = []
  433. for row in reader:
  434. row[0] = installed.pop(row[0], row[0])
  435. if row[0] in changed:
  436. row[1], row[2] = rehash(row[0])
  437. outrows.append(tuple(row))
  438. for f in generated:
  439. digest, length = rehash(f)
  440. outrows.append((normpath(f, lib_dir), digest, length))
  441. for f in installed:
  442. outrows.append((installed[f], '', ''))
  443. for row in sorted(outrows):
  444. writer.writerow(row)
  445. shutil.move(temp_record, record)
  446. def wheel_version(source_dir):
  447. """
  448. Return the Wheel-Version of an extracted wheel, if possible.
  449. Otherwise, return False if we couldn't parse / extract it.
  450. """
  451. try:
  452. dist = [d for d in pkg_resources.find_on_path(None, source_dir)][0]
  453. wheel_data = dist.get_metadata('WHEEL')
  454. wheel_data = Parser().parsestr(wheel_data)
  455. version = wheel_data['Wheel-Version'].strip()
  456. version = tuple(map(int, version.split('.')))
  457. return version
  458. except Exception:
  459. return False
  460. def check_compatibility(version, name):
  461. """
  462. Raises errors or warns if called with an incompatible Wheel-Version.
  463. Pip should refuse to install a Wheel-Version that's a major series
  464. ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
  465. installing a version only minor version ahead (e.g 1.2 > 1.1).
  466. version: a 2-tuple representing a Wheel-Version (Major, Minor)
  467. name: name of wheel or package to raise exception about
  468. :raises UnsupportedWheel: when an incompatible Wheel-Version is given
  469. """
  470. if not version:
  471. raise UnsupportedWheel(
  472. "%s is in an unsupported or invalid wheel" % name
  473. )
  474. if version[0] > VERSION_COMPATIBLE[0]:
  475. raise UnsupportedWheel(
  476. "%s's Wheel-Version (%s) is not compatible with this version "
  477. "of pip" % (name, '.'.join(map(str, version)))
  478. )
  479. elif version > VERSION_COMPATIBLE:
  480. logger.warning(
  481. 'Installing from a newer Wheel-Version (%s)',
  482. '.'.join(map(str, version)),
  483. )
  484. class Wheel(object):
  485. """A wheel file"""
  486. # TODO: maybe move the install code into this class
  487. wheel_file_re = re.compile(
  488. r"""^(?P<namever>(?P<name>.+?)-(?P<ver>.*?))
  489. ((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
  490. \.whl|\.dist-info)$""",
  491. re.VERBOSE
  492. )
  493. def __init__(self, filename):
  494. """
  495. :raises InvalidWheelFilename: when the filename is invalid for a wheel
  496. """
  497. wheel_info = self.wheel_file_re.match(filename)
  498. if not wheel_info:
  499. raise InvalidWheelFilename(
  500. "%s is not a valid wheel filename." % filename
  501. )
  502. self.filename = filename
  503. self.name = wheel_info.group('name').replace('_', '-')
  504. # we'll assume "_" means "-" due to wheel naming scheme
  505. # (https://github.com/pypa/pip/issues/1150)
  506. self.version = wheel_info.group('ver').replace('_', '-')
  507. self.build_tag = wheel_info.group('build')
  508. self.pyversions = wheel_info.group('pyver').split('.')
  509. self.abis = wheel_info.group('abi').split('.')
  510. self.plats = wheel_info.group('plat').split('.')
  511. # All the tag combinations from this file
  512. self.file_tags = {
  513. (x, y, z) for x in self.pyversions
  514. for y in self.abis for z in self.plats
  515. }
  516. def support_index_min(self, tags=None):
  517. """
  518. Return the lowest index that one of the wheel's file_tag combinations
  519. achieves in the supported_tags list e.g. if there are 8 supported tags,
  520. and one of the file tags is first in the list, then return 0. Returns
  521. None is the wheel is not supported.
  522. """
  523. if tags is None: # for mock
  524. tags = pep425tags.get_supported()
  525. indexes = [tags.index(c) for c in self.file_tags if c in tags]
  526. return min(indexes) if indexes else None
  527. def supported(self, tags=None):
  528. """Is this wheel supported on this system?"""
  529. if tags is None: # for mock
  530. tags = pep425tags.get_supported()
  531. return bool(set(tags).intersection(self.file_tags))
  532. class WheelBuilder(object):
  533. """Build wheels from a RequirementSet."""
  534. def __init__(self, finder, preparer, wheel_cache,
  535. build_options=None, global_options=None, no_clean=False):
  536. self.finder = finder
  537. self.preparer = preparer
  538. self.wheel_cache = wheel_cache
  539. self._wheel_dir = preparer.wheel_download_dir
  540. self.build_options = build_options or []
  541. self.global_options = global_options or []
  542. self.no_clean = no_clean
  543. def _build_one(self, req, output_dir, python_tag=None):
  544. """Build one wheel.
  545. :return: The filename of the built wheel, or None if the build failed.
  546. """
  547. # Install build deps into temporary directory (PEP 518)
  548. with req.build_env:
  549. return self._build_one_inside_env(req, output_dir,
  550. python_tag=python_tag)
  551. def _build_one_inside_env(self, req, output_dir, python_tag=None):
  552. with TempDirectory(kind="wheel") as temp_dir:
  553. if self.__build_one(req, temp_dir.path, python_tag=python_tag):
  554. try:
  555. wheel_name = os.listdir(temp_dir.path)[0]
  556. wheel_path = os.path.join(output_dir, wheel_name)
  557. shutil.move(
  558. os.path.join(temp_dir.path, wheel_name), wheel_path
  559. )
  560. logger.info('Stored in directory: %s', output_dir)
  561. return wheel_path
  562. except Exception:
  563. pass
  564. # Ignore return, we can't do anything else useful.
  565. self._clean_one(req)
  566. return None
  567. def _base_setup_args(self, req):
  568. # NOTE: Eventually, we'd want to also -S to the flags here, when we're
  569. # isolating. Currently, it breaks Python in virtualenvs, because it
  570. # relies on site.py to find parts of the standard library outside the
  571. # virtualenv.
  572. return [
  573. sys.executable, '-u', '-c',
  574. SETUPTOOLS_SHIM % req.setup_py
  575. ] + list(self.global_options)
  576. def __build_one(self, req, tempd, python_tag=None):
  577. base_args = self._base_setup_args(req)
  578. spin_message = 'Running setup.py bdist_wheel for %s' % (req.name,)
  579. with open_spinner(spin_message) as spinner:
  580. logger.debug('Destination directory: %s', tempd)
  581. wheel_args = base_args + ['bdist_wheel', '-d', tempd] \
  582. + self.build_options
  583. if python_tag is not None:
  584. wheel_args += ["--python-tag", python_tag]
  585. try:
  586. call_subprocess(wheel_args, cwd=req.setup_py_dir,
  587. show_stdout=False, spinner=spinner)
  588. return True
  589. except Exception:
  590. spinner.finish("error")
  591. logger.error('Failed building wheel for %s', req.name)
  592. return False
  593. def _clean_one(self, req):
  594. base_args = self._base_setup_args(req)
  595. logger.info('Running setup.py clean for %s', req.name)
  596. clean_args = base_args + ['clean', '--all']
  597. try:
  598. call_subprocess(clean_args, cwd=req.source_dir, show_stdout=False)
  599. return True
  600. except Exception:
  601. logger.error('Failed cleaning build dir for %s', req.name)
  602. return False
  603. def build(self, requirements, session, autobuilding=False):
  604. """Build wheels.
  605. :param unpack: If True, replace the sdist we built from with the
  606. newly built wheel, in preparation for installation.
  607. :return: True if all the wheels built correctly.
  608. """
  609. from pip._internal import index
  610. from pip._internal.models.link import Link
  611. building_is_possible = self._wheel_dir or (
  612. autobuilding and self.wheel_cache.cache_dir
  613. )
  614. assert building_is_possible
  615. buildset = []
  616. format_control = self.finder.format_control
  617. for req in requirements:
  618. if req.constraint:
  619. continue
  620. if req.is_wheel:
  621. if not autobuilding:
  622. logger.info(
  623. 'Skipping %s, due to already being wheel.', req.name,
  624. )
  625. elif autobuilding and req.editable:
  626. pass
  627. elif autobuilding and not req.source_dir:
  628. pass
  629. elif autobuilding and req.link and not req.link.is_artifact:
  630. # VCS checkout. Build wheel just for this run.
  631. buildset.append((req, True))
  632. else:
  633. ephem_cache = False
  634. if autobuilding:
  635. link = req.link
  636. base, ext = link.splitext()
  637. if index.egg_info_matches(base, None, link) is None:
  638. # E.g. local directory. Build wheel just for this run.
  639. ephem_cache = True
  640. if "binary" not in format_control.get_allowed_formats(
  641. canonicalize_name(req.name)):
  642. logger.info(
  643. "Skipping bdist_wheel for %s, due to binaries "
  644. "being disabled for it.", req.name,
  645. )
  646. continue
  647. buildset.append((req, ephem_cache))
  648. if not buildset:
  649. return True
  650. # Build the wheels.
  651. logger.info(
  652. 'Building wheels for collected packages: %s',
  653. ', '.join([req.name for (req, _) in buildset]),
  654. )
  655. _cache = self.wheel_cache # shorter name
  656. with indent_log():
  657. build_success, build_failure = [], []
  658. for req, ephem in buildset:
  659. python_tag = None
  660. if autobuilding:
  661. python_tag = pep425tags.implementation_tag
  662. if ephem:
  663. output_dir = _cache.get_ephem_path_for_link(req.link)
  664. else:
  665. output_dir = _cache.get_path_for_link(req.link)
  666. try:
  667. ensure_dir(output_dir)
  668. except OSError as e:
  669. logger.warning("Building wheel for %s failed: %s",
  670. req.name, e)
  671. build_failure.append(req)
  672. continue
  673. else:
  674. output_dir = self._wheel_dir
  675. wheel_file = self._build_one(
  676. req, output_dir,
  677. python_tag=python_tag,
  678. )
  679. if wheel_file:
  680. build_success.append(req)
  681. if autobuilding:
  682. # XXX: This is mildly duplicative with prepare_files,
  683. # but not close enough to pull out to a single common
  684. # method.
  685. # The code below assumes temporary source dirs -
  686. # prevent it doing bad things.
  687. if req.source_dir and not os.path.exists(os.path.join(
  688. req.source_dir, PIP_DELETE_MARKER_FILENAME)):
  689. raise AssertionError(
  690. "bad source dir - missing marker")
  691. # Delete the source we built the wheel from
  692. req.remove_temporary_source()
  693. # set the build directory again - name is known from
  694. # the work prepare_files did.
  695. req.source_dir = req.build_location(
  696. self.preparer.build_dir
  697. )
  698. # Update the link for this.
  699. req.link = Link(path_to_url(wheel_file))
  700. assert req.link.is_wheel
  701. # extract the wheel into the dir
  702. unpack_url(
  703. req.link, req.source_dir, None, False,
  704. session=session,
  705. )
  706. else:
  707. build_failure.append(req)
  708. # notify success/failure
  709. if build_success:
  710. logger.info(
  711. 'Successfully built %s',
  712. ' '.join([req.name for req in build_success]),
  713. )
  714. if build_failure:
  715. logger.info(
  716. 'Failed to build %s',
  717. ' '.join([req.name for req in build_failure]),
  718. )
  719. # Return True if all builds were successful
  720. return len(build_failure) == 0