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
|
|
|
|
|
2012-09-07 23:17:04 +02:00
|
|
|
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
|
|
|
|
2012-09-07 23:17:04 +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.
|
|
|
|
"""
|
2012-09-21 19:15:24 +02:00
|
|
|
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:
|
2012-09-07 23:17:04 +02:00
|
|
|
normalized_name = name.lower()
|
|
|
|
if normalized_name in installed_packages:
|
2012-09-21 19:15:24 +02:00
|
|
|
dist = installed_packages[normalized_name]
|
2012-09-07 23:17:04 +02:00
|
|
|
package = {
|
|
|
|
'name': dist.project_name,
|
|
|
|
'version': dist.version,
|
2012-09-21 19:15:24 +02:00
|
|
|
'location': dist.location,
|
|
|
|
'requires': [dep.project_name for dep in dist.requires()],
|
2012-09-07 23:17:04 +02:00
|
|
|
}
|
2012-09-07 21:56:00 +02:00
|
|
|
filelist = os.path.join(
|
2012-09-07 23:17:04 +02:00
|
|
|
dist.location,
|
|
|
|
dist.egg_name() + '.egg-info',
|
|
|
|
'installed-files.txt')
|
2012-09-07 21:56:00 +02:00
|
|
|
if os.path.isfile(filelist):
|
2012-09-07 23:17:04 +02:00
|
|
|
package['files'] = filelist
|
|
|
|
yield package
|
|
|
|
|
|
|
|
|
2012-09-11 22:44:42 +02:00
|
|
|
def print_results(distributions, list_all_files):
|
2012-09-07 23:17:04 +02:00
|
|
|
"""
|
|
|
|
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'])
|
2012-09-21 19:15:24 +02:00
|
|
|
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")
|