Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't follow symlinks when uninstalling files #2552

Merged
merged 9 commits into from
Mar 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pip/req/req_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _can_uninstall(self):
return True

def add(self, path):
path = normalize_path(path)
path = normalize_path(path, resolve_symlinks=False)
if not os.path.exists(path):
return
if self._permitted(path):
Expand Down
9 changes: 7 additions & 2 deletions pip/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,17 @@ def make_path_relative(path, rel_to):
return os.path.sep.join(full_parts)


def normalize_path(path):
def normalize_path(path, resolve_symlinks=True):
"""
Convert a path to its canonical, case-normalized, absolute version.

"""
return os.path.normcase(os.path.realpath(os.path.expanduser(path)))
path = os.path.expanduser(path)
if resolve_symlinks:
path = os.path.realpath(path)
else:
path = os.path.abspath(path)
return os.path.normcase(path)


def splitext(path):
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_req_uninstall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os

import pytest
from mock import Mock

import pip.req.req_uninstall
from pip.req.req_uninstall import UninstallPathSet


# Pretend all files are local, so UninstallPathSet accepts files in the tmpdir,
# outside the virtualenv
def mock_is_local(path):
return True


class TestUninstallPathSet(object):
def test_add(self, tmpdir, monkeypatch):
monkeypatch.setattr(pip.req.req_uninstall, 'is_local', mock_is_local)
file_extant = os.path.join(tmpdir, 'foo')
file_nonexistant = os.path.join(tmpdir, 'nonexistant')
with open(file_extant, 'w'):
pass

ups = UninstallPathSet(dist=Mock())
assert ups.paths == set()
ups.add(file_extant)
assert ups.paths == set([file_extant])

ups.add(file_nonexistant)
assert ups.paths == set([file_extant])

@pytest.mark.skipif("sys.platform == 'win32'")
def test_add_symlink(self, tmpdir, monkeypatch):
monkeypatch.setattr(pip.req.req_uninstall, 'is_local', mock_is_local)
f = os.path.join(tmpdir, 'foo')
with open(f, 'w'):
pass
l = os.path.join(tmpdir, 'foo_link')
os.symlink(f, l)

ups = UninstallPathSet(dist=Mock())
ups.add(l)
assert ups.paths == set([l])
39 changes: 38 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from mock import Mock, patch
from pip.utils import (egg_link_path, Inf, get_installed_distributions,
untar_file, unzip_file, rmtree)
untar_file, unzip_file, rmtree, normalize_path)
from pip.operations.freeze import freeze_excludes


Expand Down Expand Up @@ -369,3 +369,40 @@ def test_rmtree_retries_for_3sec(tmpdir, monkeypatch):
monkeypatch.setattr(shutil, 'rmtree', Failer(duration=5).call)
with pytest.raises(OSError):
rmtree('foo')


class Test_normalize_path(object):
# Technically, symlinks are possible on Windows, but you need a special
# permission bit to create them, and Python 2 doesn't support it anyway, so
# it's easiest just to skip this test on Windows altogether.
@pytest.mark.skipif("sys.platform == 'win32'")
def test_resolve_symlinks(self, tmpdir):
print(type(tmpdir))
print(dir(tmpdir))
orig_working_dir = os.getcwd()
os.chdir(tmpdir)
try:
d = os.path.join('foo', 'bar')
f = os.path.join(d, 'file1')
os.makedirs(d)
with open(f, 'w'): # Create the file
pass

os.symlink(d, 'dir_link')
os.symlink(f, 'file_link')

assert normalize_path(
'dir_link/file1', resolve_symlinks=True
) == os.path.join(tmpdir, f)
assert normalize_path(
'dir_link/file1', resolve_symlinks=False
) == os.path.join(tmpdir, 'dir_link', 'file1')

assert normalize_path(
'file_link', resolve_symlinks=True
) == os.path.join(tmpdir, f)
assert normalize_path(
'file_link', resolve_symlinks=False
) == os.path.join(tmpdir, 'file_link')
finally:
os.chdir(orig_working_dir)