2009-11-20 08:25:32 +01:00
|
|
|
"""Locations where we look for configs, install stuff, etc"""
|
2014-08-31 01:52:28 +02:00
|
|
|
from __future__ import absolute_import
|
2009-11-20 08:25:32 +01:00
|
|
|
|
2014-04-15 22:09:23 +02:00
|
|
|
import getpass
|
2009-11-20 08:25:32 +01:00
|
|
|
import os
|
2014-06-13 04:59:01 +02:00
|
|
|
import os.path
|
2014-04-15 22:09:23 +02:00
|
|
|
import site
|
|
|
|
import sys
|
2012-04-23 03:34:58 +02:00
|
|
|
import tempfile
|
2014-04-15 22:09:23 +02:00
|
|
|
|
|
|
|
from distutils import sysconfig
|
2012-11-15 01:53:22 +01:00
|
|
|
from distutils.command.install import install, SCHEME_KEYS
|
2014-04-15 22:09:23 +02:00
|
|
|
|
2014-06-26 14:15:31 +02:00
|
|
|
from pip.compat import get_path_uid, WINDOWS
|
2014-09-10 16:40:22 +02:00
|
|
|
from pip.utils import appdirs
|
2014-06-13 04:59:01 +02:00
|
|
|
|
2012-11-24 04:32:20 +01:00
|
|
|
import pip.exceptions
|
2010-07-02 02:03:54 +02:00
|
|
|
|
2014-04-15 22:09:23 +02:00
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
# Hack for flake8
|
|
|
|
install
|
|
|
|
|
2014-06-13 04:59:01 +02:00
|
|
|
|
|
|
|
# CA Bundle Locations
|
|
|
|
CA_BUNDLE_PATHS = [
|
|
|
|
# Debian/Ubuntu/Gentoo etc.
|
|
|
|
"/etc/ssl/certs/ca-certificates.crt",
|
|
|
|
|
|
|
|
# Fedora/RHEL
|
|
|
|
"/etc/pki/tls/certs/ca-bundle.crt",
|
|
|
|
|
|
|
|
# OpenSUSE
|
|
|
|
"/etc/ssl/ca-bundle.pem",
|
|
|
|
|
|
|
|
# OpenBSD
|
|
|
|
"/etc/ssl/cert.pem",
|
|
|
|
|
|
|
|
# FreeBSD/DragonFly
|
|
|
|
"/usr/local/share/certs/ca-root-nss.crt",
|
|
|
|
|
|
|
|
# Homebrew on OSX
|
|
|
|
"/usr/local/etc/openssl/cert.pem",
|
|
|
|
]
|
|
|
|
|
|
|
|
# Attempt to locate a CA Bundle that we can pass into requests, we have a list
|
|
|
|
# of possible ones from various systems. If we cannot find one then we'll set
|
|
|
|
# this to None so that we default to whatever requests is setup to handle.
|
|
|
|
#
|
|
|
|
# Note to Downstream: If you wish to disable this autodetection and simply use
|
|
|
|
# whatever requests does (likely you've already patched
|
|
|
|
# requests.certs.where()) then simply edit this line so
|
|
|
|
# that it reads ``CA_BUNDLE_PATH = None``.
|
|
|
|
CA_BUNDLE_PATH = next((x for x in CA_BUNDLE_PATHS if os.path.exists(x)), None)
|
|
|
|
|
|
|
|
|
2014-04-24 13:29:57 +02:00
|
|
|
# Application Directories
|
|
|
|
USER_CACHE_DIR = appdirs.user_cache_dir("pip")
|
|
|
|
|
2010-07-02 02:03:54 +02:00
|
|
|
|
2013-05-19 03:05:25 +02:00
|
|
|
DELETE_MARKER_MESSAGE = '''\
|
|
|
|
This file is placed here by pip to indicate the source was put
|
|
|
|
here by pip.
|
|
|
|
|
|
|
|
Once this package is successfully installed this source code will be
|
|
|
|
deleted (unless you remove this file).
|
|
|
|
'''
|
|
|
|
PIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2013-05-19 03:05:25 +02:00
|
|
|
def write_delete_marker_file(directory):
|
|
|
|
"""
|
|
|
|
Write the pip delete marker file into this directory.
|
|
|
|
"""
|
|
|
|
filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)
|
|
|
|
marker_fp = open(filepath, 'w')
|
|
|
|
marker_fp.write(DELETE_MARKER_MESSAGE)
|
|
|
|
marker_fp.close()
|
|
|
|
|
|
|
|
|
2010-07-02 02:03:54 +02:00
|
|
|
def running_under_virtualenv():
|
|
|
|
"""
|
|
|
|
Return True if we're running inside a virtualenv, False otherwise.
|
|
|
|
|
|
|
|
"""
|
2013-12-20 01:52:40 +01:00
|
|
|
if hasattr(sys, 'real_prefix'):
|
|
|
|
return True
|
|
|
|
elif sys.prefix != getattr(sys, "base_prefix", sys.prefix):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2009-11-20 08:25:32 +01:00
|
|
|
|
2012-09-01 16:55:20 +02:00
|
|
|
|
2012-06-06 05:01:35 +02:00
|
|
|
def virtualenv_no_global():
|
|
|
|
"""
|
|
|
|
Return True if in a venv and no system site packages.
|
|
|
|
"""
|
2014-01-27 15:07:10 +01:00
|
|
|
# this mirrors the logic in virtualenv.py for locating the
|
|
|
|
# no-global-site-packages.txt file
|
2012-06-06 05:01:35 +02:00
|
|
|
site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
|
2012-09-01 16:55:20 +02:00
|
|
|
no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')
|
2012-06-06 05:01:35 +02:00
|
|
|
if running_under_virtualenv() and os.path.isfile(no_global_file):
|
|
|
|
return True
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2013-06-15 05:17:45 +02:00
|
|
|
def __get_username():
|
|
|
|
""" Returns the effective username of the current process. """
|
2014-06-26 14:15:31 +02:00
|
|
|
if WINDOWS:
|
2013-06-15 05:17:45 +02:00
|
|
|
return getpass.getuser()
|
|
|
|
import pwd
|
|
|
|
return pwd.getpwuid(os.geteuid()).pw_name
|
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
|
2012-11-23 14:11:49 +01:00
|
|
|
def _get_build_prefix():
|
|
|
|
""" Returns a safe build_prefix """
|
2014-01-27 15:07:10 +01:00
|
|
|
path = os.path.join(
|
|
|
|
tempfile.gettempdir(),
|
2014-08-22 01:18:52 +02:00
|
|
|
'pip_build_%s' % __get_username().replace(' ', '_')
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2014-06-26 14:15:31 +02:00
|
|
|
if WINDOWS:
|
2012-11-24 04:32:20 +01:00
|
|
|
""" on windows(tested on 7) temp dirs are isolated """
|
2012-11-23 14:11:49 +01:00
|
|
|
return path
|
|
|
|
try:
|
|
|
|
os.mkdir(path)
|
2013-05-19 03:05:25 +02:00
|
|
|
write_delete_marker_file(path)
|
2012-11-23 14:11:49 +01:00
|
|
|
except OSError:
|
2012-11-24 00:50:59 +01:00
|
|
|
file_uid = None
|
|
|
|
try:
|
2013-11-25 00:26:22 +01:00
|
|
|
# raises OSError for symlinks
|
|
|
|
# https://github.com/pypa/pip/pull/935#discussion_r5307003
|
|
|
|
file_uid = get_path_uid(path)
|
2012-11-24 00:50:59 +01:00
|
|
|
except OSError:
|
|
|
|
file_uid = None
|
2013-11-25 00:26:22 +01:00
|
|
|
|
2013-06-15 05:17:45 +02:00
|
|
|
if file_uid != os.geteuid():
|
2014-01-27 15:07:10 +01:00
|
|
|
msg = (
|
|
|
|
"The temporary folder for building (%s) is either not owned by"
|
|
|
|
" you, or is a symlink." % path
|
|
|
|
)
|
|
|
|
print(msg)
|
|
|
|
print(
|
|
|
|
"pip will not work until the temporary folder is either "
|
|
|
|
"deleted or is a real directory owned by your user account."
|
|
|
|
)
|
2012-11-24 04:32:20 +01:00
|
|
|
raise pip.exceptions.InstallationError(msg)
|
2012-11-23 14:11:49 +01:00
|
|
|
return path
|
2010-07-01 05:09:22 +02:00
|
|
|
|
2010-07-01 17:27:07 +02:00
|
|
|
if running_under_virtualenv():
|
2009-11-20 08:25:32 +01:00
|
|
|
build_prefix = os.path.join(sys.prefix, 'build')
|
|
|
|
src_prefix = os.path.join(sys.prefix, 'src')
|
|
|
|
else:
|
2013-11-25 00:26:22 +01:00
|
|
|
# Note: intentionally NOT using mkdtemp
|
|
|
|
# See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp
|
2013-02-22 06:36:26 +01:00
|
|
|
build_prefix = _get_build_prefix()
|
2012-09-01 16:55:20 +02:00
|
|
|
|
2014-03-26 23:24:19 +01:00
|
|
|
# FIXME: keep src in cwd for now (it is not a temporary folder)
|
2012-05-30 17:43:55 +02:00
|
|
|
try:
|
|
|
|
src_prefix = os.path.join(os.getcwd(), 'src')
|
|
|
|
except OSError:
|
|
|
|
# In case the current working directory has been renamed or deleted
|
2014-01-27 15:07:10 +01:00
|
|
|
sys.exit(
|
|
|
|
"The folder you are executing pip from can no longer be found."
|
|
|
|
)
|
2009-11-20 08:25:32 +01:00
|
|
|
|
2010-11-21 16:27:37 +01:00
|
|
|
# under Mac OS X + virtualenv sys.prefix is not properly resolved
|
|
|
|
# it is something like /path/to/python/bin/..
|
2013-11-25 00:26:22 +01:00
|
|
|
# Note: using realpath due to tmp dirs on OSX being symlinks
|
2013-02-22 06:36:26 +01:00
|
|
|
build_prefix = os.path.abspath(os.path.realpath(build_prefix))
|
2010-11-21 16:27:37 +01:00
|
|
|
src_prefix = os.path.abspath(src_prefix)
|
|
|
|
|
2009-11-20 08:25:32 +01:00
|
|
|
# FIXME doesn't account for venv linked to global site-packages
|
|
|
|
|
2014-04-15 22:09:23 +02:00
|
|
|
site_packages = sysconfig.get_python_lib()
|
2014-04-15 23:59:56 +02:00
|
|
|
user_site = site.USER_SITE
|
2009-11-20 08:25:32 +01:00
|
|
|
user_dir = os.path.expanduser('~')
|
2014-06-26 14:15:31 +02:00
|
|
|
if WINDOWS:
|
2009-11-20 08:25:32 +01:00
|
|
|
bin_py = os.path.join(sys.prefix, 'Scripts')
|
2014-04-15 23:59:56 +02:00
|
|
|
bin_user = os.path.join(user_site, 'Scripts')
|
2009-11-20 08:25:32 +01:00
|
|
|
# buildout uses 'bin' on Windows too?
|
|
|
|
if not os.path.exists(bin_py):
|
|
|
|
bin_py = os.path.join(sys.prefix, 'bin')
|
2014-04-15 23:59:56 +02:00
|
|
|
bin_user = os.path.join(user_site, 'bin')
|
2010-03-11 00:20:33 +01:00
|
|
|
default_storage_dir = os.path.join(user_dir, 'pip')
|
2013-12-04 14:04:54 +01:00
|
|
|
default_config_basename = 'pip.ini'
|
2014-01-27 15:07:10 +01:00
|
|
|
default_config_file = os.path.join(
|
|
|
|
default_storage_dir,
|
|
|
|
default_config_basename,
|
|
|
|
)
|
2009-11-20 08:25:32 +01:00
|
|
|
else:
|
|
|
|
bin_py = os.path.join(sys.prefix, 'bin')
|
2014-04-15 23:59:56 +02:00
|
|
|
bin_user = os.path.join(user_site, 'bin')
|
2012-12-27 18:51:14 +01:00
|
|
|
default_storage_dir = os.path.join(user_dir, '.pip')
|
2013-12-04 14:04:54 +01:00
|
|
|
default_config_basename = 'pip.conf'
|
2014-01-27 15:07:10 +01:00
|
|
|
default_config_file = os.path.join(
|
|
|
|
default_storage_dir,
|
|
|
|
default_config_basename,
|
|
|
|
)
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2009-11-20 08:25:32 +01:00
|
|
|
# Forcing to use /usr/local/bin for standard Mac OS X framework installs
|
2011-12-10 13:56:03 +01:00
|
|
|
# Also log to ~/Library/Logs/ for use with the Console.app log viewer
|
2009-11-20 08:25:32 +01:00
|
|
|
if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
|
|
|
|
bin_py = '/usr/local/bin'
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2014-08-15 09:23:46 +02:00
|
|
|
site_config_files = [
|
|
|
|
os.path.join(path, default_config_basename)
|
|
|
|
for path in appdirs.site_config_dirs('pip')
|
|
|
|
]
|
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2013-10-30 13:03:47 +01:00
|
|
|
def distutils_scheme(dist_name, user=False, home=None, root=None):
|
2012-11-15 01:53:22 +01:00
|
|
|
"""
|
|
|
|
Return a distutils install scheme
|
|
|
|
"""
|
|
|
|
from distutils.dist import Distribution
|
|
|
|
|
|
|
|
scheme = {}
|
|
|
|
d = Distribution({'name': dist_name})
|
2013-11-05 08:40:26 +01:00
|
|
|
d.parse_config_files()
|
|
|
|
i = d.get_command_obj('install', create=True)
|
2014-01-27 15:07:10 +01:00
|
|
|
# NOTE: setting user or home has the side-effect of creating the home dir
|
|
|
|
# or user base for installations during finalize_options()
|
2013-08-31 17:17:02 +02:00
|
|
|
# ideally, we'd prefer a scheme class that has no side-effects.
|
2013-04-18 07:50:22 +02:00
|
|
|
i.user = user or i.user
|
2012-11-15 01:53:22 +01:00
|
|
|
i.home = home or i.home
|
2013-10-30 13:03:47 +01:00
|
|
|
i.root = root or i.root
|
2012-11-15 01:53:22 +01:00
|
|
|
i.finalize_options()
|
|
|
|
for key in SCHEME_KEYS:
|
2014-02-24 22:52:23 +01:00
|
|
|
scheme[key] = getattr(i, 'install_' + key)
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2014-03-13 20:19:26 +01:00
|
|
|
if i.install_lib is not None:
|
|
|
|
# install_lib takes precedence over purelib and platlib
|
|
|
|
scheme.update(dict(purelib=i.install_lib, platlib=i.install_lib))
|
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
if running_under_virtualenv():
|
2014-01-27 15:07:10 +01:00
|
|
|
scheme['headers'] = os.path.join(
|
|
|
|
sys.prefix,
|
|
|
|
'include',
|
|
|
|
'site',
|
|
|
|
'python' + sys.version[:3],
|
|
|
|
dist_name,
|
|
|
|
)
|
2012-11-15 01:53:22 +01:00
|
|
|
|
2013-10-31 13:49:23 +01:00
|
|
|
if root is not None:
|
|
|
|
scheme["headers"] = os.path.join(
|
|
|
|
root,
|
|
|
|
os.path.abspath(scheme["headers"])[1:],
|
|
|
|
)
|
|
|
|
|
2012-11-15 01:53:22 +01:00
|
|
|
return scheme
|