pip/pip/commands/show.py

76 lines
2.5 KiB
Python
Raw Normal View History

2012-09-07 21:56:00 +02:00
import os
import pkg_resources
from pip.basecommand import Command
from pip.log import logger
2012-09-12 18:39:19 +02:00
class ShowCommand(Command):
name = 'show'
2012-09-07 21:56:00 +02:00
usage = '%prog QUERY'
summary = 'Output installed distributions (exact versions, files) to stdout'
def __init__(self):
2012-09-12 18:39:19 +02:00
super(ShowCommand, self).__init__()
2012-09-11 22:44:42 +02:00
self.parser.add_option(
'-f', '--files',
dest='files',
action='store_true',
default=False,
2012-09-12 18:39:19 +02:00
help='Show the full list of installed files for each package')
2012-09-07 21:56:00 +02:00
def run(self, options, args):
if not args:
2012-09-08 23:31:59 +02:00
logger.warn('ERROR: Please provide a project name or names.')
2012-09-07 21:56:00 +02:00
return
query = args
results = search_packages_info(query)
2012-09-11 22:44:42 +02:00
print_results(results, options.files)
2012-09-07 21:56:00 +02:00
2012-09-08 00:18:48 +02:00
def search_packages_info(query):
2012-09-07 21:56:00 +02:00
"""
Gather details from installed distributions. Print distribution name,
version, location, and installed files. Installed files requires a
pip generated 'installed-files.txt' in the distributions '.egg-info'
directory.
"""
installed_packages = dict(
[(p.project_name.lower(), p) for p in pkg_resources.working_set])
2012-09-07 21:56:00 +02:00
for name in query:
normalized_name = name.lower()
if normalized_name in installed_packages:
dist = installed_packages[normalized_name]
package = {
'name': dist.project_name,
'version': dist.version,
'location': dist.location,
'requires': [dep.project_name for dep in dist.requires()],
}
2012-09-07 21:56:00 +02:00
filelist = os.path.join(
dist.location,
dist.egg_name() + '.egg-info',
'installed-files.txt')
2012-09-07 21:56:00 +02:00
if os.path.isfile(filelist):
package['files'] = filelist
yield package
2012-09-11 22:44:42 +02:00
def print_results(distributions, list_all_files):
"""
Print the informations from installed distributions found.
"""
for dist in distributions:
logger.notify("---")
logger.notify("Name: %s" % dist['name'])
logger.notify("Version: %s" % dist['version'])
logger.notify("Location: %s" % dist['location'])
logger.notify("Requires: %s" % ', '.join(dist['requires']))
2012-09-11 22:44:42 +02:00
if list_all_files:
logger.notify("Files:")
if 'files' in dist:
for line in open(dist['files']):
logger.notify(" %s" % line.strip())
else:
logger.notify("Cannot locate installed-files.txt")