Skip to content

Commit

Permalink
Add support for --path in pip freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
lkollar committed May 7, 2019
1 parent 58c93ac commit 6448bb0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
2 changes: 2 additions & 0 deletions news/6404.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support ``--target`` installations with ``pip freeze`` with the ``--path``
argument.
12 changes: 12 additions & 0 deletions src/pip/_internal/commands/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pip._internal.cache import WheelCache
from pip._internal.cli.base_command import Command
from pip._internal.exceptions import CommandError
from pip._internal.models.format_control import FormatControl
from pip._internal.operations.freeze import freeze
from pip._internal.utils.compat import stdlib_pkgs
Expand Down Expand Up @@ -56,6 +57,11 @@ def __init__(self, *args, **kw):
action='store_true',
default=False,
help='Only output packages installed in user-site.')
self.cmd_opts.add_option(
'--path',
dest='path',
action='append',
help='Use the specified installation path for listing packages')
self.cmd_opts.add_option(
'--all',
dest='freeze_all',
Expand All @@ -77,11 +83,17 @@ def run(self, options, args):
if not options.freeze_all:
skip.update(DEV_PKGS)

if options.path and (options.user or options.local):
raise CommandError(
"Cannot combine '--path' with '--user' or '--local'"
)

freeze_kwargs = dict(
requirement=options.requirements,
find_links=options.find_links,
local_only=options.local,
user_only=options.user,
paths=options.path,
skip_regex=options.skip_requirements_regex,
isolated=options.isolated_mode,
wheel_cache=wheel_cache,
Expand Down
4 changes: 3 additions & 1 deletion src/pip/_internal/operations/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def freeze(
find_links=None, # type: Optional[List[str]]
local_only=None, # type: Optional[bool]
user_only=None, # type: Optional[bool]
paths=None, # type: Optional[List[str]]
skip_regex=None, # type: Optional[str]
isolated=False, # type: bool
wheel_cache=None, # type: Optional[WheelCache]
Expand All @@ -57,7 +58,8 @@ def freeze(
installations = {} # type: Dict[str, FrozenRequirement]
for dist in get_installed_distributions(local_only=local_only,
skip=(),
user_only=user_only):
user_only=user_only,
paths=paths):
try:
req = FrozenRequirement.from_dist(dist)
except RequirementParseError:
Expand Down
24 changes: 17 additions & 7 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,15 @@ def dist_is_editable(dist):
return False


def get_installed_distributions(local_only=True,
skip=stdlib_pkgs,
include_editables=True,
editables_only=False,
user_only=False):
# type: (bool, Container[str], bool, bool, bool) -> List[Distribution]
def get_installed_distributions(
local_only=True, # type: bool
skip=stdlib_pkgs, # type: Container[str]
include_editables=True, # type: bool
editables_only=False, # type: bool
user_only=False, # type: bool
paths=None # type: Optional[List[str]]
):
# type: (...) -> List[Distribution]
"""
Return a list of installed Distribution objects.
Expand All @@ -391,7 +394,14 @@ def get_installed_distributions(local_only=True,
If ``user_only`` is True , only report installations in the user
site directory.
If ``paths`` is set, only report the distributions present at the
specified list of locations.
"""
if paths:
working_set = pkg_resources.WorkingSet(paths)
else:
working_set = pkg_resources.working_set

if local_only:
local_test = dist_is_local
else:
Expand Down Expand Up @@ -419,7 +429,7 @@ def user_test(d):
return True

# because of pkg_resources vendoring, mypy cannot find stub in typeshed
return [d for d in pkg_resources.working_set # type: ignore
return [d for d in working_set # type: ignore
if local_test(d) and
d.key not in skip and
editable_test(d) and
Expand Down
49 changes: 49 additions & 0 deletions tests/functional/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,52 @@ def test_freeze_user(script, virtualenv, data):
<BLANKLINE>""")
_check_output(result.stdout, expected)
assert 'simple2' not in result.stdout


def test_freeze_path(tmpdir, script, data):
"""
Test freeze with --path.
"""
script.pip('install', '--find-links', data.find_links,
'--target', tmpdir, 'simple==2.0')
result = script.pip('freeze', '--path', tmpdir)
expected = textwrap.dedent("""\
simple==2.0
<BLANKLINE>""")
_check_output(result.stdout, expected)


def test_freeze_path_exclude_user(tmpdir, script, data):
"""
Test freeze with --path and make sure packages from --user are not picked
up.
"""
script.pip_install_local('--find-links', data.find_links,
'--user', 'simple2')
script.pip('install', '--find-links', data.find_links,
'--target', tmpdir, 'simple==2.0')
result = script.pip('freeze', '--path', tmpdir)
expected = textwrap.dedent("""\
simple==2.0
<BLANKLINE>""")
_check_output(result.stdout, expected)


def test_freeze_path_multiple(tmpdir, script, data):
"""
Test freeze with multiple --path arguments.
"""
path1 = tmpdir / "path1"
os.mkdir(path1)
path2 = tmpdir / "path2"
os.mkdir(path2)
script.pip('install', '--find-links', data.find_links,
'--target', path1, 'simple==2.0')
script.pip('install', '--find-links', data.find_links,
'--target', path2, 'simple2==3.0')
result = script.pip('freeze', '--path', path1, '--path', path2)
expected = textwrap.dedent("""\
simple==2.0
simple2==3.0
<BLANKLINE>""")
_check_output(result.stdout, expected)

0 comments on commit 6448bb0

Please sign in to comment.