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.

wheel.py 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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.models.link import Link
  31. from pip._internal.utils.logging import indent_log
  32. from pip._internal.utils.misc import (
  33. call_subprocess, captured_stdout, ensure_dir, read_chunks,
  34. )
  35. from pip._internal.utils.setuptools_build import SETUPTOOLS_SHIM
  36. from pip._internal.utils.temp_dir import TempDirectory
  37. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  38. from pip._internal.utils.ui import open_spinner
  39. if MYPY_CHECK_RUNNING:
  40. from typing import ( # noqa: F401
  41. Dict, List, Optional, Sequence, Mapping, Tuple, IO, Text, Any,
  42. Union, Iterable
  43. )
  44. from pip._vendor.packaging.requirements import Requirement # noqa: F401
  45. from pip._internal.req.req_install import InstallRequirement # noqa: F401
  46. from pip._internal.download import PipSession # noqa: F401
  47. from pip._internal.index import FormatControl, PackageFinder # noqa: F401
  48. from pip._internal.operations.prepare import ( # noqa: F401
  49. RequirementPreparer
  50. )
  51. from pip._internal.cache import WheelCache # noqa: F401
  52. from pip._internal.pep425tags import Pep425Tag # noqa: F401
  53. InstalledCSVRow = Tuple[str, ...]
  54. VERSION_COMPATIBLE = (1, 0)
  55. logger = logging.getLogger(__name__)
  56. def normpath(src, p):
  57. return os.path.relpath(src, p).replace(os.path.sep, '/')
  58. def rehash(path, blocksize=1 << 20):
  59. # type: (str, int) -> Tuple[str, str]
  60. """Return (hash, length) for path using hashlib.sha256()"""
  61. h = hashlib.sha256()
  62. length = 0
  63. with open(path, 'rb') as f:
  64. for block in read_chunks(f, size=blocksize):
  65. length += len(block)
  66. h.update(block)
  67. digest = 'sha256=' + urlsafe_b64encode(
  68. h.digest()
  69. ).decode('latin1').rstrip('=')
  70. # unicode/str python2 issues
  71. return (digest, str(length)) # type: ignore
  72. def open_for_csv(name, mode):
  73. # type: (str, Text) -> IO
  74. if sys.version_info[0] < 3:
  75. nl = {} # type: Dict[str, Any]
  76. bin = 'b'
  77. else:
  78. nl = {'newline': ''} # type: Dict[str, Any]
  79. bin = ''
  80. return open(name, mode + bin, **nl)
  81. def replace_python_tag(wheelname, new_tag):
  82. # type: (str, str) -> str
  83. """Replace the Python tag in a wheel file name with a new value.
  84. """
  85. parts = wheelname.split('-')
  86. parts[-3] = new_tag
  87. return '-'.join(parts)
  88. def fix_script(path):
  89. # type: (str) -> Optional[bool]
  90. """Replace #!python with #!/path/to/python
  91. Return True if file was changed."""
  92. # XXX RECORD hashes will need to be updated
  93. if os.path.isfile(path):
  94. with open(path, 'rb') as script:
  95. firstline = script.readline()
  96. if not firstline.startswith(b'#!python'):
  97. return False
  98. exename = sys.executable.encode(sys.getfilesystemencoding())
  99. firstline = b'#!' + exename + os.linesep.encode("ascii")
  100. rest = script.read()
  101. with open(path, 'wb') as script:
  102. script.write(firstline)
  103. script.write(rest)
  104. return True
  105. return None
  106. dist_info_re = re.compile(r"""^(?P<namever>(?P<name>.+?)(-(?P<ver>.+?))?)
  107. \.dist-info$""", re.VERBOSE)
  108. def root_is_purelib(name, wheeldir):
  109. # type: (str, str) -> bool
  110. """
  111. Return True if the extracted wheel in wheeldir should go into purelib.
  112. """
  113. name_folded = name.replace("-", "_")
  114. for item in os.listdir(wheeldir):
  115. match = dist_info_re.match(item)
  116. if match and match.group('name') == name_folded:
  117. with open(os.path.join(wheeldir, item, 'WHEEL')) as wheel:
  118. for line in wheel:
  119. line = line.lower().rstrip()
  120. if line == "root-is-purelib: true":
  121. return True
  122. return False
  123. def get_entrypoints(filename):
  124. # type: (str) -> Tuple[Dict[str, str], Dict[str, str]]
  125. if not os.path.exists(filename):
  126. return {}, {}
  127. # This is done because you can pass a string to entry_points wrappers which
  128. # means that they may or may not be valid INI files. The attempt here is to
  129. # strip leading and trailing whitespace in order to make them valid INI
  130. # files.
  131. with open(filename) as fp:
  132. data = StringIO()
  133. for line in fp:
  134. data.write(line.strip())
  135. data.write("\n")
  136. data.seek(0)
  137. # get the entry points and then the script names
  138. entry_points = pkg_resources.EntryPoint.parse_map(data)
  139. console = entry_points.get('console_scripts', {})
  140. gui = entry_points.get('gui_scripts', {})
  141. def _split_ep(s):
  142. """get the string representation of EntryPoint, remove space and split
  143. on '='"""
  144. return str(s).replace(" ", "").split("=")
  145. # convert the EntryPoint objects into strings with module:function
  146. console = dict(_split_ep(v) for v in console.values())
  147. gui = dict(_split_ep(v) for v in gui.values())
  148. return console, gui
  149. def message_about_scripts_not_on_PATH(scripts):
  150. # type: (Sequence[str]) -> Optional[str]
  151. """Determine if any scripts are not on PATH and format a warning.
  152. Returns a warning message if one or more scripts are not on PATH,
  153. otherwise None.
  154. """
  155. if not scripts:
  156. return None
  157. # Group scripts by the path they were installed in
  158. grouped_by_dir = collections.defaultdict(set) # type: Dict[str, set]
  159. for destfile in scripts:
  160. parent_dir = os.path.dirname(destfile)
  161. script_name = os.path.basename(destfile)
  162. grouped_by_dir[parent_dir].add(script_name)
  163. # We don't want to warn for directories that are on PATH.
  164. not_warn_dirs = [
  165. os.path.normcase(i).rstrip(os.sep) for i in
  166. os.environ.get("PATH", "").split(os.pathsep)
  167. ]
  168. # If an executable sits with sys.executable, we don't warn for it.
  169. # This covers the case of venv invocations without activating the venv.
  170. not_warn_dirs.append(os.path.normcase(os.path.dirname(sys.executable)))
  171. warn_for = {
  172. parent_dir: scripts for parent_dir, scripts in grouped_by_dir.items()
  173. if os.path.normcase(parent_dir) not in not_warn_dirs
  174. }
  175. if not warn_for:
  176. return None
  177. # Format a message
  178. msg_lines = []
  179. for parent_dir, scripts in warn_for.items():
  180. scripts = sorted(scripts)
  181. if len(scripts) == 1:
  182. start_text = "script {} is".format(scripts[0])
  183. else:
  184. start_text = "scripts {} are".format(
  185. ", ".join(scripts[:-1]) + " and " + scripts[-1]
  186. )
  187. msg_lines.append(
  188. "The {} installed in '{}' which is not on PATH."
  189. .format(start_text, parent_dir)
  190. )
  191. last_line_fmt = (
  192. "Consider adding {} to PATH or, if you prefer "
  193. "to suppress this warning, use --no-warn-script-location."
  194. )
  195. if len(msg_lines) == 1:
  196. msg_lines.append(last_line_fmt.format("this directory"))
  197. else:
  198. msg_lines.append(last_line_fmt.format("these directories"))
  199. # Returns the formatted multiline message
  200. return "\n".join(msg_lines)
  201. def sorted_outrows(outrows):
  202. # type: (Iterable[InstalledCSVRow]) -> List[InstalledCSVRow]
  203. """
  204. Return the given rows of a RECORD file in sorted order.
  205. Each row is a 3-tuple (path, hash, size) and corresponds to a record of
  206. a RECORD file (see PEP 376 and PEP 427 for details). For the rows
  207. passed to this function, the size can be an integer as an int or string,
  208. or the empty string.
  209. """
  210. # Normally, there should only be one row per path, in which case the
  211. # second and third elements don't come into play when sorting.
  212. # However, in cases in the wild where a path might happen to occur twice,
  213. # we don't want the sort operation to trigger an error (but still want
  214. # determinism). Since the third element can be an int or string, we
  215. # coerce each element to a string to avoid a TypeError in this case.
  216. # For additional background, see--
  217. # https://github.com/pypa/pip/issues/5868
  218. return sorted(outrows, key=lambda row: tuple(str(x) for x in row))
  219. def get_csv_rows_for_installed(
  220. old_csv_rows, # type: Iterable[List[str]]
  221. installed, # type: Dict[str, str]
  222. changed, # type: set
  223. generated, # type: List[str]
  224. lib_dir, # type: str
  225. ):
  226. # type: (...) -> List[InstalledCSVRow]
  227. """
  228. :param installed: A map from archive RECORD path to installation RECORD
  229. path.
  230. """
  231. installed_rows = [] # type: List[InstalledCSVRow]
  232. for row in old_csv_rows:
  233. if len(row) > 3:
  234. logger.warning(
  235. 'RECORD line has more than three elements: {}'.format(row)
  236. )
  237. # Make a copy because we are mutating the row.
  238. row = list(row)
  239. old_path = row[0]
  240. new_path = installed.pop(old_path, old_path)
  241. row[0] = new_path
  242. if new_path in changed:
  243. digest, length = rehash(new_path)
  244. row[1] = digest
  245. row[2] = length
  246. installed_rows.append(tuple(row))
  247. for f in generated:
  248. digest, length = rehash(f)
  249. installed_rows.append((normpath(f, lib_dir), digest, str(length)))
  250. for f in installed:
  251. installed_rows.append((installed[f], '', ''))
  252. return installed_rows
  253. def move_wheel_files(
  254. name, # type: str
  255. req, # type: Requirement
  256. wheeldir, # type: str
  257. user=False, # type: bool
  258. home=None, # type: Optional[str]
  259. root=None, # type: Optional[str]
  260. pycompile=True, # type: bool
  261. scheme=None, # type: Optional[Mapping[str, str]]
  262. isolated=False, # type: bool
  263. prefix=None, # type: Optional[str]
  264. warn_script_location=True # type: bool
  265. ):
  266. # type: (...) -> None
  267. """Install a wheel"""
  268. # TODO: Investigate and break this up.
  269. # TODO: Look into moving this into a dedicated class for representing an
  270. # installation.
  271. if not scheme:
  272. scheme = distutils_scheme(
  273. name, user=user, home=home, root=root, isolated=isolated,
  274. prefix=prefix,
  275. )
  276. if root_is_purelib(name, wheeldir):
  277. lib_dir = scheme['purelib']
  278. else:
  279. lib_dir = scheme['platlib']
  280. info_dir = [] # type: List[str]
  281. data_dirs = []
  282. source = wheeldir.rstrip(os.path.sep) + os.path.sep
  283. # Record details of the files moved
  284. # installed = files copied from the wheel to the destination
  285. # changed = files changed while installing (scripts #! line typically)
  286. # generated = files newly generated during the install (script wrappers)
  287. installed = {} # type: Dict[str, str]
  288. changed = set()
  289. generated = [] # type: List[str]
  290. # Compile all of the pyc files that we're going to be installing
  291. if pycompile:
  292. with captured_stdout() as stdout:
  293. with warnings.catch_warnings():
  294. warnings.filterwarnings('ignore')
  295. compileall.compile_dir(source, force=True, quiet=True)
  296. logger.debug(stdout.getvalue())
  297. def record_installed(srcfile, destfile, modified=False):
  298. """Map archive RECORD paths to installation RECORD paths."""
  299. oldpath = normpath(srcfile, wheeldir)
  300. newpath = normpath(destfile, lib_dir)
  301. installed[oldpath] = newpath
  302. if modified:
  303. changed.add(destfile)
  304. def clobber(source, dest, is_base, fixer=None, filter=None):
  305. ensure_dir(dest) # common for the 'include' path
  306. for dir, subdirs, files in os.walk(source):
  307. basedir = dir[len(source):].lstrip(os.path.sep)
  308. destdir = os.path.join(dest, basedir)
  309. if is_base and basedir.split(os.path.sep, 1)[0].endswith('.data'):
  310. continue
  311. for s in subdirs:
  312. destsubdir = os.path.join(dest, basedir, s)
  313. if is_base and basedir == '' and destsubdir.endswith('.data'):
  314. data_dirs.append(s)
  315. continue
  316. elif (is_base and
  317. s.endswith('.dist-info') and
  318. canonicalize_name(s).startswith(
  319. canonicalize_name(req.name))):
  320. assert not info_dir, ('Multiple .dist-info directories: ' +
  321. destsubdir + ', ' +
  322. ', '.join(info_dir))
  323. info_dir.append(destsubdir)
  324. for f in files:
  325. # Skip unwanted files
  326. if filter and filter(f):
  327. continue
  328. srcfile = os.path.join(dir, f)
  329. destfile = os.path.join(dest, basedir, f)
  330. # directory creation is lazy and after the file filtering above
  331. # to ensure we don't install empty dirs; empty dirs can't be
  332. # uninstalled.
  333. ensure_dir(destdir)
  334. # copyfile (called below) truncates the destination if it
  335. # exists and then writes the new contents. This is fine in most
  336. # cases, but can cause a segfault if pip has loaded a shared
  337. # object (e.g. from pyopenssl through its vendored urllib3)
  338. # Since the shared object is mmap'd an attempt to call a
  339. # symbol in it will then cause a segfault. Unlinking the file
  340. # allows writing of new contents while allowing the process to
  341. # continue to use the old copy.
  342. if os.path.exists(destfile):
  343. os.unlink(destfile)
  344. # We use copyfile (not move, copy, or copy2) to be extra sure
  345. # that we are not moving directories over (copyfile fails for
  346. # directories) as well as to ensure that we are not copying
  347. # over any metadata because we want more control over what
  348. # metadata we actually copy over.
  349. shutil.copyfile(srcfile, destfile)
  350. # Copy over the metadata for the file, currently this only
  351. # includes the atime and mtime.
  352. st = os.stat(srcfile)
  353. if hasattr(os, "utime"):
  354. os.utime(destfile, (st.st_atime, st.st_mtime))
  355. # If our file is executable, then make our destination file
  356. # executable.
  357. if os.access(srcfile, os.X_OK):
  358. st = os.stat(srcfile)
  359. permissions = (
  360. st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
  361. )
  362. os.chmod(destfile, permissions)
  363. changed = False
  364. if fixer:
  365. changed = fixer(destfile)
  366. record_installed(srcfile, destfile, changed)
  367. clobber(source, lib_dir, True)
  368. assert info_dir, "%s .dist-info directory not found" % req
  369. # Get the defined entry points
  370. ep_file = os.path.join(info_dir[0], 'entry_points.txt')
  371. console, gui = get_entrypoints(ep_file)
  372. def is_entrypoint_wrapper(name):
  373. # EP, EP.exe and EP-script.py are scripts generated for
  374. # entry point EP by setuptools
  375. if name.lower().endswith('.exe'):
  376. matchname = name[:-4]
  377. elif name.lower().endswith('-script.py'):
  378. matchname = name[:-10]
  379. elif name.lower().endswith(".pya"):
  380. matchname = name[:-4]
  381. else:
  382. matchname = name
  383. # Ignore setuptools-generated scripts
  384. return (matchname in console or matchname in gui)
  385. for datadir in data_dirs:
  386. fixer = None
  387. filter = None
  388. for subdir in os.listdir(os.path.join(wheeldir, datadir)):
  389. fixer = None
  390. if subdir == 'scripts':
  391. fixer = fix_script
  392. filter = is_entrypoint_wrapper
  393. source = os.path.join(wheeldir, datadir, subdir)
  394. dest = scheme[subdir]
  395. clobber(source, dest, False, fixer=fixer, filter=filter)
  396. maker = ScriptMaker(None, scheme['scripts'])
  397. # Ensure old scripts are overwritten.
  398. # See https://github.com/pypa/pip/issues/1800
  399. maker.clobber = True
  400. # Ensure we don't generate any variants for scripts because this is almost
  401. # never what somebody wants.
  402. # See https://bitbucket.org/pypa/distlib/issue/35/
  403. maker.variants = {''}
  404. # This is required because otherwise distlib creates scripts that are not
  405. # executable.
  406. # See https://bitbucket.org/pypa/distlib/issue/32/
  407. maker.set_mode = True
  408. # Simplify the script and fix the fact that the default script swallows
  409. # every single stack trace.
  410. # See https://bitbucket.org/pypa/distlib/issue/34/
  411. # See https://bitbucket.org/pypa/distlib/issue/33/
  412. def _get_script_text(entry):
  413. if entry.suffix is None:
  414. raise InstallationError(
  415. "Invalid script entry point: %s for req: %s - A callable "
  416. "suffix is required. Cf https://packaging.python.org/en/"
  417. "latest/distributing.html#console-scripts for more "
  418. "information." % (entry, req)
  419. )
  420. return maker.script_template % {
  421. "module": entry.prefix,
  422. "import_name": entry.suffix.split(".")[0],
  423. "func": entry.suffix,
  424. }
  425. # ignore type, because mypy disallows assigning to a method,
  426. # see https://github.com/python/mypy/issues/2427
  427. maker._get_script_text = _get_script_text # type: ignore
  428. maker.script_template = r"""# -*- coding: utf-8 -*-
  429. import re
  430. import sys
  431. from %(module)s import %(import_name)s
  432. if __name__ == '__main__':
  433. sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
  434. sys.exit(%(func)s())
  435. """
  436. # Special case pip and setuptools to generate versioned wrappers
  437. #
  438. # The issue is that some projects (specifically, pip and setuptools) use
  439. # code in setup.py to create "versioned" entry points - pip2.7 on Python
  440. # 2.7, pip3.3 on Python 3.3, etc. But these entry points are baked into
  441. # the wheel metadata at build time, and so if the wheel is installed with
  442. # a *different* version of Python the entry points will be wrong. The
  443. # correct fix for this is to enhance the metadata to be able to describe
  444. # such versioned entry points, but that won't happen till Metadata 2.0 is
  445. # available.
  446. # In the meantime, projects using versioned entry points will either have
  447. # incorrect versioned entry points, or they will not be able to distribute
  448. # "universal" wheels (i.e., they will need a wheel per Python version).
  449. #
  450. # Because setuptools and pip are bundled with _ensurepip and virtualenv,
  451. # we need to use universal wheels. So, as a stopgap until Metadata 2.0, we
  452. # override the versioned entry points in the wheel and generate the
  453. # correct ones. This code is purely a short-term measure until Metadata 2.0
  454. # is available.
  455. #
  456. # To add the level of hack in this section of code, in order to support
  457. # ensurepip this code will look for an ``ENSUREPIP_OPTIONS`` environment
  458. # variable which will control which version scripts get installed.
  459. #
  460. # ENSUREPIP_OPTIONS=altinstall
  461. # - Only pipX.Y and easy_install-X.Y will be generated and installed
  462. # ENSUREPIP_OPTIONS=install
  463. # - pipX.Y, pipX, easy_install-X.Y will be generated and installed. Note
  464. # that this option is technically if ENSUREPIP_OPTIONS is set and is
  465. # not altinstall
  466. # DEFAULT
  467. # - The default behavior is to install pip, pipX, pipX.Y, easy_install
  468. # and easy_install-X.Y.
  469. pip_script = console.pop('pip', None)
  470. if pip_script:
  471. if "ENSUREPIP_OPTIONS" not in os.environ:
  472. spec = 'pip = ' + pip_script
  473. generated.extend(maker.make(spec))
  474. if os.environ.get("ENSUREPIP_OPTIONS", "") != "altinstall":
  475. spec = 'pip%s = %s' % (sys.version[:1], pip_script)
  476. generated.extend(maker.make(spec))
  477. spec = 'pip%s = %s' % (sys.version[:3], pip_script)
  478. generated.extend(maker.make(spec))
  479. # Delete any other versioned pip entry points
  480. pip_ep = [k for k in console if re.match(r'pip(\d(\.\d)?)?$', k)]
  481. for k in pip_ep:
  482. del console[k]
  483. easy_install_script = console.pop('easy_install', None)
  484. if easy_install_script:
  485. if "ENSUREPIP_OPTIONS" not in os.environ:
  486. spec = 'easy_install = ' + easy_install_script
  487. generated.extend(maker.make(spec))
  488. spec = 'easy_install-%s = %s' % (sys.version[:3], easy_install_script)
  489. generated.extend(maker.make(spec))
  490. # Delete any other versioned easy_install entry points
  491. easy_install_ep = [
  492. k for k in console if re.match(r'easy_install(-\d\.\d)?$', k)
  493. ]
  494. for k in easy_install_ep:
  495. del console[k]
  496. # Generate the console and GUI entry points specified in the wheel
  497. if len(console) > 0:
  498. generated_console_scripts = maker.make_multiple(
  499. ['%s = %s' % kv for kv in console.items()]
  500. )
  501. generated.extend(generated_console_scripts)
  502. if warn_script_location:
  503. msg = message_about_scripts_not_on_PATH(generated_console_scripts)
  504. if msg is not None:
  505. logger.warning(msg)
  506. if len(gui) > 0:
  507. generated.extend(
  508. maker.make_multiple(
  509. ['%s = %s' % kv for kv in gui.items()],
  510. {'gui': True}
  511. )
  512. )
  513. # Record pip as the installer
  514. installer = os.path.join(info_dir[0], 'INSTALLER')
  515. temp_installer = os.path.join(info_dir[0], 'INSTALLER.pip')
  516. with open(temp_installer, 'wb') as installer_file:
  517. installer_file.write(b'pip\n')
  518. shutil.move(temp_installer, installer)
  519. generated.append(installer)
  520. # Record details of all files installed
  521. record = os.path.join(info_dir[0], 'RECORD')
  522. temp_record = os.path.join(info_dir[0], 'RECORD.pip')
  523. with open_for_csv(record, 'r') as record_in:
  524. with open_for_csv(temp_record, 'w+') as record_out:
  525. reader = csv.reader(record_in)
  526. outrows = get_csv_rows_for_installed(
  527. reader, installed=installed, changed=changed,
  528. generated=generated, lib_dir=lib_dir,
  529. )
  530. writer = csv.writer(record_out)
  531. # Sort to simplify testing.
  532. for row in sorted_outrows(outrows):
  533. writer.writerow(row)
  534. shutil.move(temp_record, record)
  535. def wheel_version(source_dir):
  536. # type: (Optional[str]) -> Optional[Tuple[int, ...]]
  537. """
  538. Return the Wheel-Version of an extracted wheel, if possible.
  539. Otherwise, return None if we couldn't parse / extract it.
  540. """
  541. try:
  542. dist = [d for d in pkg_resources.find_on_path(None, source_dir)][0]
  543. wheel_data = dist.get_metadata('WHEEL')
  544. wheel_data = Parser().parsestr(wheel_data)
  545. version = wheel_data['Wheel-Version'].strip()
  546. version = tuple(map(int, version.split('.')))
  547. return version
  548. except Exception:
  549. return None
  550. def check_compatibility(version, name):
  551. # type: (Optional[Tuple[int, ...]], str) -> None
  552. """
  553. Raises errors or warns if called with an incompatible Wheel-Version.
  554. Pip should refuse to install a Wheel-Version that's a major series
  555. ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
  556. installing a version only minor version ahead (e.g 1.2 > 1.1).
  557. version: a 2-tuple representing a Wheel-Version (Major, Minor)
  558. name: name of wheel or package to raise exception about
  559. :raises UnsupportedWheel: when an incompatible Wheel-Version is given
  560. """
  561. if not version:
  562. raise UnsupportedWheel(
  563. "%s is in an unsupported or invalid wheel" % name
  564. )
  565. if version[0] > VERSION_COMPATIBLE[0]:
  566. raise UnsupportedWheel(
  567. "%s's Wheel-Version (%s) is not compatible with this version "
  568. "of pip" % (name, '.'.join(map(str, version)))
  569. )
  570. elif version > VERSION_COMPATIBLE:
  571. logger.warning(
  572. 'Installing from a newer Wheel-Version (%s)',
  573. '.'.join(map(str, version)),
  574. )
  575. class Wheel(object):
  576. """A wheel file"""
  577. # TODO: Maybe move the class into the models sub-package
  578. # TODO: Maybe move the install code into this class
  579. wheel_file_re = re.compile(
  580. r"""^(?P<namever>(?P<name>.+?)-(?P<ver>.*?))
  581. ((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
  582. \.whl|\.dist-info)$""",
  583. re.VERBOSE
  584. )
  585. def __init__(self, filename):
  586. # type: (str) -> None
  587. """
  588. :raises InvalidWheelFilename: when the filename is invalid for a wheel
  589. """
  590. wheel_info = self.wheel_file_re.match(filename)
  591. if not wheel_info:
  592. raise InvalidWheelFilename(
  593. "%s is not a valid wheel filename." % filename
  594. )
  595. self.filename = filename
  596. self.name = wheel_info.group('name').replace('_', '-')
  597. # we'll assume "_" means "-" due to wheel naming scheme
  598. # (https://github.com/pypa/pip/issues/1150)
  599. self.version = wheel_info.group('ver').replace('_', '-')
  600. self.build_tag = wheel_info.group('build')
  601. self.pyversions = wheel_info.group('pyver').split('.')
  602. self.abis = wheel_info.group('abi').split('.')
  603. self.plats = wheel_info.group('plat').split('.')
  604. # All the tag combinations from this file
  605. self.file_tags = {
  606. (x, y, z) for x in self.pyversions
  607. for y in self.abis for z in self.plats
  608. }
  609. def support_index_min(self, tags=None):
  610. # type: (Optional[List[Pep425Tag]]) -> Optional[int]
  611. """
  612. Return the lowest index that one of the wheel's file_tag combinations
  613. achieves in the supported_tags list e.g. if there are 8 supported tags,
  614. and one of the file tags is first in the list, then return 0. Returns
  615. None is the wheel is not supported.
  616. """
  617. if tags is None: # for mock
  618. tags = pep425tags.get_supported()
  619. indexes = [tags.index(c) for c in self.file_tags if c in tags]
  620. return min(indexes) if indexes else None
  621. def supported(self, tags=None):
  622. # type: (Optional[List[Pep425Tag]]) -> bool
  623. """Is this wheel supported on this system?"""
  624. if tags is None: # for mock
  625. tags = pep425tags.get_supported()
  626. return bool(set(tags).intersection(self.file_tags))
  627. def _contains_egg_info(
  628. s, _egg_info_re=re.compile(r'([a-z0-9_.]+)-([a-z0-9_.!+-]+)', re.I)):
  629. """Determine whether the string looks like an egg_info.
  630. :param s: The string to parse. E.g. foo-2.1
  631. """
  632. return bool(_egg_info_re.search(s))
  633. def should_use_ephemeral_cache(
  634. req, # type: InstallRequirement
  635. format_control, # type: FormatControl
  636. autobuilding, # type: bool
  637. cache_available # type: bool
  638. ):
  639. # type: (...) -> Optional[bool]
  640. """
  641. Return whether to build an InstallRequirement object using the
  642. ephemeral cache.
  643. :param cache_available: whether a cache directory is available for the
  644. autobuilding=True case.
  645. :return: True or False to build the requirement with ephem_cache=True
  646. or False, respectively; or None not to build the requirement.
  647. """
  648. if req.constraint:
  649. return None
  650. if req.is_wheel:
  651. if not autobuilding:
  652. logger.info(
  653. 'Skipping %s, due to already being wheel.', req.name,
  654. )
  655. return None
  656. if not autobuilding:
  657. return False
  658. if req.editable or not req.source_dir:
  659. return None
  660. if req.link and not req.link.is_artifact:
  661. # VCS checkout. Build wheel just for this run.
  662. return True
  663. if "binary" not in format_control.get_allowed_formats(
  664. canonicalize_name(req.name)):
  665. logger.info(
  666. "Skipping bdist_wheel for %s, due to binaries "
  667. "being disabled for it.", req.name,
  668. )
  669. return None
  670. link = req.link
  671. base, ext = link.splitext()
  672. if cache_available and _contains_egg_info(base):
  673. return False
  674. # Otherwise, build the wheel just for this run using the ephemeral
  675. # cache since we are either in the case of e.g. a local directory, or
  676. # no cache directory is available to use.
  677. return True
  678. def format_command(
  679. command_args, # type: List[str]
  680. command_output, # type: str
  681. ):
  682. # type: (...) -> str
  683. """
  684. Format command information for logging.
  685. """
  686. text = 'Command arguments: {}\n'.format(command_args)
  687. if not command_output:
  688. text += 'Command output: None'
  689. elif logger.getEffectiveLevel() > logging.DEBUG:
  690. text += 'Command output: [use --verbose to show]'
  691. else:
  692. if not command_output.endswith('\n'):
  693. command_output += '\n'
  694. text += (
  695. 'Command output:\n{}'
  696. '-----------------------------------------'
  697. ).format(command_output)
  698. return text
  699. def get_legacy_build_wheel_path(
  700. names, # type: List[str]
  701. temp_dir, # type: str
  702. req, # type: InstallRequirement
  703. command_args, # type: List[str]
  704. command_output, # type: str
  705. ):
  706. # type: (...) -> Optional[str]
  707. """
  708. Return the path to the wheel in the temporary build directory.
  709. """
  710. # Sort for determinism.
  711. names = sorted(names)
  712. if not names:
  713. msg = (
  714. 'Legacy build of wheel for {!r} created no files.\n'
  715. ).format(req.name)
  716. msg += format_command(command_args, command_output)
  717. logger.warning(msg)
  718. return None
  719. if len(names) > 1:
  720. msg = (
  721. 'Legacy build of wheel for {!r} created more than one file.\n'
  722. 'Filenames (choosing first): {}\n'
  723. ).format(req.name, names)
  724. msg += format_command(command_args, command_output)
  725. logger.warning(msg)
  726. return os.path.join(temp_dir, names[0])
  727. class WheelBuilder(object):
  728. """Build wheels from a RequirementSet."""
  729. def __init__(
  730. self,
  731. finder, # type: PackageFinder
  732. preparer, # type: RequirementPreparer
  733. wheel_cache, # type: WheelCache
  734. build_options=None, # type: Optional[List[str]]
  735. global_options=None, # type: Optional[List[str]]
  736. no_clean=False # type: bool
  737. ):
  738. # type: (...) -> None
  739. self.finder = finder
  740. self.preparer = preparer
  741. self.wheel_cache = wheel_cache
  742. self._wheel_dir = preparer.wheel_download_dir
  743. self.build_options = build_options or []
  744. self.global_options = global_options or []
  745. self.no_clean = no_clean
  746. def _build_one(self, req, output_dir, python_tag=None):
  747. """Build one wheel.
  748. :return: The filename of the built wheel, or None if the build failed.
  749. """
  750. # Install build deps into temporary directory (PEP 518)
  751. with req.build_env:
  752. return self._build_one_inside_env(req, output_dir,
  753. python_tag=python_tag)
  754. def _build_one_inside_env(self, req, output_dir, python_tag=None):
  755. with TempDirectory(kind="wheel") as temp_dir:
  756. if req.use_pep517:
  757. builder = self._build_one_pep517
  758. else:
  759. builder = self._build_one_legacy
  760. wheel_path = builder(req, temp_dir.path, python_tag=python_tag)
  761. if wheel_path is not None:
  762. wheel_name = os.path.basename(wheel_path)
  763. dest_path = os.path.join(output_dir, wheel_name)
  764. try:
  765. shutil.move(wheel_path, dest_path)
  766. logger.info('Stored in directory: %s', output_dir)
  767. return dest_path
  768. except Exception:
  769. pass
  770. # Ignore return, we can't do anything else useful.
  771. self._clean_one(req)
  772. return None
  773. def _base_setup_args(self, req):
  774. # NOTE: Eventually, we'd want to also -S to the flags here, when we're
  775. # isolating. Currently, it breaks Python in virtualenvs, because it
  776. # relies on site.py to find parts of the standard library outside the
  777. # virtualenv.
  778. return [
  779. sys.executable, '-u', '-c',
  780. SETUPTOOLS_SHIM % req.setup_py
  781. ] + list(self.global_options)
  782. def _build_one_pep517(self, req, tempd, python_tag=None):
  783. """Build one InstallRequirement using the PEP 517 build process.
  784. Returns path to wheel if successfully built. Otherwise, returns None.
  785. """
  786. assert req.metadata_directory is not None
  787. try:
  788. req.spin_message = 'Building wheel for %s (PEP 517)' % (req.name,)
  789. logger.debug('Destination directory: %s', tempd)
  790. wheel_name = req.pep517_backend.build_wheel(
  791. tempd,
  792. metadata_directory=req.metadata_directory
  793. )
  794. if python_tag:
  795. # General PEP 517 backends don't necessarily support
  796. # a "--python-tag" option, so we rename the wheel
  797. # file directly.
  798. new_name = replace_python_tag(wheel_name, python_tag)
  799. os.rename(
  800. os.path.join(tempd, wheel_name),
  801. os.path.join(tempd, new_name)
  802. )
  803. # Reassign to simplify the return at the end of function
  804. wheel_name = new_name
  805. except Exception:
  806. logger.error('Failed building wheel for %s', req.name)
  807. return None
  808. return os.path.join(tempd, wheel_name)
  809. def _build_one_legacy(self, req, tempd, python_tag=None):
  810. """Build one InstallRequirement using the "legacy" build process.
  811. Returns path to wheel if successfully built. Otherwise, returns None.
  812. """
  813. base_args = self._base_setup_args(req)
  814. spin_message = 'Building wheel for %s (setup.py)' % (req.name,)
  815. with open_spinner(spin_message) as spinner:
  816. logger.debug('Destination directory: %s', tempd)
  817. wheel_args = base_args + ['bdist_wheel', '-d', tempd] \
  818. + self.build_options
  819. if python_tag is not None:
  820. wheel_args += ["--python-tag", python_tag]
  821. try:
  822. output = call_subprocess(wheel_args, cwd=req.setup_py_dir,
  823. show_stdout=False, spinner=spinner)
  824. except Exception:
  825. spinner.finish("error")
  826. logger.error('Failed building wheel for %s', req.name)
  827. return None
  828. names = os.listdir(tempd)
  829. wheel_path = get_legacy_build_wheel_path(
  830. names=names,
  831. temp_dir=tempd,
  832. req=req,
  833. command_args=wheel_args,
  834. command_output=output,
  835. )
  836. return wheel_path
  837. def _clean_one(self, req):
  838. base_args = self._base_setup_args(req)
  839. logger.info('Running setup.py clean for %s', req.name)
  840. clean_args = base_args + ['clean', '--all']
  841. try:
  842. call_subprocess(clean_args, cwd=req.source_dir, show_stdout=False)
  843. return True
  844. except Exception:
  845. logger.error('Failed cleaning build dir for %s', req.name)
  846. return False
  847. def build(
  848. self,
  849. requirements, # type: Iterable[InstallRequirement]
  850. session, # type: PipSession
  851. autobuilding=False # type: bool
  852. ):
  853. # type: (...) -> List[InstallRequirement]
  854. """Build wheels.
  855. :param unpack: If True, replace the sdist we built from with the
  856. newly built wheel, in preparation for installation.
  857. :return: True if all the wheels built correctly.
  858. """
  859. buildset = []
  860. format_control = self.finder.format_control
  861. # Whether a cache directory is available for autobuilding=True.
  862. cache_available = bool(self._wheel_dir or self.wheel_cache.cache_dir)
  863. for req in requirements:
  864. ephem_cache = should_use_ephemeral_cache(
  865. req, format_control=format_control, autobuilding=autobuilding,
  866. cache_available=cache_available,
  867. )
  868. if ephem_cache is None:
  869. continue
  870. buildset.append((req, ephem_cache))
  871. if not buildset:
  872. return []
  873. # Is any wheel build not using the ephemeral cache?
  874. if any(not ephem_cache for _, ephem_cache in buildset):
  875. have_directory_for_build = self._wheel_dir or (
  876. autobuilding and self.wheel_cache.cache_dir
  877. )
  878. assert have_directory_for_build
  879. # TODO by @pradyunsg
  880. # Should break up this method into 2 separate methods.
  881. # Build the wheels.
  882. logger.info(
  883. 'Building wheels for collected packages: %s',
  884. ', '.join([req.name for (req, _) in buildset]),
  885. )
  886. _cache = self.wheel_cache # shorter name
  887. with indent_log():
  888. build_success, build_failure = [], []
  889. for req, ephem in buildset:
  890. python_tag = None
  891. if autobuilding:
  892. python_tag = pep425tags.implementation_tag
  893. if ephem:
  894. output_dir = _cache.get_ephem_path_for_link(req.link)
  895. else:
  896. output_dir = _cache.get_path_for_link(req.link)
  897. try:
  898. ensure_dir(output_dir)
  899. except OSError as e:
  900. logger.warning("Building wheel for %s failed: %s",
  901. req.name, e)
  902. build_failure.append(req)
  903. continue
  904. else:
  905. output_dir = self._wheel_dir
  906. wheel_file = self._build_one(
  907. req, output_dir,
  908. python_tag=python_tag,
  909. )
  910. if wheel_file:
  911. build_success.append(req)
  912. if autobuilding:
  913. # XXX: This is mildly duplicative with prepare_files,
  914. # but not close enough to pull out to a single common
  915. # method.
  916. # The code below assumes temporary source dirs -
  917. # prevent it doing bad things.
  918. if req.source_dir and not os.path.exists(os.path.join(
  919. req.source_dir, PIP_DELETE_MARKER_FILENAME)):
  920. raise AssertionError(
  921. "bad source dir - missing marker")
  922. # Delete the source we built the wheel from
  923. req.remove_temporary_source()
  924. # set the build directory again - name is known from
  925. # the work prepare_files did.
  926. req.source_dir = req.build_location(
  927. self.preparer.build_dir
  928. )
  929. # Update the link for this.
  930. req.link = Link(path_to_url(wheel_file))
  931. assert req.link.is_wheel
  932. # extract the wheel into the dir
  933. unpack_url(
  934. req.link, req.source_dir, None, False,
  935. session=session,
  936. )
  937. else:
  938. build_failure.append(req)
  939. # notify success/failure
  940. if build_success:
  941. logger.info(
  942. 'Successfully built %s',
  943. ' '.join([req.name for req in build_success]),
  944. )
  945. if build_failure:
  946. logger.info(
  947. 'Failed to build %s',
  948. ' '.join([req.name for req in build_failure]),
  949. )
  950. # Return a list of requirements that failed to build
  951. return build_failure