1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/pip/locations.py

122 lines
4.5 KiB
Python
Raw Normal View History

"""Locations where we look for configs, install stuff, etc"""
import sys
import site
import os
import shutil
import tempfile
from distutils.command.install import install, SCHEME_KEYS
2011-03-15 20:49:48 +01:00
from pip.backwardcompat import get_python_lib
def running_under_virtualenv():
"""
Return True if we're running inside a virtualenv, False otherwise.
"""
return hasattr(sys, 'real_prefix')
2012-09-01 16:55:20 +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')
if running_under_virtualenv() and os.path.isfile(no_global_file):
return True
if running_under_virtualenv():
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
build_prefix = os.path.join(tempfile.gettempdir(), 'pip-build')
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.")
# under Mac OS X + virtualenv sys.prefix is not properly resolved
# it is something like /path/to/python/bin/..
build_prefix = os.path.abspath(build_prefix)
src_prefix = os.path.abspath(src_prefix)
# FIXME doesn't account for venv linked to global site-packages
2011-03-15 20:49:48 +01:00
site_packages = get_python_lib()
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')
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')
else:
bin_py = os.path.join(sys.prefix, 'bin')
# Use XDG_CONFIG_HOME instead of the ~/.pip
# On some systems, we may have to create this, on others it probably exists
xdg_dir = os.path.join(user_dir, '.config')
xdg_dir = os.environ.get('XDG_CONFIG_HOME', xdg_dir)
if not os.path.exists(xdg_dir):
os.mkdir(xdg_dir)
default_storage_dir = os.path.join(xdg_dir, 'pip')
default_config_file = os.path.join(default_storage_dir, 'pip.conf')
default_log_file = os.path.join(default_storage_dir, 'pip.log')
# Migration path for users- move things from the old dir if it exists
# If the new dir exists and has no pip.conf and the old dir does, move it
# When these checks are finished, delete the old directory
old_storage_dir = os.path.join(user_dir, '.pip')
2012-05-30 17:43:55 +02:00
old_config_file = os.path.join(old_storage_dir, 'pip.conf')
if os.path.exists(old_storage_dir):
if not os.path.exists(default_storage_dir):
shutil.copytree(old_storage_dir, default_storage_dir)
2012-05-30 17:43:55 +02:00
elif os.path.exists(old_config_file) and not os.path.exists(default_config_file):
shutil.copy2(old_config_file, default_config_file)
shutil.rmtree(old_storage_dir)
# Forcing to use /usr/local/bin for standard Mac OS X framework installs
# Also log to ~/Library/Logs/ for use with the Console.app log viewer
if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
bin_py = '/usr/local/bin'
default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')
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)
2012-11-15 02:33:16 +01:00
if sys.version_info >= (2, 6):
i.user = user or i.user
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