7
7
from pip .log import logger
8
8
from pip .basecommand import Command
9
9
from pip .util import get_installed_distributions
10
+ from pip ._vendor import pkg_resources
11
+
10
12
11
13
# packages to exclude from freeze output
12
14
freeze_excludes = stdlib_pkgs + ['setuptools' , 'pip' , 'distribute' ]
13
15
14
16
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
+
15
34
class FreezeCommand (Command ):
16
35
"""
17
36
Output installed packages in requirements format.
@@ -20,7 +39,7 @@ class FreezeCommand(Command):
20
39
"""
21
40
name = 'freeze'
22
41
usage = """
23
- %prog [options]"""
42
+ %prog [options] [PACKAGE PACKAGE...] """
24
43
summary = 'Output installed packages in requirements format.'
25
44
26
45
def __init__ (self , * args , ** kw ):
@@ -72,10 +91,19 @@ def run(self, options, args):
72
91
for link in find_links :
73
92
f .write ('-f %s\n ' % link )
74
93
installations = {}
94
+
95
+ only_dists = []
96
+ if args :
97
+ only_dists = args
98
+ only_dists .extend (recursive_dependencies (only_dists ))
99
+ only_dists = [name .lower () for name in only_dists ]
100
+
75
101
for dist in get_installed_distributions (local_only = local_only ,
76
102
skip = freeze_excludes ):
77
- req = pip .FrozenRequirement .from_dist (dist , find_tags = find_tags )
78
- installations [req .name ] = req
103
+ if not only_dists or dist .project_name .lower () in only_dists :
104
+ req = pip .FrozenRequirement .from_dist (
105
+ dist , find_tags = find_tags )
106
+ installations [req .name ] = req
79
107
if requirement :
80
108
req_f = open (requirement )
81
109
for line in req_f :
0 commit comments