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

Fix pip freeze not showing correct entry for mercurial packages that use subdirectories. #7072

Merged
merged 17 commits into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions news/7071.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `pip freeze` not showing correct entry for mercurial packages that use subdirectories.
4 changes: 2 additions & 2 deletions src/pip/_internal/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ def update_submodules(cls, location):

@classmethod
def controls_location(cls, location):
if super(Git, cls).controls_location(location):
return True
if not super(Git, cls).controls_location(location):
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
return False
try:
r = cls.run_command(['rev-parse'],
cwd=location,
Expand Down
44 changes: 44 additions & 0 deletions src/pip/_internal/vcs/mercurial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from pip._vendor.six.moves import configparser

from pip._internal.exceptions import BadCommand
from pip._internal.utils.compat import samefile
from pip._internal.utils.misc import display_path, make_command, path_to_url
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
Expand Down Expand Up @@ -111,5 +113,47 @@ def is_commit_id_equal(cls, dest, name):
"""Always assume the versions don't match"""
return False

@classmethod
def get_subdirectory(cls, location):
# find the repo root
root_dir = cls.run_command(['root'],
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
show_stdout=False, cwd=location).strip()
if not os.path.isabs(root_dir):
root_dir = os.path.join(location, root_dir)
# find setup.py
orig_location = location
while not os.path.exists(os.path.join(location, 'setup.py')):
last_location = location
location = os.path.dirname(location)
if location == last_location:
# We've traversed up to the root of the filesystem without
# finding setup.py
logger.warning(
"Could not find setup.py for directory %s (tried all "
"parent directories)",
orig_location,
)
return None
# relative path of setup.py to repo root
if samefile(root_dir, location):
return None
return os.path.relpath(location, root_dir)
chrahunt marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def controls_location(cls, location):
if not super(Mercurial, cls).controls_location(location):
return False
try:
r = cls.run_command(['identify'],
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
cwd=location,
show_stdout=False,
on_returncode='ignore',
extra_ok_returncodes=[255])
return not r.startswith('abort:')
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
except BadCommand:
logger.debug("could not determine if %s is under hg control "
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
"because hg is not available", location)
return False


vcs.register(Mercurial)
17 changes: 12 additions & 5 deletions src/pip/_internal/vcs/versioncontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,17 @@ def controls_location(cls, location):
# type: (str) -> bool
"""
Check if a location is controlled by the vcs.
It is meant to be overridden to implement smarter detection
mechanisms for specific vcs.

This can do more than is_repository_directory() alone. For example,
the Git override checks that Git is actually available.
Searches up the filesystem and checks is_repository_directory().

It is meant to be extended to add smarter detection mechanisms for
specific vcs. For example, the Git override checks that Git is
actually available.
"""
return cls.is_repository_directory(location)
while not cls.is_repository_directory(location):
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
last_location = location
location = os.path.dirname(location)
if location == last_location:
# We've traversed up to the root of the filesystem.
return False
return True
43 changes: 43 additions & 0 deletions tests/functional/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,49 @@ def test_freeze_git_clone_srcdir(script, tmpdir):
_check_output(result.stdout, expected)


@need_mercurial
def test_freeze_mercurial_clone_srcdir(script, tmpdir):
"""
Test freezing a Mercurial clone where setup.py is in a subdirectory
relative to the repo root and the source code is in a subdirectory
relative to setup.py.
"""
# Returns path to a generated package called "version_pkg"
pkg_version = _create_test_package_with_srcdir(script, vcs='hg')

result = script.run(
'hg', 'clone', pkg_version, 'pip-test-package',
expect_stderr=True,
chrahunt marked this conversation as resolved.
Show resolved Hide resolved
)
repo_dir = script.scratch_path / 'pip-test-package'
result = script.run(
'python', 'setup.py', 'develop',
cwd=repo_dir / 'subdir',
expect_stderr=True,
)
result = script.pip('freeze', expect_stderr=True)
expected = textwrap.dedent(
"""
...-e hg+...#egg=version_pkg&subdirectory=subdir
...
"""
).strip()
_check_output(result.stdout, expected)

result = script.pip(
'freeze', '-f', '%s#egg=pip_test_package' % repo_dir,
expect_stderr=True,
)
expected = textwrap.dedent(
"""
-f %(repo)s#egg=pip_test_package...
-e hg+...#egg=version_pkg&subdirectory=subdir
...
""" % {'repo': repo_dir},
).strip()
_check_output(result.stdout, expected)


@pytest.mark.git
def test_freeze_git_remote(script, tmpdir):
"""
Expand Down