Skip to content

Commit cc94c3a

Browse files
committed
'pip show {NAME}' also shows the list of recursive dependencies of package '{NAME}'.
1 parent 982a891 commit cc94c3a

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

pip/commands/freeze.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,13 @@
66
from pip.req import InstallRequirement
77
from pip.log import logger
88
from pip.basecommand import Command
9-
from pip.util import get_installed_distributions
10-
from pip._vendor import pkg_resources
9+
from pip.util import get_installed_distributions, recursive_dependencies
1110

1211

1312
# packages to exclude from freeze output
1413
freeze_excludes = stdlib_pkgs + ['setuptools', 'pip', 'distribute']
1514

1615

17-
def recursive_dependencies(query):
18-
"""Return list of dependencies of ``dists``, recursively."""
19-
dependencies = set()
20-
installed = dict(
21-
[(p.project_name.lower(), p) for p in pkg_resources.working_set])
22-
query_names = [name.lower() for name in query]
23-
for pkg in query_names:
24-
try:
25-
dist = installed[pkg]
26-
for dep in dist.requires():
27-
dependencies.add(dep.project_name)
28-
dependencies.update(recursive_dependencies([dep.project_name]))
29-
except KeyError:
30-
pass # pkg is not installed.
31-
return dependencies
32-
33-
3416
class FreezeCommand(Command):
3517
"""
3618
Output installed packages in requirements format.

pip/commands/show.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from pip.basecommand import Command
44
from pip.log import logger
5+
from pip.util import recursive_dependencies
56
from pip._vendor import pkg_resources
67

78

@@ -78,6 +79,8 @@ def print_results(distributions, list_all_files):
7879
logger.notify("Version: %s" % dist['version'])
7980
logger.notify("Location: %s" % dist['location'])
8081
logger.notify("Requires: %s" % ', '.join(dist['requires']))
82+
logger.notify("Requires recursive: %s" % ', '.join(
83+
recursive_dependencies([dist['name']])))
8184
if list_all_files:
8285
logger.notify("Files:")
8386
if dist['files'] is not None:

pip/util.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ def get_installed_distributions(local_only=True,
400400
]
401401

402402

403+
def recursive_dependencies(query):
404+
"""Return list of dependencies of dists in ``query``, recursively."""
405+
dependencies = set()
406+
installed = dict(
407+
[(p.project_name.lower(), p) for p in pkg_resources.working_set])
408+
query_names = [name.lower() for name in query]
409+
for pkg in query_names:
410+
try:
411+
dist = installed[pkg]
412+
for dep in dist.requires():
413+
dependencies.add(dep.project_name)
414+
dependencies.update(recursive_dependencies([dep.project_name]))
415+
except KeyError:
416+
pass # pkg is not installed.
417+
return dependencies
418+
419+
403420
def egg_link_path(dist):
404421
"""
405422
Return the path for the .egg-link file if it exists, otherwise, None.

tests/functional/test_show.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_show(script):
1515
assert lines[2] == 'Version: %s' % __version__, lines[2]
1616
assert lines[3].startswith('Location: '), lines[3]
1717
assert lines[4] == 'Requires: '
18+
assert lines[5] == 'Requires recursive: '
1819

1920

2021
def test_show_with_files_not_found(script, data):
@@ -32,6 +33,7 @@ def test_show_with_files_not_found(script, data):
3233
assert lines[2] == 'Version: 0.0.0', lines[2]
3334
assert lines[3].startswith('Location: '), lines[3]
3435
assert lines[4] == 'Requires: ', lines[4]
36+
assert lines[4] == 'Requires recursive: ', lines[5]
3537
assert lines[5] == 'Files:', lines[5]
3638
assert lines[6] == 'Cannot locate installed-files.txt', lines[6]
3739

0 commit comments

Comments
 (0)