Skip to content

Commit

Permalink
We no longer need to handle a static build directory
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft committed Apr 7, 2015
1 parent 83eeec2 commit 81ad0f0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 145 deletions.
50 changes: 2 additions & 48 deletions pip/locations.py
Expand Up @@ -6,18 +6,12 @@
import os.path
import site
import sys
import tempfile

from distutils import sysconfig
from distutils.command.install import install, SCHEME_KEYS
from distutils.command.install import install, SCHEME_KEYS # noqa

from pip.compat import get_path_uid, WINDOWS
from pip.compat import WINDOWS
from pip.utils import appdirs
from pip import exceptions


# Hack for flake8
install


# CA Bundle Locations
Expand Down Expand Up @@ -108,48 +102,9 @@ def __get_username():
return pwd.getpwuid(os.geteuid()).pw_name


def _get_build_prefix():
""" Returns a safe build_prefix """
path = os.path.join(
tempfile.gettempdir(),
'pip_build_%s' % __get_username().replace(' ', '_')
)
if WINDOWS:
""" on windows(tested on 7) temp dirs are isolated """
return path
try:
os.mkdir(path)
write_delete_marker_file(path)
except OSError:
file_uid = None
try:
# raises OSError for symlinks
# https://github.com/pypa/pip/pull/935#discussion_r5307003
file_uid = get_path_uid(path)
except OSError:
file_uid = None

if file_uid != os.geteuid():
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."
)
raise exceptions.InstallationError(msg)
return path

if running_under_virtualenv():
build_prefix = os.path.join(sys.prefix, 'build')
src_prefix = os.path.join(sys.prefix, 'src')
else:
# Note: intentionally NOT using mkdtemp
# See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp
build_prefix = _get_build_prefix()

# FIXME: keep src in cwd for now (it is not a temporary folder)
try:
src_prefix = os.path.join(os.getcwd(), 'src')
Expand All @@ -162,7 +117,6 @@ def _get_build_prefix():
# under Mac OS X + virtualenv sys.prefix is not properly resolved
# it is something like /path/to/python/bin/..
# Note: using realpath due to tmp dirs on OSX being symlinks
build_prefix = os.path.abspath(os.path.realpath(build_prefix))
src_prefix = os.path.abspath(src_prefix)

# FIXME doesn't account for venv linked to global site-packages
Expand Down
15 changes: 1 addition & 14 deletions pip/req/req_set.py
Expand Up @@ -13,9 +13,8 @@
from pip.download import (url_to_path, unpack_url)
from pip.exceptions import (InstallationError, BestVersionAlreadyInstalled,
DistributionNotFound, PreviousBuildDirError)
from pip.locations import (PIP_DELETE_MARKER_FILENAME, build_prefix)
from pip.req.req_install import InstallRequirement
from pip.utils import (display_path, rmtree, dist_in_usersite, normalize_path)
from pip.utils import display_path, dist_in_usersite, normalize_path
from pip.utils.logging import indent_log
from pip.vcs import vcs

Expand Down Expand Up @@ -558,18 +557,6 @@ def cleanup_files(self):
for req in self.reqs_to_cleanup:
req.remove_temporary_source()

if self._pip_has_created_build_dir():
logger.debug('Removing temporary dir %s...', self.build_dir)
rmtree(self.build_dir)

def _pip_has_created_build_dir(self):
return (
self.build_dir == build_prefix and
os.path.exists(
os.path.join(self.build_dir, PIP_DELETE_MARKER_FILENAME)
)
)

def _to_install(self):
"""Create the installation order.
Expand Down
83 changes: 0 additions & 83 deletions tests/unit/test_locations.py
Expand Up @@ -8,10 +8,7 @@
import tempfile
import getpass

import pytest

from mock import Mock
import pip

from pip.locations import distutils_scheme

Expand Down Expand Up @@ -77,86 +74,6 @@ def get_mock_getpwuid(self, uid):
result.pw_name = self.username
return result

def get_build_dir_location(self):
""" returns a string pointing to the
current build_prefix.
"""
return os.path.join(self.tempdir, 'pip_build_%s' % self.username)

def test_dir_path(self):
""" test the path name for the build_prefix
"""
from pip import locations
assert locations._get_build_prefix() == self.get_build_dir_location()

# skip on windows, build dir is not created
@pytest.mark.skipif("sys.platform == 'win32'")
@pytest.mark.skipif("not hasattr(os, 'O_NOFOLLOW')")
def test_dir_created(self):
""" test that the build_prefix directory is generated when
_get_build_prefix is called.
"""
assert not os.path.exists(self.get_build_dir_location()), \
"the build_prefix directory should not exist yet!"
from pip import locations
locations._get_build_prefix()
assert os.path.exists(self.get_build_dir_location()), \
"the build_prefix directory should now exist!"

# skip on windows, build dir is not created
@pytest.mark.skipif("sys.platform == 'win32'")
def test_dir_created_without_NOFOLLOW(self, monkeypatch):
""" test that the build_prefix directory is generated when
os.O_NOFOLLOW doen't exist
"""
if hasattr(os, 'O_NOFOLLOW'):
monkeypatch.delattr("os.O_NOFOLLOW")
assert not os.path.exists(self.get_build_dir_location()), \
"the build_prefix directory should not exist yet!"
from pip import locations
locations._get_build_prefix()
assert os.path.exists(self.get_build_dir_location()), \
"the build_prefix directory should now exist!"

# skip on windows; this exception logic only runs on linux
@pytest.mark.skipif("sys.platform == 'win32'")
@pytest.mark.skipif("not hasattr(os, 'O_NOFOLLOW')")
def test_error_raised_when_owned_by_another(self):
""" test calling _get_build_prefix when there is a temporary
directory owned by another user raises an InstallationError.
"""
from pip import locations
os.geteuid = lambda: 1111
os.mkdir(self.get_build_dir_location())

with pytest.raises(pip.exceptions.InstallationError):
locations._get_build_prefix()

# skip on windows; this exception logic only runs on linux
@pytest.mark.skipif("sys.platform == 'win32'")
def test_error_raised_when_owned_by_another_without_NOFOLLOW(
self, monkeypatch):
""" test calling _get_build_prefix when there is a temporary
directory owned by another user raises an InstallationError.
(when os.O_NOFOLLOW doesn't exist
"""
if hasattr(os, 'O_NOFOLLOW'):
monkeypatch.delattr("os.O_NOFOLLOW")
from pip import locations
os.geteuid = lambda: 1111
os.mkdir(self.get_build_dir_location())

with pytest.raises(pip.exceptions.InstallationError):
locations._get_build_prefix()

def test_no_error_raised_when_owned_by_you(self):
""" test calling _get_build_prefix when there is a temporary
directory owned by you raise no InstallationError.
"""
from pip import locations
os.mkdir(self.get_build_dir_location())
locations._get_build_prefix()


class TestDisutilsScheme:

Expand Down

0 comments on commit 81ad0f0

Please sign in to comment.