2012-10-02 07:50:24 +02:00
|
|
|
"""
|
2013-04-02 07:44:46 +02:00
|
|
|
Support for installing and building the "wheel" binary package format.
|
2012-10-02 07:50:24 +02:00
|
|
|
"""
|
2014-08-31 01:52:28 +02:00
|
|
|
from __future__ import absolute_import
|
2012-10-02 07:50:24 +02:00
|
|
|
|
2013-11-22 03:59:31 +01:00
|
|
|
import compileall
|
2012-10-02 07:50:24 +02:00
|
|
|
import csv
|
2015-03-30 23:44:02 +02:00
|
|
|
import errno
|
2012-10-02 07:50:24 +02:00
|
|
|
import functools
|
|
|
|
import hashlib
|
2014-08-31 01:52:28 +02:00
|
|
|
import logging
|
2013-04-05 23:21:11 +02:00
|
|
|
import os
|
2013-04-02 07:44:46 +02:00
|
|
|
import re
|
2013-04-05 23:21:11 +02:00
|
|
|
import shutil
|
2014-12-22 19:47:37 +01:00
|
|
|
import stat
|
2013-04-05 23:21:11 +02:00
|
|
|
import sys
|
2015-04-01 03:39:45 +02:00
|
|
|
import tempfile
|
2015-02-28 11:10:42 +01:00
|
|
|
import warnings
|
2014-01-07 10:47:04 +01:00
|
|
|
|
2013-04-05 23:21:11 +02:00
|
|
|
from base64 import urlsafe_b64encode
|
2014-02-17 00:01:32 +01:00
|
|
|
from email.parser import Parser
|
2013-04-05 23:21:11 +02:00
|
|
|
|
2014-09-11 21:25:23 +02:00
|
|
|
from pip._vendor.six import StringIO
|
|
|
|
|
2015-03-30 23:44:02 +02:00
|
|
|
import pip
|
2015-04-01 03:39:45 +02:00
|
|
|
from pip.download import path_to_url, unpack_url
|
2014-02-18 05:16:54 +01:00
|
|
|
from pip.exceptions import InvalidWheelFilename, UnsupportedWheel
|
2015-04-01 03:39:45 +02:00
|
|
|
from pip.locations import distutils_scheme, PIP_DELETE_MARKER_FILENAME
|
2013-07-01 05:22:55 +02:00
|
|
|
from pip import pep425tags
|
2015-03-31 03:40:30 +02:00
|
|
|
from pip.utils import (
|
2015-04-01 03:39:45 +02:00
|
|
|
call_subprocess, ensure_dir, make_path_relative, captured_stdout,
|
|
|
|
rmtree)
|
2014-08-31 01:52:28 +02:00
|
|
|
from pip.utils.logging import indent_log
|
2013-10-23 16:57:12 +02:00
|
|
|
from pip._vendor.distlib.scripts import ScriptMaker
|
2014-02-18 05:16:54 +01:00
|
|
|
from pip._vendor import pkg_resources
|
2014-06-06 20:21:51 +02:00
|
|
|
from pip._vendor.six.moves import configparser
|
2014-02-17 00:01:32 +01:00
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
|
2013-04-02 07:44:46 +02:00
|
|
|
wheel_ext = '.whl'
|
2012-10-02 07:50:24 +02:00
|
|
|
|
2014-02-15 07:21:22 +01:00
|
|
|
VERSION_COMPATIBLE = (1, 0)
|
|
|
|
|
2013-04-02 07:44:46 +02:00
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-04-20 06:43:02 +02:00
|
|
|
class WheelCache(object):
|
|
|
|
"""A cache of wheels for future installs."""
|
|
|
|
|
|
|
|
def __init__(self, cache_dir):
|
|
|
|
"""Create a wheel cache.
|
|
|
|
|
|
|
|
:param cache_dir: The root of the cache.
|
|
|
|
"""
|
|
|
|
self._cache_dir = cache_dir
|
|
|
|
|
|
|
|
def cached_wheel(self, link):
|
|
|
|
return cached_wheel(self._cache_dir, link)
|
|
|
|
|
|
|
|
|
2015-03-30 23:44:02 +02:00
|
|
|
def _cache_for_filename(cache_dir, sdistfilename):
|
|
|
|
"""Return a directory to store cached wheels in for sdistfilename.
|
|
|
|
|
|
|
|
Because there are M wheels for any one sdist, we provide a directory
|
|
|
|
to cache them in, and then consult that directory when looking up
|
|
|
|
cache hits.
|
|
|
|
|
2015-04-01 03:39:45 +02:00
|
|
|
We only insert things into the cache if they have plausible version
|
|
|
|
numbers, so that we don't contaminate the cache with things that were not
|
|
|
|
unique. E.g. ./package might have dozens of installs done for it and build
|
|
|
|
a version of 0.0...and if we built and cached a wheel, we'd end up using
|
|
|
|
the same wheel even if the source has been edited.
|
|
|
|
|
2015-03-30 23:44:02 +02:00
|
|
|
:param cache_dir: The cache_dir being used by pip.
|
|
|
|
:param sdistfilename: The filename of the sdist for which this will cache
|
|
|
|
wheels.
|
|
|
|
"""
|
|
|
|
return os.path.join(cache_dir, 'wheels', sdistfilename)
|
|
|
|
|
|
|
|
|
|
|
|
def cached_wheel(cache_dir, link):
|
|
|
|
if not cache_dir:
|
|
|
|
return link
|
|
|
|
if not link:
|
|
|
|
return link
|
|
|
|
if link.is_wheel:
|
|
|
|
return link
|
|
|
|
root = _cache_for_filename(cache_dir, link.filename)
|
|
|
|
try:
|
|
|
|
wheel_names = os.listdir(root)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
return link
|
|
|
|
raise
|
|
|
|
candidates = []
|
|
|
|
for wheel_name in wheel_names:
|
|
|
|
try:
|
|
|
|
wheel = Wheel(wheel_name)
|
|
|
|
except InvalidWheelFilename:
|
|
|
|
continue
|
|
|
|
if not wheel.supported():
|
|
|
|
# Built for a different python/arch/etc
|
|
|
|
continue
|
|
|
|
candidates.append((wheel.support_index_min(), wheel_name))
|
|
|
|
if not candidates:
|
|
|
|
return link
|
|
|
|
candidates.sort()
|
|
|
|
path = os.path.join(root, candidates[0][1])
|
|
|
|
return pip.index.Link(path_to_url(path), trusted=True)
|
|
|
|
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
def rehash(path, algo='sha256', blocksize=1 << 20):
|
2012-10-02 07:50:24 +02:00
|
|
|
"""Return (hash, length) for path using hashlib.new(algo)"""
|
|
|
|
h = hashlib.new(algo)
|
|
|
|
length = 0
|
2013-10-23 16:57:12 +02:00
|
|
|
with open(path, 'rb') as f:
|
2012-10-02 07:50:24 +02:00
|
|
|
block = f.read(blocksize)
|
|
|
|
while block:
|
|
|
|
length += len(block)
|
|
|
|
h.update(block)
|
|
|
|
block = f.read(blocksize)
|
2014-01-27 15:07:10 +01:00
|
|
|
digest = 'sha256=' + urlsafe_b64encode(
|
|
|
|
h.digest()
|
|
|
|
).decode('latin1').rstrip('=')
|
2012-10-02 07:50:24 +02:00
|
|
|
return (digest, length)
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
def open_for_csv(name, mode):
|
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
nl = {}
|
|
|
|
bin = 'b'
|
|
|
|
else:
|
2014-01-27 15:07:10 +01:00
|
|
|
nl = {'newline': ''}
|
2012-10-02 07:50:24 +02:00
|
|
|
bin = ''
|
|
|
|
return open(name, mode + bin, **nl)
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
def fix_script(path):
|
|
|
|
"""Replace #!python with #!/path/to/python
|
|
|
|
Return True if file was changed."""
|
|
|
|
# XXX RECORD hashes will need to be updated
|
|
|
|
if os.path.isfile(path):
|
2014-10-02 23:45:37 +02:00
|
|
|
with open(path, 'rb') as script:
|
2012-10-02 07:50:24 +02:00
|
|
|
firstline = script.readline()
|
2014-06-27 04:44:42 +02:00
|
|
|
if not firstline.startswith(b'#!python'):
|
2012-10-02 07:50:24 +02:00
|
|
|
return False
|
|
|
|
exename = sys.executable.encode(sys.getfilesystemencoding())
|
2014-06-27 04:44:42 +02:00
|
|
|
firstline = b'#!' + exename + os.linesep.encode("ascii")
|
2012-10-02 07:50:24 +02:00
|
|
|
rest = script.read()
|
2014-10-02 23:45:37 +02:00
|
|
|
with open(path, 'wb') as script:
|
2012-10-02 07:50:24 +02:00
|
|
|
script.write(firstline)
|
|
|
|
script.write(rest)
|
|
|
|
return True
|
2013-07-06 07:20:09 +02:00
|
|
|
|
2013-06-30 19:58:54 +02:00
|
|
|
dist_info_re = re.compile(r"""^(?P<namever>(?P<name>.+?)(-(?P<ver>\d.+?))?)
|
|
|
|
\.dist-info$""", re.VERBOSE)
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2013-06-30 19:58:54 +02:00
|
|
|
def root_is_purelib(name, wheeldir):
|
|
|
|
"""
|
|
|
|
Return True if the extracted wheel in wheeldir should go into purelib.
|
|
|
|
"""
|
|
|
|
name_folded = name.replace("-", "_")
|
|
|
|
for item in os.listdir(wheeldir):
|
|
|
|
match = dist_info_re.match(item)
|
|
|
|
if match and match.group('name') == name_folded:
|
|
|
|
with open(os.path.join(wheeldir, item, 'WHEEL')) as wheel:
|
|
|
|
for line in wheel:
|
|
|
|
line = line.lower().rstrip()
|
|
|
|
if line == "root-is-purelib: true":
|
|
|
|
return True
|
|
|
|
return False
|
2012-10-02 07:50:24 +02:00
|
|
|
|
2013-11-08 12:32:35 +01:00
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
def get_entrypoints(filename):
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
return {}, {}
|
2013-11-08 12:32:35 +01:00
|
|
|
|
2013-11-19 14:23:15 +01:00
|
|
|
# This is done because you can pass a string to entry_points wrappers which
|
|
|
|
# means that they may or may not be valid INI files. The attempt here is to
|
|
|
|
# strip leading and trailing whitespace in order to make them valid INI
|
|
|
|
# files.
|
2013-11-08 12:32:35 +01:00
|
|
|
with open(filename) as fp:
|
|
|
|
data = StringIO()
|
|
|
|
for line in fp:
|
|
|
|
data.write(line.strip())
|
|
|
|
data.write("\n")
|
|
|
|
data.seek(0)
|
|
|
|
|
2014-06-06 20:21:51 +02:00
|
|
|
cp = configparser.RawConfigParser()
|
2013-11-08 12:32:35 +01:00
|
|
|
cp.readfp(data)
|
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
console = {}
|
|
|
|
gui = {}
|
|
|
|
if cp.has_section('console_scripts'):
|
|
|
|
console = dict(cp.items('console_scripts'))
|
|
|
|
if cp.has_section('gui_scripts'):
|
|
|
|
gui = dict(cp.items('gui_scripts'))
|
|
|
|
return console, gui
|
|
|
|
|
2013-11-08 12:32:35 +01:00
|
|
|
|
2013-11-22 03:59:31 +01:00
|
|
|
def move_wheel_files(name, req, wheeldir, user=False, home=None, root=None,
|
2014-12-11 15:44:53 +01:00
|
|
|
pycompile=True, scheme=None, isolated=False):
|
2012-11-15 01:53:22 +01:00
|
|
|
"""Install a wheel"""
|
2012-10-02 07:50:24 +02:00
|
|
|
|
2014-04-22 08:07:25 +02:00
|
|
|
if not scheme:
|
2014-12-11 15:44:53 +01:00
|
|
|
scheme = distutils_scheme(
|
|
|
|
name, user=user, home=home, root=root, isolated=isolated
|
|
|
|
)
|
2013-07-06 07:20:09 +02:00
|
|
|
|
2013-06-30 19:58:54 +02:00
|
|
|
if root_is_purelib(name, wheeldir):
|
|
|
|
lib_dir = scheme['purelib']
|
|
|
|
else:
|
|
|
|
lib_dir = scheme['platlib']
|
2012-10-02 07:50:24 +02:00
|
|
|
|
|
|
|
info_dir = []
|
|
|
|
data_dirs = []
|
|
|
|
source = wheeldir.rstrip(os.path.sep) + os.path.sep
|
2013-11-01 18:28:35 +01:00
|
|
|
|
|
|
|
# Record details of the files moved
|
|
|
|
# installed = files copied from the wheel to the destination
|
|
|
|
# changed = files changed while installing (scripts #! line typically)
|
|
|
|
# generated = files newly generated during the install (script wrappers)
|
2012-10-02 07:50:24 +02:00
|
|
|
installed = {}
|
|
|
|
changed = set()
|
2013-10-23 16:57:12 +02:00
|
|
|
generated = []
|
2012-10-02 07:50:24 +02:00
|
|
|
|
2013-11-22 03:59:31 +01:00
|
|
|
# Compile all of the pyc files that we're going to be installing
|
|
|
|
if pycompile:
|
2014-08-21 15:40:02 +02:00
|
|
|
with captured_stdout() as stdout:
|
2015-02-28 11:10:42 +01:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings('ignore')
|
|
|
|
compileall.compile_dir(source, force=True, quiet=True)
|
2015-02-28 00:17:21 +01:00
|
|
|
logger.debug(stdout.getvalue())
|
2013-11-22 03:59:31 +01:00
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
def normpath(src, p):
|
|
|
|
return make_path_relative(src, p).replace(os.path.sep, '/')
|
|
|
|
|
|
|
|
def record_installed(srcfile, destfile, modified=False):
|
|
|
|
"""Map archive RECORD paths to installation RECORD paths."""
|
|
|
|
oldpath = normpath(srcfile, wheeldir)
|
2013-06-30 19:58:54 +02:00
|
|
|
newpath = normpath(destfile, lib_dir)
|
2012-10-02 07:50:24 +02:00
|
|
|
installed[oldpath] = newpath
|
|
|
|
if modified:
|
|
|
|
changed.add(destfile)
|
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
def clobber(source, dest, is_base, fixer=None, filter=None):
|
2015-03-31 03:40:30 +02:00
|
|
|
ensure_dir(dest) # common for the 'include' path
|
2013-05-24 04:42:03 +02:00
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
for dir, subdirs, files in os.walk(source):
|
|
|
|
basedir = dir[len(source):].lstrip(os.path.sep)
|
2014-04-22 08:07:25 +02:00
|
|
|
destdir = os.path.join(dest, basedir)
|
2012-10-02 07:50:24 +02:00
|
|
|
if is_base and basedir.split(os.path.sep, 1)[0].endswith('.data'):
|
|
|
|
continue
|
|
|
|
for s in subdirs:
|
|
|
|
destsubdir = os.path.join(dest, basedir, s)
|
|
|
|
if is_base and basedir == '' and destsubdir.endswith('.data'):
|
|
|
|
data_dirs.append(s)
|
|
|
|
continue
|
2015-02-24 13:46:10 +01:00
|
|
|
elif (is_base and
|
|
|
|
s.endswith('.dist-info') and
|
2014-01-27 15:07:10 +01:00
|
|
|
# is self.req.project_name case preserving?
|
2015-02-24 13:46:10 +01:00
|
|
|
s.lower().startswith(
|
2014-01-27 15:07:10 +01:00
|
|
|
req.project_name.replace('-', '_').lower())):
|
2012-10-02 07:50:24 +02:00
|
|
|
assert not info_dir, 'Multiple .dist-info directories'
|
|
|
|
info_dir.append(destsubdir)
|
|
|
|
for f in files:
|
2013-10-23 16:57:12 +02:00
|
|
|
# Skip unwanted files
|
|
|
|
if filter and filter(f):
|
|
|
|
continue
|
2012-10-02 07:50:24 +02:00
|
|
|
srcfile = os.path.join(dir, f)
|
|
|
|
destfile = os.path.join(dest, basedir, f)
|
2014-04-23 01:09:04 +02:00
|
|
|
# directory creation is lazy and after the file filtering above
|
2014-04-23 22:08:07 +02:00
|
|
|
# to ensure we don't install empty dirs; empty dirs can't be
|
|
|
|
# uninstalled.
|
2015-03-31 03:40:30 +02:00
|
|
|
ensure_dir(destdir)
|
2014-11-20 17:17:42 +01:00
|
|
|
|
|
|
|
# We use copyfile (not move, copy, or copy2) to be extra sure
|
|
|
|
# that we are not moving directories over (copyfile fails for
|
|
|
|
# directories) as well as to ensure that we are not copying
|
|
|
|
# over any metadata because we want more control over what
|
|
|
|
# metadata we actually copy over.
|
|
|
|
shutil.copyfile(srcfile, destfile)
|
|
|
|
|
|
|
|
# Copy over the metadata for the file, currently this only
|
|
|
|
# includes the atime and mtime.
|
|
|
|
st = os.stat(srcfile)
|
|
|
|
if hasattr(os, "utime"):
|
|
|
|
os.utime(destfile, (st.st_atime, st.st_mtime))
|
|
|
|
|
2014-12-22 19:47:37 +01:00
|
|
|
# If our file is executable, then make our destination file
|
|
|
|
# executable.
|
|
|
|
if os.access(srcfile, os.X_OK):
|
|
|
|
st = os.stat(srcfile)
|
|
|
|
permissions = (
|
|
|
|
st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
|
|
|
|
)
|
|
|
|
os.chmod(destfile, permissions)
|
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
changed = False
|
|
|
|
if fixer:
|
|
|
|
changed = fixer(destfile)
|
|
|
|
record_installed(srcfile, destfile, changed)
|
|
|
|
|
2013-06-30 19:58:54 +02:00
|
|
|
clobber(source, lib_dir, True)
|
2012-10-02 07:50:24 +02:00
|
|
|
|
|
|
|
assert info_dir, "%s .dist-info directory not found" % req
|
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
# Get the defined entry points
|
|
|
|
ep_file = os.path.join(info_dir[0], 'entry_points.txt')
|
|
|
|
console, gui = get_entrypoints(ep_file)
|
|
|
|
|
|
|
|
def is_entrypoint_wrapper(name):
|
|
|
|
# EP, EP.exe and EP-script.py are scripts generated for
|
|
|
|
# entry point EP by setuptools
|
|
|
|
if name.lower().endswith('.exe'):
|
|
|
|
matchname = name[:-4]
|
|
|
|
elif name.lower().endswith('-script.py'):
|
|
|
|
matchname = name[:-10]
|
2013-11-02 18:04:24 +01:00
|
|
|
elif name.lower().endswith(".pya"):
|
|
|
|
matchname = name[:-4]
|
2013-10-23 16:57:12 +02:00
|
|
|
else:
|
|
|
|
matchname = name
|
|
|
|
# Ignore setuptools-generated scripts
|
|
|
|
return (matchname in console or matchname in gui)
|
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
for datadir in data_dirs:
|
|
|
|
fixer = None
|
2013-10-23 16:57:12 +02:00
|
|
|
filter = None
|
2012-10-02 07:50:24 +02:00
|
|
|
for subdir in os.listdir(os.path.join(wheeldir, datadir)):
|
|
|
|
fixer = None
|
|
|
|
if subdir == 'scripts':
|
|
|
|
fixer = fix_script
|
2013-10-23 16:57:12 +02:00
|
|
|
filter = is_entrypoint_wrapper
|
2012-10-02 07:50:24 +02:00
|
|
|
source = os.path.join(wheeldir, datadir, subdir)
|
2012-11-15 01:53:22 +01:00
|
|
|
dest = scheme[subdir]
|
2013-10-23 16:57:12 +02:00
|
|
|
clobber(source, dest, False, fixer=fixer, filter=filter)
|
|
|
|
|
|
|
|
maker = ScriptMaker(None, scheme['scripts'])
|
2014-06-07 05:20:42 +02:00
|
|
|
|
2014-06-07 05:07:43 +02:00
|
|
|
# Ensure old scripts are overwritten.
|
|
|
|
# See https://github.com/pypa/pip/issues/1800
|
|
|
|
maker.clobber = True
|
2013-11-03 02:00:02 +01:00
|
|
|
|
|
|
|
# Ensure we don't generate any variants for scripts because this is almost
|
|
|
|
# never what somebody wants.
|
|
|
|
# See https://bitbucket.org/pypa/distlib/issue/35/
|
2013-10-23 16:57:12 +02:00
|
|
|
maker.variants = set(('', ))
|
|
|
|
|
2013-11-02 17:03:29 +01:00
|
|
|
# This is required because otherwise distlib creates scripts that are not
|
|
|
|
# executable.
|
|
|
|
# See https://bitbucket.org/pypa/distlib/issue/32/
|
|
|
|
maker.set_mode = True
|
|
|
|
|
2013-11-03 02:00:02 +01:00
|
|
|
# Simplify the script and fix the fact that the default script swallows
|
|
|
|
# every single stack trace.
|
|
|
|
# See https://bitbucket.org/pypa/distlib/issue/34/
|
|
|
|
# See https://bitbucket.org/pypa/distlib/issue/33/
|
|
|
|
def _get_script_text(entry):
|
|
|
|
return maker.script_template % {
|
|
|
|
"module": entry.prefix,
|
|
|
|
"import_name": entry.suffix.split(".")[0],
|
|
|
|
"func": entry.suffix,
|
|
|
|
}
|
|
|
|
|
|
|
|
maker._get_script_text = _get_script_text
|
|
|
|
maker.script_template = """# -*- coding: utf-8 -*-
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from %(module)s import %(import_name)s
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
|
|
|
sys.exit(%(func)s())
|
|
|
|
"""
|
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
# Special case pip and setuptools to generate versioned wrappers
|
2013-11-01 18:28:35 +01:00
|
|
|
#
|
|
|
|
# The issue is that some projects (specifically, pip and setuptools) use
|
|
|
|
# code in setup.py to create "versioned" entry points - pip2.7 on Python
|
|
|
|
# 2.7, pip3.3 on Python 3.3, etc. But these entry points are baked into
|
|
|
|
# the wheel metadata at build time, and so if the wheel is installed with
|
|
|
|
# a *different* version of Python the entry points will be wrong. The
|
|
|
|
# correct fix for this is to enhance the metadata to be able to describe
|
|
|
|
# such versioned entry points, but that won't happen till Metadata 2.0 is
|
|
|
|
# available.
|
|
|
|
# In the meantime, projects using versioned entry points will either have
|
|
|
|
# incorrect versioned entry points, or they will not be able to distribute
|
|
|
|
# "universal" wheels (i.e., they will need a wheel per Python version).
|
|
|
|
#
|
|
|
|
# Because setuptools and pip are bundled with _ensurepip and virtualenv,
|
|
|
|
# we need to use universal wheels. So, as a stopgap until Metadata 2.0, we
|
|
|
|
# override the versioned entry points in the wheel and generate the
|
|
|
|
# correct ones. This code is purely a short-term measure until Metadat 2.0
|
|
|
|
# is available.
|
2013-11-07 18:28:10 +01:00
|
|
|
#
|
|
|
|
# To add the level of hack in this section of code, in order to support
|
|
|
|
# ensurepip this code will look for an ``ENSUREPIP_OPTIONS`` environment
|
|
|
|
# variable which will control which version scripts get installed.
|
|
|
|
#
|
|
|
|
# ENSUREPIP_OPTIONS=altinstall
|
|
|
|
# - Only pipX.Y and easy_install-X.Y will be generated and installed
|
|
|
|
# ENSUREPIP_OPTIONS=install
|
|
|
|
# - pipX.Y, pipX, easy_install-X.Y will be generated and installed. Note
|
|
|
|
# that this option is technically if ENSUREPIP_OPTIONS is set and is
|
|
|
|
# not altinstall
|
|
|
|
# DEFAULT
|
|
|
|
# - The default behavior is to install pip, pipX, pipX.Y, easy_install
|
|
|
|
# and easy_install-X.Y.
|
2013-10-23 16:57:12 +02:00
|
|
|
pip_script = console.pop('pip', None)
|
|
|
|
if pip_script:
|
2013-11-05 01:12:59 +01:00
|
|
|
if "ENSUREPIP_OPTIONS" not in os.environ:
|
|
|
|
spec = 'pip = ' + pip_script
|
|
|
|
generated.extend(maker.make(spec))
|
|
|
|
|
|
|
|
if os.environ.get("ENSUREPIP_OPTIONS", "") != "altinstall":
|
|
|
|
spec = 'pip%s = %s' % (sys.version[:1], pip_script)
|
|
|
|
generated.extend(maker.make(spec))
|
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
spec = 'pip%s = %s' % (sys.version[:3], pip_script)
|
|
|
|
generated.extend(maker.make(spec))
|
|
|
|
# Delete any other versioned pip entry points
|
|
|
|
pip_ep = [k for k in console if re.match(r'pip(\d(\.\d)?)?$', k)]
|
|
|
|
for k in pip_ep:
|
|
|
|
del console[k]
|
|
|
|
easy_install_script = console.pop('easy_install', None)
|
|
|
|
if easy_install_script:
|
2013-11-05 01:12:59 +01:00
|
|
|
if "ENSUREPIP_OPTIONS" not in os.environ:
|
|
|
|
spec = 'easy_install = ' + easy_install_script
|
|
|
|
generated.extend(maker.make(spec))
|
|
|
|
|
2013-10-23 16:57:12 +02:00
|
|
|
spec = 'easy_install-%s = %s' % (sys.version[:3], easy_install_script)
|
|
|
|
generated.extend(maker.make(spec))
|
|
|
|
# Delete any other versioned easy_install entry points
|
2014-01-27 15:07:10 +01:00
|
|
|
easy_install_ep = [
|
|
|
|
k for k in console if re.match(r'easy_install(-\d\.\d)?$', k)
|
|
|
|
]
|
2013-10-23 16:57:12 +02:00
|
|
|
for k in easy_install_ep:
|
|
|
|
del console[k]
|
|
|
|
|
2013-11-01 18:28:35 +01:00
|
|
|
# Generate the console and GUI entry points specified in the wheel
|
2013-10-23 16:57:12 +02:00
|
|
|
if len(console) > 0:
|
2014-01-27 15:07:10 +01:00
|
|
|
generated.extend(
|
|
|
|
maker.make_multiple(['%s = %s' % kv for kv in console.items()])
|
|
|
|
)
|
2013-10-23 16:57:12 +02:00
|
|
|
if len(gui) > 0:
|
2014-01-27 15:07:10 +01:00
|
|
|
generated.extend(
|
|
|
|
maker.make_multiple(
|
|
|
|
['%s = %s' % kv for kv in gui.items()],
|
|
|
|
{'gui': True}
|
|
|
|
)
|
|
|
|
)
|
2012-10-02 07:50:24 +02:00
|
|
|
|
|
|
|
record = os.path.join(info_dir[0], 'RECORD')
|
|
|
|
temp_record = os.path.join(info_dir[0], 'RECORD.pip')
|
|
|
|
with open_for_csv(record, 'r') as record_in:
|
|
|
|
with open_for_csv(temp_record, 'w+') as record_out:
|
|
|
|
reader = csv.reader(record_in)
|
|
|
|
writer = csv.writer(record_out)
|
|
|
|
for row in reader:
|
|
|
|
row[0] = installed.pop(row[0], row[0])
|
|
|
|
if row[0] in changed:
|
|
|
|
row[1], row[2] = rehash(row[0])
|
|
|
|
writer.writerow(row)
|
2013-10-23 16:57:12 +02:00
|
|
|
for f in generated:
|
|
|
|
h, l = rehash(f)
|
|
|
|
writer.writerow((f, h, l))
|
2012-10-02 07:50:24 +02:00
|
|
|
for f in installed:
|
|
|
|
writer.writerow((installed[f], '', ''))
|
|
|
|
shutil.move(temp_record, record)
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2012-10-02 07:50:24 +02:00
|
|
|
def _unique(fn):
|
|
|
|
@functools.wraps(fn)
|
|
|
|
def unique(*args, **kw):
|
|
|
|
seen = set()
|
|
|
|
for item in fn(*args, **kw):
|
|
|
|
if item not in seen:
|
|
|
|
seen.add(item)
|
|
|
|
yield item
|
|
|
|
return unique
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2013-04-02 07:44:46 +02:00
|
|
|
# TODO: this goes somewhere besides the wheel module
|
2012-10-02 07:50:24 +02:00
|
|
|
@_unique
|
|
|
|
def uninstallation_paths(dist):
|
|
|
|
"""
|
|
|
|
Yield all the uninstallation paths for dist based on RECORD-without-.pyc
|
|
|
|
|
|
|
|
Yield paths to all the files in RECORD. For each .py file in RECORD, add
|
|
|
|
the .pyc in the same directory.
|
|
|
|
|
|
|
|
UninstallPathSet.add() takes care of the __pycache__ .pyc.
|
|
|
|
"""
|
2014-08-31 01:52:28 +02:00
|
|
|
from pip.utils import FakeFile # circular import
|
2012-10-02 07:50:24 +02:00
|
|
|
r = csv.reader(FakeFile(dist.get_metadata_lines('RECORD')))
|
|
|
|
for row in r:
|
|
|
|
path = os.path.join(dist.location, row[0])
|
|
|
|
yield path
|
|
|
|
if path.endswith('.py'):
|
|
|
|
dn, fn = os.path.split(path)
|
|
|
|
base = fn[:-3]
|
2014-02-24 22:52:23 +01:00
|
|
|
path = os.path.join(dn, base + '.pyc')
|
2012-10-02 07:50:24 +02:00
|
|
|
yield path
|
2012-10-17 00:57:10 +02:00
|
|
|
|
|
|
|
|
2014-02-15 07:21:22 +01:00
|
|
|
def wheel_version(source_dir):
|
|
|
|
"""
|
|
|
|
Return the Wheel-Version of an extracted wheel, if possible.
|
|
|
|
|
|
|
|
Otherwise, return False if we couldn't parse / extract it.
|
|
|
|
"""
|
|
|
|
try:
|
2014-02-18 05:16:54 +01:00
|
|
|
dist = [d for d in pkg_resources.find_on_path(None, source_dir)][0]
|
2014-02-17 00:01:32 +01:00
|
|
|
|
2014-02-15 07:21:22 +01:00
|
|
|
wheel_data = dist.get_metadata('WHEEL')
|
2014-02-17 00:01:32 +01:00
|
|
|
wheel_data = Parser().parsestr(wheel_data)
|
2014-02-18 05:16:54 +01:00
|
|
|
|
2014-02-17 00:01:32 +01:00
|
|
|
version = wheel_data['Wheel-Version'].strip()
|
2014-02-15 07:21:22 +01:00
|
|
|
version = tuple(map(int, version.split('.')))
|
|
|
|
return version
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-02-18 05:16:54 +01:00
|
|
|
def check_compatibility(version, name):
|
|
|
|
"""
|
2014-02-19 08:44:44 +01:00
|
|
|
Raises errors or warns if called with an incompatible Wheel-Version.
|
2014-02-18 05:16:54 +01:00
|
|
|
|
2014-02-19 08:44:44 +01:00
|
|
|
Pip should refuse to install a Wheel-Version that's a major series
|
|
|
|
ahead of what it's compatible with (e.g 2.0 > 1.1); and warn when
|
|
|
|
installing a version only minor version ahead (e.g 1.2 > 1.1).
|
|
|
|
|
|
|
|
version: a 2-tuple representing a Wheel-Version (Major, Minor)
|
2014-02-18 05:16:54 +01:00
|
|
|
name: name of wheel or package to raise exception about
|
2014-02-19 08:44:44 +01:00
|
|
|
|
|
|
|
:raises UnsupportedWheel: when an incompatible Wheel-Version is given
|
2014-02-18 05:16:54 +01:00
|
|
|
"""
|
|
|
|
if not version:
|
|
|
|
raise UnsupportedWheel(
|
|
|
|
"%s is in an unsupported or invalid wheel" % name
|
|
|
|
)
|
|
|
|
if version[0] > VERSION_COMPATIBLE[0]:
|
|
|
|
raise UnsupportedWheel(
|
|
|
|
"%s's Wheel-Version (%s) is not compatible with this version "
|
|
|
|
"of pip" % (name, '.'.join(map(str, version)))
|
|
|
|
)
|
|
|
|
elif version > VERSION_COMPATIBLE:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.warning(
|
|
|
|
'Installing from a newer Wheel-Version (%s)',
|
|
|
|
'.'.join(map(str, version)),
|
|
|
|
)
|
2014-02-18 05:16:54 +01:00
|
|
|
|
|
|
|
|
2013-04-02 07:44:46 +02:00
|
|
|
class Wheel(object):
|
|
|
|
"""A wheel file"""
|
|
|
|
|
|
|
|
# TODO: maybe move the install code into this class
|
|
|
|
|
|
|
|
wheel_file_re = re.compile(
|
2014-01-27 15:07:10 +01:00
|
|
|
r"""^(?P<namever>(?P<name>.+?)-(?P<ver>\d.*?))
|
|
|
|
((-(?P<build>\d.*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)
|
|
|
|
\.whl|\.dist-info)$""",
|
|
|
|
re.VERBOSE
|
|
|
|
)
|
2013-04-02 07:44:46 +02:00
|
|
|
|
|
|
|
def __init__(self, filename):
|
2013-11-15 01:35:24 +01:00
|
|
|
"""
|
|
|
|
:raises InvalidWheelFilename: when the filename is invalid for a wheel
|
|
|
|
"""
|
2013-04-02 07:44:46 +02:00
|
|
|
wheel_info = self.wheel_file_re.match(filename)
|
2013-11-15 01:35:24 +01:00
|
|
|
if not wheel_info:
|
2014-01-27 15:07:10 +01:00
|
|
|
raise InvalidWheelFilename(
|
|
|
|
"%s is not a valid wheel filename." % filename
|
|
|
|
)
|
2013-04-02 07:44:46 +02:00
|
|
|
self.filename = filename
|
|
|
|
self.name = wheel_info.group('name').replace('_', '-')
|
2013-08-22 08:30:15 +02:00
|
|
|
# we'll assume "_" means "-" due to wheel naming scheme
|
|
|
|
# (https://github.com/pypa/pip/issues/1150)
|
|
|
|
self.version = wheel_info.group('ver').replace('_', '-')
|
2013-04-02 07:44:46 +02:00
|
|
|
self.pyversions = wheel_info.group('pyver').split('.')
|
|
|
|
self.abis = wheel_info.group('abi').split('.')
|
|
|
|
self.plats = wheel_info.group('plat').split('.')
|
|
|
|
|
|
|
|
# All the tag combinations from this file
|
2014-01-27 15:07:10 +01:00
|
|
|
self.file_tags = set(
|
|
|
|
(x, y, z) for x in self.pyversions
|
|
|
|
for y in self.abis for z in self.plats
|
|
|
|
)
|
2013-04-02 07:44:46 +02:00
|
|
|
|
2013-07-01 05:22:55 +02:00
|
|
|
def support_index_min(self, tags=None):
|
2013-04-02 07:44:46 +02:00
|
|
|
"""
|
2013-11-15 01:35:24 +01:00
|
|
|
Return the lowest index that one of the wheel's file_tag combinations
|
|
|
|
achieves in the supported_tags list e.g. if there are 8 supported tags,
|
|
|
|
and one of the file tags is first in the list, then return 0. Returns
|
|
|
|
None is the wheel is not supported.
|
2013-04-02 07:44:46 +02:00
|
|
|
"""
|
2014-01-27 15:07:10 +01:00
|
|
|
if tags is None: # for mock
|
2013-07-01 05:22:55 +02:00
|
|
|
tags = pep425tags.supported_tags
|
2013-06-30 20:56:43 +02:00
|
|
|
indexes = [tags.index(c) for c in self.file_tags if c in tags]
|
2013-04-02 07:44:46 +02:00
|
|
|
return min(indexes) if indexes else None
|
|
|
|
|
2013-07-01 05:22:55 +02:00
|
|
|
def supported(self, tags=None):
|
2013-04-02 07:44:46 +02:00
|
|
|
"""Is this wheel supported on this system?"""
|
2014-01-27 15:07:10 +01:00
|
|
|
if tags is None: # for mock
|
2013-07-01 05:22:55 +02:00
|
|
|
tags = pep425tags.supported_tags
|
2013-06-30 20:15:03 +02:00
|
|
|
return bool(set(tags).intersection(self.file_tags))
|
2013-04-02 07:44:46 +02:00
|
|
|
|
2012-10-17 00:57:10 +02:00
|
|
|
|
|
|
|
class WheelBuilder(object):
|
|
|
|
"""Build wheels from a RequirementSet."""
|
|
|
|
|
2015-03-27 03:13:03 +01:00
|
|
|
def __init__(self, requirement_set, finder, build_options=None,
|
2015-04-20 06:43:02 +02:00
|
|
|
global_options=None):
|
2012-10-17 00:57:10 +02:00
|
|
|
self.requirement_set = requirement_set
|
|
|
|
self.finder = finder
|
2015-04-20 06:43:02 +02:00
|
|
|
self._cache_root = requirement_set._wheel_cache._cache_dir
|
2015-04-01 03:39:45 +02:00
|
|
|
self._wheel_dir = requirement_set.wheel_download_dir
|
2014-12-10 17:56:51 +01:00
|
|
|
self.build_options = build_options or []
|
|
|
|
self.global_options = global_options or []
|
2012-10-17 00:57:10 +02:00
|
|
|
|
2015-04-01 03:39:45 +02:00
|
|
|
def _build_one(self, req, output_dir):
|
|
|
|
"""Build one wheel.
|
2012-10-17 00:57:10 +02:00
|
|
|
|
2015-04-01 03:39:45 +02:00
|
|
|
:return: The filename of the built wheel, or None if the build failed.
|
|
|
|
"""
|
|
|
|
tempd = tempfile.mkdtemp('pip-wheel-')
|
|
|
|
try:
|
|
|
|
if self.__build_one(req, tempd):
|
|
|
|
try:
|
|
|
|
wheel_name = os.listdir(tempd)[0]
|
|
|
|
wheel_path = os.path.join(output_dir, wheel_name)
|
|
|
|
os.rename(os.path.join(tempd, wheel_name), wheel_path)
|
|
|
|
logger.info('Stored in directory: %s', output_dir)
|
|
|
|
return wheel_path
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
finally:
|
|
|
|
rmtree(tempd)
|
|
|
|
|
|
|
|
def __build_one(self, req, tempd):
|
2012-10-17 00:57:10 +02:00
|
|
|
base_args = [
|
|
|
|
sys.executable, '-c',
|
2014-01-27 15:07:10 +01:00
|
|
|
"import setuptools;__file__=%r;"
|
|
|
|
"exec(compile(open(__file__).read().replace('\\r\\n', '\\n'), "
|
|
|
|
"__file__, 'exec'))" % req.setup_py
|
|
|
|
] + list(self.global_options)
|
2012-10-17 00:57:10 +02:00
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.info('Running setup.py bdist_wheel for %s', req.name)
|
2015-04-01 03:39:45 +02:00
|
|
|
logger.debug('Destination directory: %s', tempd)
|
|
|
|
wheel_args = base_args + ['bdist_wheel', '-d', tempd] \
|
2014-01-27 15:07:10 +01:00
|
|
|
+ self.build_options
|
2012-10-17 00:57:10 +02:00
|
|
|
try:
|
|
|
|
call_subprocess(wheel_args, cwd=req.source_dir, show_stdout=False)
|
|
|
|
return True
|
|
|
|
except:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.error('Failed building wheel for %s', req.name)
|
2012-10-17 00:57:10 +02:00
|
|
|
return False
|
|
|
|
|
2015-04-01 03:39:45 +02:00
|
|
|
def build(self, autobuilding=False):
|
|
|
|
"""Build wheels.
|
2012-10-17 00:57:10 +02:00
|
|
|
|
2015-04-01 03:39:45 +02:00
|
|
|
:param unpack: If True, replace the sdist we built from the with the
|
|
|
|
newly built wheel, in preparation for installation.
|
|
|
|
:return: True if all the wheels built correctly.
|
|
|
|
"""
|
|
|
|
assert self._wheel_dir or (autobuilding and self._cache_root)
|
|
|
|
# unpack sdists and constructs req set
|
2012-10-17 00:57:10 +02:00
|
|
|
self.requirement_set.prepare_files(self.finder)
|
|
|
|
|
|
|
|
reqset = self.requirement_set.requirements.values()
|
|
|
|
|
2014-05-03 19:02:23 +02:00
|
|
|
buildset = []
|
|
|
|
for req in reqset:
|
|
|
|
if req.is_wheel:
|
2015-04-01 03:39:45 +02:00
|
|
|
if not autobuilding:
|
|
|
|
logger.info(
|
|
|
|
'Skipping %s, due to already being wheel.', req.name)
|
2014-05-03 19:02:23 +02:00
|
|
|
elif req.editable:
|
2015-04-01 03:39:45 +02:00
|
|
|
if not autobuilding:
|
|
|
|
logger.info(
|
|
|
|
'Skipping bdist_wheel for %s, due to being editable',
|
|
|
|
req.name)
|
|
|
|
elif autobuilding and not req.source_dir:
|
|
|
|
pass
|
2014-05-03 19:02:23 +02:00
|
|
|
else:
|
2015-04-01 03:39:45 +02:00
|
|
|
if autobuilding:
|
|
|
|
link = req.link
|
|
|
|
base, ext = link.splitext()
|
|
|
|
if pip.index.egg_info_matches(base, None, link) is None:
|
|
|
|
# Doesn't look like a package - don't autobuild a wheel
|
|
|
|
# because we'll have no way to lookup the result sanely
|
|
|
|
continue
|
2014-05-03 19:02:23 +02:00
|
|
|
buildset.append(req)
|
2014-02-01 20:41:55 +01:00
|
|
|
|
|
|
|
if not buildset:
|
2014-04-28 15:28:31 +02:00
|
|
|
return True
|
2012-10-17 00:57:10 +02:00
|
|
|
|
2014-03-04 07:44:28 +01:00
|
|
|
# Build the wheels.
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.info(
|
|
|
|
'Building wheels for collected packages: %s',
|
|
|
|
', '.join([req.name for req in buildset]),
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2014-08-31 01:52:28 +02:00
|
|
|
with indent_log():
|
|
|
|
build_success, build_failure = [], []
|
|
|
|
for req in buildset:
|
2015-04-01 03:39:45 +02:00
|
|
|
if autobuilding:
|
|
|
|
output_dir = _cache_for_filename(
|
|
|
|
self._cache_root, req.link.filename)
|
|
|
|
ensure_dir(output_dir)
|
|
|
|
else:
|
|
|
|
output_dir = self._wheel_dir
|
|
|
|
wheel_file = self._build_one(req, output_dir)
|
|
|
|
if wheel_file:
|
2014-08-31 01:52:28 +02:00
|
|
|
build_success.append(req)
|
2015-04-01 03:39:45 +02:00
|
|
|
if autobuilding:
|
|
|
|
# XXX: This is mildly duplicative with prepare_files,
|
|
|
|
# but not close enough to pull out to a single common
|
|
|
|
# method.
|
|
|
|
# The code below assumes temporary source dirs -
|
|
|
|
# prevent it doing bad things.
|
|
|
|
if req.source_dir and not os.path.exists(os.path.join(
|
|
|
|
req.source_dir, PIP_DELETE_MARKER_FILENAME)):
|
|
|
|
raise AssertionError(
|
|
|
|
"bad source dir - missing marker")
|
|
|
|
# Delete the source we built the wheel from
|
|
|
|
req.remove_temporary_source()
|
|
|
|
# set the build directory again - name is known from
|
|
|
|
# the work prepare_files did.
|
|
|
|
req.source_dir = req.build_location(
|
|
|
|
self.requirement_set.build_dir)
|
|
|
|
# Update the link for this.
|
|
|
|
req.link = pip.index.Link(
|
|
|
|
path_to_url(wheel_file), trusted=True)
|
|
|
|
assert req.link.is_wheel
|
|
|
|
# extract the wheel into the dir
|
|
|
|
unpack_url(
|
|
|
|
req.link, req.source_dir, None, False,
|
|
|
|
session=self.requirement_set.session)
|
2014-08-31 01:52:28 +02:00
|
|
|
else:
|
|
|
|
build_failure.append(req)
|
2012-10-17 00:57:10 +02:00
|
|
|
|
2014-04-30 07:33:04 +02:00
|
|
|
# notify success/failure
|
2012-10-17 00:57:10 +02:00
|
|
|
if build_success:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.info(
|
|
|
|
'Successfully built %s',
|
|
|
|
' '.join([req.name for req in build_success]),
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2012-10-17 00:57:10 +02:00
|
|
|
if build_failure:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.info(
|
|
|
|
'Failed to build %s',
|
|
|
|
' '.join([req.name for req in build_failure]),
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2014-04-28 15:28:31 +02:00
|
|
|
# Return True if all builds were successful
|
|
|
|
return len(build_failure) == 0
|