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
|
|
|
|
|
2012-09-07 23:17:04 +02:00
|
|
|
results = search_packages_info(query)
|
|
|
|
print_results(results)
|
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-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:
|
2012-09-07 23:17:04 +02:00
|
|
|
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])
|
2012-09-07 23:17:04 +02:00
|
|
|
package = {
|
|
|
|
'name': dist.project_name,
|
|
|
|
'version': dist.version,
|
|
|
|
'location': dist.location
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2012-09-11 21:35:03 +02:00
|
|
|
for line in open(dist['files']):
|
2012-09-07 23:17:04 +02:00
|
|
|
logger.notify(" %s" % line.strip())
|
|
|
|
else:
|
|
|
|
logger.notify("Cannot locate installed-files.txt")
|
2012-09-07 21:56:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
StatusCommand()
|