Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/pip/commands/show.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
| author | bcclaywell |
|---|---|
| date | Mon, 12 Oct 2015 17:43:33 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d67268158946 |
|---|---|
| 1 from __future__ import absolute_import | |
| 2 | |
| 3 from email.parser import FeedParser | |
| 4 import logging | |
| 5 import os | |
| 6 | |
| 7 from pip.basecommand import Command | |
| 8 from pip.status_codes import SUCCESS, ERROR | |
| 9 from pip._vendor import pkg_resources | |
| 10 | |
| 11 | |
| 12 logger = logging.getLogger(__name__) | |
| 13 | |
| 14 | |
| 15 class ShowCommand(Command): | |
| 16 """Show information about one or more installed packages.""" | |
| 17 name = 'show' | |
| 18 usage = """ | |
| 19 %prog [options] <package> ...""" | |
| 20 summary = 'Show information about installed packages.' | |
| 21 | |
| 22 def __init__(self, *args, **kw): | |
| 23 super(ShowCommand, self).__init__(*args, **kw) | |
| 24 self.cmd_opts.add_option( | |
| 25 '-f', '--files', | |
| 26 dest='files', | |
| 27 action='store_true', | |
| 28 default=False, | |
| 29 help='Show the full list of installed files for each package.') | |
| 30 | |
| 31 self.parser.insert_option_group(0, self.cmd_opts) | |
| 32 | |
| 33 def run(self, options, args): | |
| 34 if not args: | |
| 35 logger.warning('ERROR: Please provide a package name or names.') | |
| 36 return ERROR | |
| 37 query = args | |
| 38 | |
| 39 results = search_packages_info(query) | |
| 40 if not print_results(results, options.files): | |
| 41 return ERROR | |
| 42 return SUCCESS | |
| 43 | |
| 44 | |
| 45 def search_packages_info(query): | |
| 46 """ | |
| 47 Gather details from installed distributions. Print distribution name, | |
| 48 version, location, and installed files. Installed files requires a | |
| 49 pip generated 'installed-files.txt' in the distributions '.egg-info' | |
| 50 directory. | |
| 51 """ | |
| 52 installed = dict( | |
| 53 [(p.project_name.lower(), p) for p in pkg_resources.working_set]) | |
| 54 query_names = [name.lower() for name in query] | |
| 55 for dist in [installed[pkg] for pkg in query_names if pkg in installed]: | |
| 56 package = { | |
| 57 'name': dist.project_name, | |
| 58 'version': dist.version, | |
| 59 'location': dist.location, | |
| 60 'requires': [dep.project_name for dep in dist.requires()], | |
| 61 } | |
| 62 file_list = None | |
| 63 metadata = None | |
| 64 if isinstance(dist, pkg_resources.DistInfoDistribution): | |
| 65 # RECORDs should be part of .dist-info metadatas | |
| 66 if dist.has_metadata('RECORD'): | |
| 67 lines = dist.get_metadata_lines('RECORD') | |
| 68 paths = [l.split(',')[0] for l in lines] | |
| 69 paths = [os.path.join(dist.location, p) for p in paths] | |
| 70 file_list = [os.path.relpath(p, dist.location) for p in paths] | |
| 71 | |
| 72 if dist.has_metadata('METADATA'): | |
| 73 metadata = dist.get_metadata('METADATA') | |
| 74 else: | |
| 75 # Otherwise use pip's log for .egg-info's | |
| 76 if dist.has_metadata('installed-files.txt'): | |
| 77 paths = dist.get_metadata_lines('installed-files.txt') | |
| 78 paths = [os.path.join(dist.egg_info, p) for p in paths] | |
| 79 file_list = [os.path.relpath(p, dist.location) for p in paths] | |
| 80 if dist.has_metadata('entry_points.txt'): | |
| 81 entry_points = dist.get_metadata_lines('entry_points.txt') | |
| 82 package['entry_points'] = entry_points | |
| 83 | |
| 84 if dist.has_metadata('PKG-INFO'): | |
| 85 metadata = dist.get_metadata('PKG-INFO') | |
| 86 | |
| 87 # @todo: Should pkg_resources.Distribution have a | |
| 88 # `get_pkg_info` method? | |
| 89 feed_parser = FeedParser() | |
| 90 feed_parser.feed(metadata) | |
| 91 pkg_info_dict = feed_parser.close() | |
| 92 for key in ('metadata-version', 'summary', | |
| 93 'home-page', 'author', 'author-email', 'license'): | |
| 94 package[key] = pkg_info_dict.get(key) | |
| 95 | |
| 96 # use and short-circuit to check for None | |
| 97 package['files'] = file_list and sorted(file_list) | |
| 98 yield package | |
| 99 | |
| 100 | |
| 101 def print_results(distributions, list_all_files): | |
| 102 """ | |
| 103 Print the informations from installed distributions found. | |
| 104 """ | |
| 105 results_printed = False | |
| 106 for dist in distributions: | |
| 107 results_printed = True | |
| 108 logger.info("---") | |
| 109 logger.info("Metadata-Version: %s" % dist.get('metadata-version')) | |
| 110 logger.info("Name: %s" % dist['name']) | |
| 111 logger.info("Version: %s" % dist['version']) | |
| 112 logger.info("Summary: %s" % dist.get('summary')) | |
| 113 logger.info("Home-page: %s" % dist.get('home-page')) | |
| 114 logger.info("Author: %s" % dist.get('author')) | |
| 115 logger.info("Author-email: %s" % dist.get('author-email')) | |
| 116 logger.info("License: %s" % dist.get('license')) | |
| 117 logger.info("Location: %s" % dist['location']) | |
| 118 logger.info("Requires: %s" % ', '.join(dist['requires'])) | |
| 119 if list_all_files: | |
| 120 logger.info("Files:") | |
| 121 if dist['files'] is not None: | |
| 122 for line in dist['files']: | |
| 123 logger.info(" %s" % line.strip()) | |
| 124 else: | |
| 125 logger.info("Cannot locate installed-files.txt") | |
| 126 if 'entry_points' in dist: | |
| 127 logger.info("Entry-points:") | |
| 128 for line in dist['entry_points']: | |
| 129 logger.info(" %s" % line.strip()) | |
| 130 return results_printed |
