pip/pip/commands/status.py

71 lines
2.2 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
class StatusCommand(Command):
name = 'status'
usage = '%prog QUERY'
summary = 'Output installed distributions (exact versions, files) to stdout'
def __init__(self):
super(StatusCommand, self).__init__()
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)
print_results(results)
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.
"""
2012-09-07 23:44:49 +02:00
installed_packages = dict([(p.project_name.lower(), p.project_name) \
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:
2012-09-07 23:44:49 +02:00
dist = pkg_resources.get_distribution( \
installed_packages[normalized_name])
package = {
'name': dist.project_name,
'version': dist.version,
'location': dist.location
}
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
def print_results(distributions):
"""
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("Files:")
2012-09-08 00:18:48 +02:00
if 'files' in dist:
for line in open(dist['files']):
logger.notify(" %s" % line.strip())
else:
logger.notify("Cannot locate installed-files.txt")
2012-09-07 21:56:00 +02:00
StatusCommand()