3
from pip.basecommand import Command
4
from pip.log import logger
7
class ShowCommand(Command):
8
"""Show information about one or more installed packages."""
11
%prog [options] <package> ..."""
12
summary = 'Show information about installed packages.'
14
def __init__(self, *args, **kw):
15
super(ShowCommand, self).__init__(*args, **kw)
16
self.cmd_opts.add_option(
21
help='Show the full list of installed files for each package.')
23
self.parser.insert_option_group(0, self.cmd_opts)
25
def run(self, options, args):
27
logger.warn('ERROR: Please provide a package name or names.')
31
results = search_packages_info(query)
32
print_results(results, options.files)
35
def search_packages_info(query):
37
Gather details from installed distributions. Print distribution name,
38
version, location, and installed files. Installed files requires a
39
pip generated 'installed-files.txt' in the distributions '.egg-info'
42
installed_packages = dict(
43
[(p.project_name.lower(), p) for p in pkg_resources.working_set])
45
normalized_name = name.lower()
46
if normalized_name in installed_packages:
47
dist = installed_packages[normalized_name]
49
'name': dist.project_name,
50
'version': dist.version,
51
'location': dist.location,
52
'requires': [dep.project_name for dep in dist.requires()],
54
filelist = os.path.join(
56
dist.egg_name() + '.egg-info',
57
'installed-files.txt')
58
if os.path.isfile(filelist):
59
package['files'] = filelist
63
def print_results(distributions, list_all_files):
65
Print the informations from installed distributions found.
67
for dist in distributions:
69
logger.notify("Name: %s" % dist['name'])
70
logger.notify("Version: %s" % dist['version'])
71
logger.notify("Location: %s" % dist['location'])
72
logger.notify("Requires: %s" % ', '.join(dist['requires']))
74
logger.notify("Files:")
76
for line in open(dist['files']):
77
logger.notify(" %s" % line.strip())
79
logger.notify("Cannot locate installed-files.txt")