wheel.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. """
  2. Support for installing and building the "wheel" binary package format.
  3. """
  4. from __future__ import absolute_import
  5. import compileall
  6. import csv
  7. import functools
  8. import hashlib
  9. import logging
  10. import os
  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.six import StringIO
  19. from pip.exceptions import InvalidWheelFilename, UnsupportedWheel
  20. from pip.locations import distutils_scheme
  21. from pip import pep425tags
  22. from pip.utils import (call_subprocess, normalize_path, make_path_relative,
  23. captured_stdout)
  24. from pip.utils.logging import indent_log
  25. from pip._vendor.distlib.scripts import ScriptMaker
  26. from pip._vendor import pkg_resources
  27. from pip._vendor.six.moves import configparser
  28. wheel_ext = '.whl'
  29. VERSION_COMPATIBLE = (1, 0)
  30. logger = logging.getLogger(__name__)
  31. def rehash(path, algo='sha256', blocksize=1 << 20):
  32. """Return (hash, length) for path using hashlib.new(algo)"""
  33. h = hashlib.new(algo)
  34. length = 0
  35. with open(path, 'rb') as f:
  36. block = f.read(blocksize)
  37. while block:
  38. length += len(block)
  39. h.update(block)
  40. block = f.read(blocksize)
  41. digest = 'sha256=' + urlsafe_b64encode(
  42. h.digest()
  43. ).decode('latin1').rstrip('=')
  44. return (digest, length)
  45. def open_for_csv(name, mode):
  46. if sys.version_info[0] < 3:
  47. nl = {}
  48. bin = 'b'
  49. else:
  50. nl = {'newline': ''}
  51. bin = ''
  52. return open(name, mode + bin, **nl)
  53. def fix_script(path):
  54. """Replace #!python with #!/path/to/python
  55. Return True if file was changed."""
  56. # XXX RECORD hashes will need to be updated
  57. if os.path.isfile(path):
  58. with open(path, 'rb') as script:
  59. firstline = script.readline()
  60. if not firstline.startswith(b'#!python'):
  61. return False
  62. exename = sys.executable.encode(sys.getfilesystemencoding())
  63. firstline = b'#!' + exename + os.linesep.encode("ascii")
  64. rest = script.read()
  65. with open(path, 'wb') as script:
  66. script.write(firstline)
  67. script.write(rest)
  68. return True
  69. dist_info_re = re.compile(r"""^(?P<namever>(?P<name>.+?)(-(?P<ver>\d.+?))?)
  70. \.dist-info$""", re.VERBOSE)
  71. def root_is_purelib(name, wheeldir):
  72. """
  73. Return True if the extracted wheel in wheeldir should go into purelib.
  74. """
  75. name_folded = name.replace("-", "_")
  76. for item in os.listdir(wheeldir):
  77. match = dist_info_re.match(item)
  78. if match and match.group('name') == name_folded:
  79. with open(os.path.join(wheeldir, item, 'WHEEL')) as wheel:
  80. for line in wheel:
  81. line = line.lower().rstrip()
  82. if line == "root-is-purelib: true":
  83. return True
  84. return False
  85. def get_entrypoints(filename):
  86. if not os.path.exists(filename):
  87. return {}, {}
  88. # This is done because you can pass a string to entry_points wrappers which
  89. # means that they may or may not be valid INI files. The attempt here is to
  90. # strip leading and trailing whitespace in order to make them valid INI
  91. # files.
  92. with open(filename) as fp:
  93. data = StringIO()
  94. for line in fp:
  95. data.write(line.strip())
  96. data.write("\n")
  97. data.seek(0)
  98. cp = configparser.RawConfigParser()
  99. cp.readfp(data)
  100. console = {}
  101. gui = {}
  102. if cp.has_section('console_scripts'):
  103. console = dict(cp.items('console_scripts'))
  104. if cp.has_section('gui_scripts'):
  105. gui = dict(cp.items('gui_scripts'))
  106. return console, gui
  107. def move_wheel_files(name, req, wheeldir, user=False, home=None, root=None,
  108. pycompile=True, scheme=None, isolated=False):
  109. """Install a wheel"""
  110. if not scheme:
  111. scheme = distutils_scheme(
  112. name, user=user, home=home, root=root, isolated=isolated
  113. )
  114. if root_is_purelib(name, wheeldir):
  115. lib_dir = scheme['purelib']
  116. else:
  117. lib_dir = scheme['platlib']
  118. info_dir = []
  119. data_dirs = []
  120. source = wheeldir.rstrip(os.path.sep) + os.path.sep
  121. # Record details of the files moved
  122. # installed = files copied from the wheel to the destination
  123. # changed = files changed while installing (scripts #! line typically)
  124. # generated = files newly generated during the install (script wrappers)
  125. installed = {}
  126. changed = set()
  127. generated = []
  128. # Compile all of the pyc files that we're going to be installing
  129. if pycompile:
  130. with captured_stdout() as stdout:
  131. with warnings.catch_warnings():
  132. warnings.filterwarnings('ignore')
  133. compileall.compile_dir(source, force=True, quiet=True)
  134. logger.debug(stdout.getvalue())
  135. def normpath(src, p):
  136. return make_path_relative(src, p).replace(os.path.sep, '/')
  137. def record_installed(srcfile, destfile, modified=False):
  138. """Map archive RECORD paths to installation RECORD paths."""
  139. oldpath = normpath(srcfile, wheeldir)
  140. newpath = normpath(destfile, lib_dir)
  141. installed[oldpath] = newpath
  142. if modified:
  143. changed.add(destfile)
  144. def clobber(source, dest, is_base, fixer=None, filter=None):
  145. if not os.path.exists(dest): # common for the 'include' path
  146. os.makedirs(dest)
  147. for dir, subdirs, files in os.walk(source):
  148. basedir = dir[len(source):].lstrip(os.path.sep)
  149. destdir = os.path.join(dest, basedir)
  150. if is_base and basedir.split(os.path.sep, 1)[0].endswith('.data'):
  151. continue
  152. for s in subdirs:
  153. destsubdir = os.path.join(dest, basedir, s)
  154. if is_base and basedir == '' and destsubdir.endswith('.data'):
  155. data_dirs.append(s)
  156. continue
  157. elif (is_base and
  158. s.endswith('.dist-info') and
  159. # is self.req.project_name case preserving?
  160. s.lower().startswith(
  161. req.project_name.replace('-', '_').lower())):
  162. assert not info_dir, 'Multiple .dist-info directories'
  163. info_dir.append(destsubdir)
  164. for f in files:
  165. # Skip unwanted files
  166. if filter and filter(f):
  167. continue
  168. srcfile = os.path.join(dir, f)
  169. destfile = os.path.join(dest, basedir, f)
  170. # directory creation is lazy and after the file filtering above
  171. # to ensure we don't install empty dirs; empty dirs can't be
  172. # uninstalled.
  173. if not os.path.exists(destdir):
  174. os.makedirs(destdir)
  175. # We use copyfile (not move, copy, or copy2) to be extra sure
  176. # that we are not moving directories over (copyfile fails for
  177. # directories) as well as to ensure that we are not copying
  178. # over any metadata because we want more control over what
  179. # metadata we actually copy over.
  180. shutil.copyfile(srcfile, destfile)
  181. # Copy over the metadata for the file, currently this only
  182. # includes the atime and mtime.
  183. st = os.stat(srcfile)
  184. if hasattr(os, "utime"):
  185. os.utime(destfile, (st.st_atime, st.st_mtime))
  186. # If our file is executable, then make our destination file
  187. # executable.
  188. if os.access(srcfile, os.X_OK):
  189. st = os.stat(srcfile)
  190. permissions = (
  191. st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
  192. )
  193. os.chmod(destfile, permissions)
  194. changed = False
  195. if fixer:
  196. changed = fixer(destfile)
  197. record_installed(srcfile, destfile, changed)
  198. clobber(source, lib_dir, True)
  199. assert info_dir, "%s .dist-info directory not found" % req
  200. # Get the defined entry points
  201. ep_file = os.path.join(info_dir[0], 'entry_points.txt')
  202. console, gui = get_entrypoints(ep_file)
  203. def is_entrypoint_wrapper(name):
  204. # EP, EP.exe and EP-script.py are scripts generated for
  205. # entry point EP by setuptools
  206. if name.lower().endswith('.exe'):
  207. matchname = name[:-4]
  208. elif name.lower().endswith('-script.py'):
  209. matchname = name[:-10]
  210. elif name.lower().endswith(".pya"):
  211. matchname = name[:-4]
  212. else:
  213. matchname = name
  214. # Ignore setuptools-generated scripts
  215. return (matchname in console or matchname in gui)
  216. for datadir in data_dirs:
  217. fixer = None
  218. filter = None
  219. for subdir in os.listdir(os.path.join(wheeldir, datadir)):
  220. fixer = None
  221. if subdir == 'scripts':
  222. fixer = fix_script
  223. filter = is_entrypoint_wrapper
  224. source = os.path.join(wheeldir, datadir, subdir)
  225. dest = scheme[subdir]
  226. clobber(source, dest, False, fixer=fixer, filter=filter)
  227. maker = ScriptMaker(None, scheme['scripts'])
  228. # Ensure old scripts are overwritten.
  229. # See https://github.com/pypa/pip/issues/1800
  230. maker.clobber = True
  231. # Ensure we don't generate any variants for scripts because this is almost
  232. # never what somebody wants.
  233. # See https://bitbucket.org/pypa/distlib/issue/35/
  234. maker.variants = set(('', ))
  235. # This is required because otherwise distlib creates scripts that are not
  236. # executable.
  237. # See https://bitbucket.org/pypa/distlib/issue/32/
  238. maker.set_mode = True
  239. # Simplify the script and fix the fact that the default script swallows
  240. # every single stack trace.
  241. # See https://bitbucket.org/pypa/distlib/issue/34/
  242. # See https://bitbucket.org/pypa/distlib/issue/33/
  243. def _get_script_text(entry):
  244. return maker.script_template % {
  245. "module": entry.prefix,
  246. "import_name": entry.suffix.split(".")[0],
  247. "func": entry.suffix,
  248. }
  249. maker._get_script_text = _get_script_text
  250. maker.script_template = """# -*- coding: utf-8 -*-
  251. import re
  252. import sys
  253. from %(module)s import %(import_name)s
  254. if __name__ == '__main__':
  255. sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
  256. sys.exit(%(func)s())
  257. """
  258. # Special case pip and setuptools to generate versioned wrappers
  259. #
  260. # The issue is that some projects (specifically, pip and setuptools) use
  261. # code in setup.py to create "versioned" entry points - pip2.7 on Python
  262. # 2.7, pip3.3 on Python 3.3, etc. But these entry points are baked into
  263. # the wheel metadata at build time, and so if the wheel is installed with
  264. # a *different* version of Python the entry points will be wrong. The
  265. # correct fix for this is to enhance the metadata to be able to describe
  266. # such versioned entry points, but that won't happen till Metadata 2.0 is
  267. # available.
  268. # In the meantime, projects using versioned entry points will either have
  269. # incorrect versioned entry points, or they will not be able to distribute
  270. # "universal" wheels (i.e., they will need a wheel per Python version).
  271. #
  272. # Because setuptools and pip are bundled with _ensurepip and virtualenv,
  273. # we need to use universal wheels. So, as a stopgap until Metadata 2.0, we
  274. # override the versioned entry points in the wheel and generate the
  275. # correct ones. This code is purely a short-term measure until Metadat 2.0
  276. # is available.
  277. #
  278. # To add the level of hack in this section of code, in order to support
  279. # ensurepip this code will look for an ``ENSUREPIP_OPTIONS`` environment
  280. # variable which will control which version scripts get installed.
  281. #
  282. # ENSUREPIP_OPTIONS=altinstall
  283. # - Only pipX.Y and easy_install-X.Y will be generated and installed
  284. # ENSUREPIP_OPTIONS=install
  285. # - pipX.Y, pipX, easy_install-X.Y will be generated and installed. Note
  286. # that this option is technically if ENSUREPIP_OPTIONS is set and is
  287. # not altinstall
  288. # DEFAULT
  289. # - The default behavior is to install pip, pipX, pipX.Y, easy_install
  290. # and easy_install-X.Y.
  291. pip_script = console.pop('pip', None)
  292. if pip_script:
  293. if "ENSUREPIP_OPTIONS" not in os.environ:
  294. spec = 'pip = ' + pip_script
  295. generated.extend(maker.make(spec))
  296. if os.environ.get("ENSUREPIP_OPTIONS", "") != "altinstall":
  297. spec = 'pip%s = %s' % (sys.version[:1], pip_script)
  298. generated.extend(maker.make(spec))
  299. spec = 'pip%s = %s' % (sys.version[:3], pip_script)
  300. generated.extend(maker.make(spec))
  301. # Delete any other versioned pip entry points
  302. pip_ep = [k for k in console if re.match(r'pip(\d(\.\d)?)?$', k)]
  303. for k in pip_ep:
  304. del console[k]
  305. easy_install_script = console.pop('easy_install', None)
  306. if easy_install_script:
  307. if "ENSUREPIP_OPTIONS" not in os.environ:
  308. spec = 'easy_install = ' + easy_install_script
  309. generated.extend(maker.make(spec))
  310. spec = 'easy_install-%s = %s' % (sys.version[:3], easy_install_script)
  311. generated.extend(maker.make(spec))
  312. # Delete any other versioned easy_install entry points
  313. easy_install_ep = [
  314. k for k in console if re.match(r'easy_install(-\d\.\d)?$', k)
  315. ]
  316. for k in easy_install_ep:
  317. del console[k]
  318. # Generate the console and GUI entry points specified in the wheel
  319. if len(console) > 0:
  320. generated.extend(
  321. maker.make_multiple(['%s = %s' % kv for kv in console.items()])
  322. )
  323. if len(gui) > 0:
  324. generated.extend(
  325. maker.make_multiple(
  326. ['%s = %s' % kv for kv in gui.items()],
  327. {'gui': True}
  328. )
  329. )
  330. record = os.path.join(info_dir[0], 'RECORD')
  331. temp_record = os.path.join(info_dir[0], 'RECORD.pip')
  332. with open_for_csv(record, 'r') as record_in:
  333. with open_for_csv(temp_record, 'w+') as record_out:
  334. reader = csv.reader(record_in)
  335. writer = csv.writer(record_out)
  336. for row in reader:
  337. row[0] = installed.pop(row[0], row[0])
  338. if row[0] in changed:
  339. row[1], row[2] = rehash(row[0])
  340. writer.writerow(row)
  341. for f in generated:
  342. h, l = rehash(f)
  343. writer.writerow((f, h, l))
  344. for f in installed:
  345. writer.writerow((installed[f], '', ''))
  346. shutil.move(temp_record, record)
  347. def _unique(fn):
  348. @functools.wraps(fn)
  349. def unique(*args, **kw):
  350. seen = set()
  351. for item in fn(*args, **kw):
  352. if item not in seen:
  353. seen.add(item)
  354. yield item
  355. return unique
  356. # TODO: this goes somewhere besides the wheel module
  357. @_unique
  358. def uninstallation_paths(dist):
  359. """
  360. Yield all the uninstallation paths for dist based on RECORD-without-.pyc
  361. Yield paths to all the files in RECORD. For each .py file in RECORD, add
  362. the .pyc in the same directory.
  363. UninstallPathSet.add() takes care of the __pycache__ .pyc.
  364. """
  365. from pip.utils import FakeFile # circular import
  366. r = csv.reader(FakeFile(dist.get_metadata_lines('RECORD')))
  367. for row in r:
  368. path = os.path.join(dist.location, row[0])
  369. yield path
  370. if path.endswith('.py'):
  371. dn, fn = os.path.split(path)
  372. base = fn[:-3]
  373. path = os.path.join(dn, base + '.pyc')
  374. yield path
  375. def wheel_version(source_dir):
  376. """
  377. Return the Wheel-Version of an extracted wheel, if possible.
  378. Otherwise, return False if we couldn't parse / extract it.
  379. """
  380. try:
  381. dist = [d for d in pkg_resources.find_on_path(None, source_dir)][0]
  382. wheel_data = dist.get_metadata('WHEEL')
  383. wheel_data = Parser().parsestr(wheel_data)
  384. version = wheel_data['Wheel-Version'].strip()
  385. version = tuple(map(int, version.split('.')))
  386. return version
  387. except:
  388. return False
  389. def check_compatibility(version, name):
  390. """
  391. Raises errors or warns if called with an incompatible Wheel-Version.
  392. Pip should refuse to install a Wheel-Version that's a major series
  393. ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
  394. installing a version only minor version ahead (e.g 1.2 > 1.1).
  395. version: a 2-tuple representing a Wheel-Version (Major, Minor)
  396. name: name of wheel or package to raise exception about
  397. :raises UnsupportedWheel: when an incompatible Wheel-Version is given
  398. """
  399. if not version:
  400. raise UnsupportedWheel(
  401. "%s is in an unsupported or invalid wheel" % name
  402. )
  403. if version[0] > VERSION_COMPATIBLE[0]:
  404. raise UnsupportedWheel(
  405. "%s's Wheel-Version (%s) is not compatible with this version "
  406. "of pip" % (name, '.'.join(map(str, version)))
  407. )
  408. elif version > VERSION_COMPATIBLE:
  409. logger.warning(
  410. 'Installing from a newer Wheel-Version (%s)',
  411. '.'.join(map(str, version)),
  412. )
  413. class Wheel(object):
  414. """A wheel file"""
  415. # TODO: maybe move the install code into this class
  416. wheel_file_re = re.compile(
  417. r"""^(?P<namever>(?P<name>.+?)-(?P<ver>\d.*?))
  418. ((-(?P<build>\d.*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
  419. \.whl|\.dist-info)$""",
  420. re.VERBOSE
  421. )
  422. def __init__(self, filename):
  423. """
  424. :raises InvalidWheelFilename: when the filename is invalid for a wheel
  425. """
  426. wheel_info = self.wheel_file_re.match(filename)
  427. if not wheel_info:
  428. raise InvalidWheelFilename(
  429. "%s is not a valid wheel filename." % filename
  430. )
  431. self.filename = filename
  432. self.name = wheel_info.group('name').replace('_', '-')
  433. # we'll assume "_" means "-" due to wheel naming scheme
  434. # (https://github.com/pypa/pip/issues/1150)
  435. self.version = wheel_info.group('ver').replace('_', '-')
  436. self.pyversions = wheel_info.group('pyver').split('.')
  437. self.abis = wheel_info.group('abi').split('.')
  438. self.plats = wheel_info.group('plat').split('.')
  439. # All the tag combinations from this file
  440. self.file_tags = set(
  441. (x, y, z) for x in self.pyversions
  442. for y in self.abis for z in self.plats
  443. )
  444. def support_index_min(self, tags=None):
  445. """
  446. Return the lowest index that one of the wheel's file_tag combinations
  447. achieves in the supported_tags list e.g. if there are 8 supported tags,
  448. and one of the file tags is first in the list, then return 0. Returns
  449. None is the wheel is not supported.
  450. """
  451. if tags is None: # for mock
  452. tags = pep425tags.supported_tags
  453. indexes = [tags.index(c) for c in self.file_tags if c in tags]
  454. return min(indexes) if indexes else None
  455. def supported(self, tags=None):
  456. """Is this wheel supported on this system?"""
  457. if tags is None: # for mock
  458. tags = pep425tags.supported_tags
  459. return bool(set(tags).intersection(self.file_tags))
  460. class WheelBuilder(object):
  461. """Build wheels from a RequirementSet."""
  462. def __init__(self, requirement_set, finder, wheel_dir, build_options=None,
  463. global_options=None):
  464. self.requirement_set = requirement_set
  465. self.finder = finder
  466. self.wheel_dir = normalize_path(wheel_dir)
  467. self.build_options = build_options or []
  468. self.global_options = global_options or []
  469. def _build_one(self, req):
  470. """Build one wheel."""
  471. base_args = [
  472. sys.executable, '-c',
  473. "import setuptools;__file__=%r;"
  474. "exec(compile(open(__file__).read().replace('\\r\\n', '\\n'), "
  475. "__file__, 'exec'))" % req.setup_py
  476. ] + list(self.global_options)
  477. logger.info('Running setup.py bdist_wheel for %s', req.name)
  478. logger.info('Destination directory: %s', self.wheel_dir)
  479. wheel_args = base_args + ['bdist_wheel', '-d', self.wheel_dir] \
  480. + self.build_options
  481. try:
  482. call_subprocess(wheel_args, cwd=req.source_dir, show_stdout=False)
  483. return True
  484. except:
  485. logger.error('Failed building wheel for %s', req.name)
  486. return False
  487. def build(self):
  488. """Build wheels."""
  489. # unpack and constructs req set
  490. self.requirement_set.prepare_files(self.finder)
  491. reqset = self.requirement_set.requirements.values()
  492. buildset = []
  493. for req in reqset:
  494. if req.is_wheel:
  495. logger.info(
  496. 'Skipping %s, due to already being wheel.', req.name,
  497. )
  498. elif req.editable:
  499. logger.info(
  500. 'Skipping %s, due to being editable', req.name,
  501. )
  502. else:
  503. buildset.append(req)
  504. if not buildset:
  505. return True
  506. # Build the wheels.
  507. logger.info(
  508. 'Building wheels for collected packages: %s',
  509. ', '.join([req.name for req in buildset]),
  510. )
  511. with indent_log():
  512. build_success, build_failure = [], []
  513. for req in buildset:
  514. if self._build_one(req):
  515. build_success.append(req)
  516. else:
  517. build_failure.append(req)
  518. # notify success/failure
  519. if build_success:
  520. logger.info(
  521. 'Successfully built %s',
  522. ' '.join([req.name for req in build_success]),
  523. )
  524. if build_failure:
  525. logger.info(
  526. 'Failed to build %s',
  527. ' '.join([req.name for req in build_failure]),
  528. )
  529. # Return True if all builds were successful
  530. return len(build_failure) == 0