2012-09-07 21:56:00 +02:00
|
|
|
import os
|
2014-01-07 10:47:04 +01:00
|
|
|
|
2012-09-07 21:56:00 +02:00
|
|
|
from pip.basecommand import Command
|
|
|
|
from pip.log import logger
|
2014-01-07 10:47:04 +01:00
|
|
|
from pip._vendor import pkg_resources
|
2012-09-07 21:56:00 +02:00
|
|
|
|
|
|
|
|
2012-09-12 18:39:19 +02:00
|
|
|
class ShowCommand(Command):
|
2013-02-16 19:00:29 +01:00
|
|
|
"""Show information about one or more installed packages."""
|
2012-09-12 18:39:19 +02:00
|
|
|
name = 'show'
|
2013-01-18 22:25:15 +01:00
|
|
|
usage = """
|
|
|
|
%prog [options] <package> ..."""
|
|
|
|
summary = 'Show information about installed packages.'
|
2012-09-07 21:56:00 +02:00
|
|
|
|
2012-11-10 05:15:45 +01:00
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super(ShowCommand, self).__init__(*args, **kw)
|
2012-12-10 02:29:46 +01:00
|
|
|
self.cmd_opts.add_option(
|
2012-09-11 22:44:42 +02:00
|
|
|
'-f', '--files',
|
|
|
|
dest='files',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2013-01-18 22:25:15 +01:00
|
|
|
help='Show the full list of installed files for each package.')
|
2012-09-07 21:56:00 +02:00
|
|
|
|
2012-12-12 14:06:05 +01:00
|
|
|
self.parser.insert_option_group(0, self.cmd_opts)
|
2012-12-10 02:29:46 +01:00
|
|
|
|
2012-09-07 21:56:00 +02:00
|
|
|
def run(self, options, args):
|
|
|
|
if not args:
|
2013-01-18 22:25:15 +01:00
|
|
|
logger.warn('ERROR: Please provide a package 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.
|
|
|
|
"""
|
2014-03-12 06:31:41 +01:00
|
|
|
installed = dict(
|
2012-09-21 19:15:24 +02:00
|
|
|
[(p.project_name.lower(), p) for p in pkg_resources.working_set])
|
2014-03-12 06:31:41 +01:00
|
|
|
query_names = [name.lower() for name in query]
|
|
|
|
for dist in [installed[pkg] for pkg in query_names if pkg in installed]:
|
|
|
|
package = {
|
|
|
|
'name': dist.project_name,
|
|
|
|
'version': dist.version,
|
|
|
|
'location': dist.location,
|
|
|
|
'requires': [dep.project_name for dep in dist.requires()],
|
|
|
|
}
|
|
|
|
file_list = None
|
|
|
|
if isinstance(dist, pkg_resources.DistInfoDistribution):
|
|
|
|
# RECORDs should be part of .dist-info metadatas
|
|
|
|
if dist.has_metadata('RECORD'):
|
|
|
|
lines = dist.get_metadata_lines('RECORD')
|
|
|
|
paths = [l.split(',')[0] for l in lines]
|
|
|
|
paths = [os.path.join(dist.location, p) for p in paths]
|
2014-07-16 15:36:39 +02:00
|
|
|
file_list = [os.path.relpath(p, dist.location) for p in paths]
|
2014-03-12 06:31:41 +01:00
|
|
|
else:
|
|
|
|
# Otherwise use pip's log for .egg-info's
|
|
|
|
if dist.has_metadata('installed-files.txt'):
|
2014-07-16 15:36:39 +02:00
|
|
|
paths = dist.get_metadata_lines('installed-files.txt')
|
|
|
|
paths = [os.path.join(dist.egg_info, p) for p in paths]
|
|
|
|
file_list = [os.path.relpath(p, dist.location) for p in paths]
|
|
|
|
|
2014-03-12 06:31:41 +01:00
|
|
|
# use and short-circuit to check for None
|
|
|
|
package['files'] = file_list and sorted(file_list)
|
|
|
|
yield package
|
2012-09-07 23:17:04 +02:00
|
|
|
|
|
|
|
|
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:")
|
2014-03-12 06:31:41 +01:00
|
|
|
if dist['files'] is not None:
|
|
|
|
for line in dist['files']:
|
2012-09-11 22:44:42 +02:00
|
|
|
logger.notify(" %s" % line.strip())
|
|
|
|
else:
|
|
|
|
logger.notify("Cannot locate installed-files.txt")
|