2009-11-20 08:25:32 +01:00
|
|
|
"""Locations where we look for configs, install stuff, etc"""
|
|
|
|
|
|
|
|
import sys
|
2012-06-06 05:01:35 +02:00
|
|
|
import site
|
2009-11-20 08:25:32 +01:00
|
|
|
import os
|
2012-04-23 03:34:58 +02:00
|
|
|
import tempfile
|
2012-11-15 01:53:22 +01:00
|
|
|
from distutils.command.install import install, SCHEME_KEYS
|
2012-11-23 14:11:49 +01:00
|
|
|
import getpass
|
2011-03-15 20:49:48 +01:00
|
|
|
from pip.backwardcompat import get_python_lib
|
2012-11-24 04:32:20 +01:00
|
|
|
import pip.exceptions
|
2010-07-02 02:03:54 +02:00
|
|
|
|
2013-02-07 08:27:53 +01:00
|
|
|
default_cert_path = os.path.join(os.path.dirname(__file__), 'cacert.pem')
|
2010-07-02 02:03:54 +02:00
|
|
|
|
|
|
|
def running_under_virtualenv():
|
|
|
|
"""
|
|
|
|
Return True if we're running inside a virtualenv, False otherwise.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return hasattr(sys, 'real_prefix')
|
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.
|
|
|
|
"""
|
|
|
|
#this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file
|
|
|
|
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
|
|
|
|
|
2012-11-23 14:11:49 +01:00
|
|
|
def _get_build_prefix():
|
|
|
|
""" Returns a safe build_prefix """
|
|
|
|
path = os.path.join(tempfile.gettempdir(), 'pip-build-%s' % \
|
|
|
|
getpass.getuser())
|
|
|
|
if sys.platform == 'win32':
|
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)
|
|
|
|
except OSError:
|
2012-11-24 00:50:59 +01:00
|
|
|
file_uid = None
|
|
|
|
try:
|
2012-11-28 12:30:56 +01:00
|
|
|
fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
|
2012-11-24 00:50:59 +01:00
|
|
|
file_uid = os.fstat(fd).st_uid
|
|
|
|
os.close(fd)
|
|
|
|
except OSError:
|
|
|
|
file_uid = None
|
|
|
|
if file_uid != os.getuid():
|
2012-11-24 04:32:20 +01:00
|
|
|
msg = "The temporary folder for building (%s) is not owned by your user!" \
|
|
|
|
% path
|
|
|
|
print (msg)
|
2012-11-23 14:11:49 +01:00
|
|
|
print("pip will not work until the temporary folder is " + \
|
|
|
|
"either deleted or 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:
|
2012-06-05 00:23:31 +02:00
|
|
|
# Use tempfile to create a temporary folder for build
|
|
|
|
# Note: we are NOT using mkdtemp so we can have a consistent build dir
|
2013-01-16 08:18:23 +01:00
|
|
|
# Note: using realpath due to tmp dirs on OSX being symlinks
|
2013-02-22 06:36:26 +01:00
|
|
|
build_prefix = _get_build_prefix()
|
2012-09-01 16:55:20 +02:00
|
|
|
|
2012-06-05 00:23:31 +02: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
|
|
|
|
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-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
|
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
site_packages = get_python_lib()
|
2009-11-20 08:25:32 +01:00
|
|
|
user_dir = os.path.expanduser('~')
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
bin_py = os.path.join(sys.prefix, 'Scripts')
|
|
|
|
# buildout uses 'bin' on Windows too?
|
|
|
|
if not os.path.exists(bin_py):
|
|
|
|
bin_py = os.path.join(sys.prefix, 'bin')
|
2010-03-11 00:20:33 +01:00
|
|
|
default_storage_dir = os.path.join(user_dir, 'pip')
|
|
|
|
default_config_file = os.path.join(default_storage_dir, 'pip.ini')
|
|
|
|
default_log_file = os.path.join(default_storage_dir, 'pip.log')
|
2009-11-20 08:25:32 +01:00
|
|
|
else:
|
|
|
|
bin_py = os.path.join(sys.prefix, 'bin')
|
2012-12-27 18:51:14 +01:00
|
|
|
default_storage_dir = os.path.join(user_dir, '.pip')
|
2010-03-11 00:20:33 +01:00
|
|
|
default_config_file = os.path.join(default_storage_dir, 'pip.conf')
|
|
|
|
default_log_file = os.path.join(default_storage_dir, 'pip.log')
|
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'
|
2011-12-10 13:56:03 +01:00
|
|
|
default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')
|
2012-11-15 01:53:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def distutils_scheme(dist_name, user=False, home=None):
|
|
|
|
"""
|
|
|
|
Return a distutils install scheme
|
|
|
|
"""
|
|
|
|
from distutils.dist import Distribution
|
|
|
|
|
|
|
|
scheme = {}
|
|
|
|
d = Distribution({'name': dist_name})
|
|
|
|
i = install(d)
|
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
|
|
|
|
i.finalize_options()
|
|
|
|
for key in SCHEME_KEYS:
|
|
|
|
scheme[key] = getattr(i, 'install_'+key)
|
|
|
|
|
|
|
|
#be backward-compatible with what pip has always done?
|
|
|
|
scheme['scripts'] = bin_py
|
|
|
|
|
|
|
|
if running_under_virtualenv():
|
|
|
|
scheme['headers'] = os.path.join(sys.prefix,
|
|
|
|
'include',
|
|
|
|
'site',
|
|
|
|
'python' + sys.version[:3],
|
|
|
|
dist_name)
|
|
|
|
|
|
|
|
return scheme
|