summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLászló Kiss Kollár <lkollar@users.noreply.github.com>2019-05-28 10:28:18 +0100
committerChris Jerdonek <chris.jerdonek@gmail.com>2019-05-28 02:28:18 -0700
commit929fefdbae2ddddfd325204b43434310a8600115 (patch)
tree36e4e69876d826b6c44759099546ec7f937101ab
parentd3b9942a2478978f971853e9c906b157786bdb23 (diff)
downloadpip-929fefdbae2ddddfd325204b43434310a8600115.tar.gz
Add --path to pip freeze to support --target installations (#6450)
* Add support for --path in pip freeze
-rw-r--r--news/6404.feature2
-rw-r--r--src/pip/_internal/commands/freeze.py13
-rw-r--r--src/pip/_internal/operations/freeze.py4
-rw-r--r--src/pip/_internal/utils/misc.py24
-rw-r--r--tests/functional/test_freeze.py59
5 files changed, 94 insertions, 8 deletions
diff --git a/news/6404.feature b/news/6404.feature
new file mode 100644
index 000000000..f3f6bae55
--- /dev/null
+++ b/news/6404.feature
@@ -0,0 +1,2 @@
+Add a ``--path`` argument to ``pip freeze`` to support ``--target``
+installations.
diff --git a/src/pip/_internal/commands/freeze.py b/src/pip/_internal/commands/freeze.py
index dc9c53a6b..47bd443a4 100644
--- a/src/pip/_internal/commands/freeze.py
+++ b/src/pip/_internal/commands/freeze.py
@@ -4,6 +4,7 @@ import sys
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
@@ -57,6 +58,12 @@ class FreezeCommand(Command):
default=False,
help='Only output packages installed in user-site.')
self.cmd_opts.add_option(
+ '--path',
+ dest='path',
+ action='append',
+ help='Restrict to the specified installation path for listing '
+ 'packages (can be used multiple times).')
+ self.cmd_opts.add_option(
'--all',
dest='freeze_all',
action='store_true',
@@ -77,11 +84,17 @@ class FreezeCommand(Command):
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,
diff --git a/src/pip/_internal/operations/freeze.py b/src/pip/_internal/operations/freeze.py
index 03e8696bb..6f5a3dd97 100644
--- a/src/pip/_internal/operations/freeze.py
+++ b/src/pip/_internal/operations/freeze.py
@@ -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]
@@ -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 as exc:
diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py
index 024f4e750..67ab33cef 100644
--- a/src/pip/_internal/utils/misc.py
+++ b/src/pip/_internal/utils/misc.py
@@ -390,12 +390,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.
@@ -412,7 +415,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:
@@ -440,7 +450,7 @@ def get_installed_distributions(local_only=True,
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
diff --git a/tests/functional/test_freeze.py b/tests/functional/test_freeze.py
index 68625ff43..38fad0a66 100644
--- a/tests/functional/test_freeze.py
+++ b/tests/functional/test_freeze.py
@@ -659,3 +659,62 @@ 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==1.0')
+ result = script.pip('freeze', '--user')
+ expected = textwrap.dedent("""\
+ simple2==3.0
+ <BLANKLINE>""")
+ _check_output(result.stdout, expected)
+ result = script.pip('freeze', '--path', tmpdir)
+ expected = textwrap.dedent("""\
+ simple==1.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)
+ expected = textwrap.dedent("""\
+ simple==2.0
+ <BLANKLINE>""")
+ _check_output(result.stdout, expected)
+ result = script.pip('freeze', '--path', path1, '--path', path2)
+ expected = textwrap.dedent("""\
+ simple==2.0
+ simple2==3.0
+ <BLANKLINE>""")
+ _check_output(result.stdout, expected)