PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/pip/wheel.py

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