Skip to content

Commit 4396efa

Browse files
committed
pip freeze is not recursive by default. Added --recursive option to make it recursive. Refs pypa#1308.
1 parent ffa2694 commit 4396efa

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

pip/commands/freeze.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ def __init__(self, *args, **kw):
6565
action='store_true',
6666
default=False,
6767
help='Only output packages installed in user-site.')
68+
self.cmd_opts.add_option(
69+
'--recursive',
70+
dest='recursive',
71+
action='store_true',
72+
default=False,
73+
help="Freeze packages and dependencies, recursively.")
6874

6975
self.parser.insert_option_group(0, self.cmd_opts)
7076

@@ -73,6 +79,7 @@ def run(self, options, args):
7379
find_links = options.find_links or []
7480
local_only = options.local
7581
user_only = options.user
82+
recursive = options.recursive
7683
# FIXME: Obviously this should be settable:
7784
find_tags = False
7885
skip_match = None
@@ -99,19 +106,22 @@ def run(self, options, args):
99106

100107
only_dists = []
101108
if args:
109+
# Freeze only a subset of installed packages.
102110
only_dists = args
103-
only_dists.extend(get_recursive_dependencies(*only_dists))
111+
if recursive: # Freeze dependencies, recursively.
112+
only_dists.extend(get_recursive_dependencies(*only_dists))
104113
only_dists = [name.lower() for name in only_dists]
105114

106115
for dist in get_installed_distributions(local_only=local_only,
107116
skip=freeze_excludes,
108117
user_only=user_only):
109-
req = pip.FrozenRequirement.from_dist(
110-
dist,
111-
dependency_links,
112-
find_tags=find_tags,
113-
)
114-
installations[req.name] = req
118+
if not only_dists or dist.project_name.lower() in only_dists:
119+
req = pip.FrozenRequirement.from_dist(
120+
dist,
121+
dependency_links,
122+
find_tags=find_tags,
123+
)
124+
installations[req.name] = req
115125
if requirement:
116126
req_f = open(requirement)
117127
for line in req_f:

tests/functional/test_freeze.py

+56
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,59 @@ def test_freeze_user(script, virtualenv):
385385
<BLANKLINE>""")
386386
_check_output(result, expected)
387387
assert 'simple2' not in result.stdout
388+
389+
390+
def test_freeze_distributions(script):
391+
"""Test that output is filtered if arguments are specified."""
392+
# Install some distributions.
393+
script.scratch_path.join("initools-req.txt").write(textwrap.dedent("""\
394+
simple==2.0
395+
requiresupper==1.0
396+
"""))
397+
script.pip_install_local(
398+
'-r', script.scratch_path / 'initools-req.txt',
399+
)
400+
# Freeze a single package.
401+
result = script.pip('freeze', 'simple', expect_stderr=True)
402+
expected = textwrap.dedent("""\
403+
Script result: pip freeze simple
404+
-- stdout: --------------------
405+
simple==2.0
406+
<BLANKLINE>""")
407+
_check_output(result, expected)
408+
# Freeze a single package does not show dependencies by default.
409+
result = script.pip('freeze', 'requiresupper', expect_stderr=True)
410+
expected = textwrap.dedent("""\
411+
Script result: pip freeze requiresupper
412+
-- stdout: --------------------
413+
requiresupper==1.0
414+
<BLANKLINE>""")
415+
_check_output(result, expected)
416+
# Freeze multiple packages.
417+
result = script.pip('freeze', 'simple', 'requiresupper',
418+
expect_stderr=True)
419+
expected = textwrap.dedent("""\
420+
Script result: pip freeze simple requiresupper
421+
-- stdout: --------------------
422+
requiresupper==1.0
423+
simple==2.0
424+
<BLANKLINE>""")
425+
_check_output(result, expected)
426+
# Freeze is case insensitive.
427+
result = script.pip('freeze', 'SiMPLe', expect_stderr=True)
428+
expected = textwrap.dedent("""\
429+
Script result: pip freeze SiMPLe
430+
-- stdout: --------------------
431+
simple==2.0
432+
<BLANKLINE>""")
433+
_check_output(result, expected)
434+
# Freeze is optionally recursive.
435+
result = script.pip('freeze', '--recursive', 'requiresupper',
436+
expect_stderr=True)
437+
expected = textwrap.dedent("""\
438+
Script result: pip freeze --recursive requiresupper
439+
-- stdout: --------------------
440+
requiresupper==1.0
441+
Upper==2.0
442+
<BLANKLINE>""")
443+
_check_output(result, expected)

0 commit comments

Comments
 (0)