wheel.py 20 KB

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