diff options
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/commands/show.py')
| -rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/commands/show.py | 164 | 
1 files changed, 164 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/commands/show.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/commands/show.py new file mode 100644 index 0000000..bad9628 --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/commands/show.py  | |||
| @@ -0,0 +1,164 @@ | |||
| 1 | from __future__ import absolute_import | ||
| 2 | |||
| 3 | import logging | ||
| 4 | import os | ||
| 5 | from email.parser import FeedParser # type: ignore | ||
| 6 | |||
| 7 | from pip._vendor import pkg_resources | ||
| 8 | from pip._vendor.packaging.utils import canonicalize_name | ||
| 9 | |||
| 10 | from pip._internal.basecommand import Command | ||
| 11 | from pip._internal.status_codes import ERROR, SUCCESS | ||
| 12 | |||
| 13 | logger = logging.getLogger(__name__) | ||
| 14 | |||
| 15 | |||
| 16 | class ShowCommand(Command): | ||
| 17 | """Show information about one or more installed packages.""" | ||
| 18 | name = 'show' | ||
| 19 | usage = """ | ||
| 20 | %prog [options] <package> ...""" | ||
| 21 | summary = 'Show information about installed packages.' | ||
| 22 | ignore_require_venv = True | ||
| 23 | |||
| 24 | def __init__(self, *args, **kw): | ||
| 25 | super(ShowCommand, self).__init__(*args, **kw) | ||
| 26 | self.cmd_opts.add_option( | ||
| 27 | '-f', '--files', | ||
| 28 | dest='files', | ||
| 29 | action='store_true', | ||
| 30 | default=False, | ||
| 31 | help='Show the full list of installed files for each package.') | ||
| 32 | |||
| 33 | self.parser.insert_option_group(0, self.cmd_opts) | ||
| 34 | |||
| 35 | def run(self, options, args): | ||
| 36 | if not args: | ||
| 37 | logger.warning('ERROR: Please provide a package name or names.') | ||
| 38 | return ERROR | ||
| 39 | query = args | ||
| 40 | |||
| 41 | results = search_packages_info(query) | ||
| 42 | if not print_results( | ||
| 43 | results, list_files=options.files, verbose=options.verbose): | ||
| 44 | return ERROR | ||
| 45 | return SUCCESS | ||
| 46 | |||
| 47 | |||
| 48 | def search_packages_info(query): | ||
| 49 | """ | ||
| 50 | Gather details from installed distributions. Print distribution name, | ||
| 51 | version, location, and installed files. Installed files requires a | ||
| 52 | pip generated 'installed-files.txt' in the distributions '.egg-info' | ||
| 53 | directory. | ||
| 54 | """ | ||
| 55 | installed = {} | ||
| 56 | for p in pkg_resources.working_set: | ||
| 57 | installed[canonicalize_name(p.project_name)] = p | ||
| 58 | |||
| 59 | query_names = [canonicalize_name(name) for name in query] | ||
| 60 | |||
| 61 | for dist in [installed[pkg] for pkg in query_names if pkg in installed]: | ||
| 62 | package = { | ||
| 63 | 'name': dist.project_name, | ||
| 64 | 'version': dist.version, | ||
| 65 | 'location': dist.location, | ||
| 66 | 'requires': [dep.project_name for dep in dist.requires()], | ||
| 67 | } | ||
| 68 | file_list = None | ||
| 69 | metadata = None | ||
| 70 | if isinstance(dist, pkg_resources.DistInfoDistribution): | ||
| 71 | # RECORDs should be part of .dist-info metadatas | ||
| 72 | if dist.has_metadata('RECORD'): | ||
| 73 | lines = dist.get_metadata_lines('RECORD') | ||
| 74 | paths = [l.split(',')[0] for l in lines] | ||
| 75 | paths = [os.path.join(dist.location, p) for p in paths] | ||
| 76 | file_list = [os.path.relpath(p, dist.location) for p in paths] | ||
| 77 | |||
| 78 | if dist.has_metadata('METADATA'): | ||
| 79 | metadata = dist.get_metadata('METADATA') | ||
| 80 | else: | ||
| 81 | # Otherwise use pip's log for .egg-info's | ||
| 82 | if dist.has_metadata('installed-files.txt'): | ||
| 83 | paths = dist.get_metadata_lines('installed-files.txt') | ||
| 84 | paths = [os.path.join(dist.egg_info, p) for p in paths] | ||
| 85 | file_list = [os.path.relpath(p, dist.location) for p in paths] | ||
| 86 | |||
| 87 | if dist.has_metadata('PKG-INFO'): | ||
| 88 | metadata = dist.get_metadata('PKG-INFO') | ||
| 89 | |||
| 90 | if dist.has_metadata('entry_points.txt'): | ||
| 91 | entry_points = dist.get_metadata_lines('entry_points.txt') | ||
| 92 | package['entry_points'] = entry_points | ||
| 93 | |||
| 94 | if dist.has_metadata('INSTALLER'): | ||
| 95 | for line in dist.get_metadata_lines('INSTALLER'): | ||
| 96 | if line.strip(): | ||
| 97 | package['installer'] = line.strip() | ||
| 98 | break | ||
| 99 | |||
| 100 | # @todo: Should pkg_resources.Distribution have a | ||
| 101 | # `get_pkg_info` method? | ||
| 102 | feed_parser = FeedParser() | ||
| 103 | feed_parser.feed(metadata) | ||
| 104 | pkg_info_dict = feed_parser.close() | ||
| 105 | for key in ('metadata-version', 'summary', | ||
| 106 | 'home-page', 'author', 'author-email', 'license'): | ||
| 107 | package[key] = pkg_info_dict.get(key) | ||
| 108 | |||
| 109 | # It looks like FeedParser cannot deal with repeated headers | ||
| 110 | classifiers = [] | ||
| 111 | for line in metadata.splitlines(): | ||
| 112 | if line.startswith('Classifier: '): | ||
| 113 | classifiers.append(line[len('Classifier: '):]) | ||
| 114 | package['classifiers'] = classifiers | ||
| 115 | |||
| 116 | if file_list: | ||
| 117 | package['files'] = sorted(file_list) | ||
| 118 | yield package | ||
| 119 | |||
| 120 | |||
| 121 | def print_results(distributions, list_files=False, verbose=False): | ||
| 122 | """ | ||
| 123 | Print the informations from installed distributions found. | ||
| 124 | """ | ||
| 125 | results_printed = False | ||
| 126 | for i, dist in enumerate(distributions): | ||
| 127 | results_printed = True | ||
| 128 | if i > 0: | ||
| 129 | logger.info("---") | ||
| 130 | |||
| 131 | name = dist.get('name', '') | ||
| 132 | required_by = [ | ||
| 133 | pkg.project_name for pkg in pkg_resources.working_set | ||
| 134 | if name in [required.name for required in pkg.requires()] | ||
| 135 | ] | ||
| 136 | |||
| 137 | logger.info("Name: %s", name) | ||
| 138 | logger.info("Version: %s", dist.get('version', '')) | ||
| 139 | logger.info("Summary: %s", dist.get('summary', '')) | ||
| 140 | logger.info("Home-page: %s", dist.get('home-page', '')) | ||
| 141 | logger.info("Author: %s", dist.get('author', '')) | ||
| 142 | logger.info("Author-email: %s", dist.get('author-email', '')) | ||
| 143 | logger.info("License: %s", dist.get('license', '')) | ||
| 144 | logger.info("Location: %s", dist.get('location', '')) | ||
| 145 | logger.info("Requires: %s", ', '.join(dist.get('requires', []))) | ||
| 146 | logger.info("Required-by: %s", ', '.join(required_by)) | ||
| 147 | |||
| 148 | if verbose: | ||
| 149 | logger.info("Metadata-Version: %s", | ||
| 150 | dist.get('metadata-version', '')) | ||
| 151 | logger.info("Installer: %s", dist.get('installer', '')) | ||
| 152 | logger.info("Classifiers:") | ||
| 153 | for classifier in dist.get('classifiers', []): | ||
| 154 | logger.info(" %s", classifier) | ||
| 155 | logger.info("Entry-points:") | ||
| 156 | for entry in dist.get('entry_points', []): | ||
| 157 | logger.info(" %s", entry.strip()) | ||
| 158 | if list_files: | ||
| 159 | logger.info("Files:") | ||
| 160 | for line in dist.get('files', []): | ||
| 161 | logger.info(" %s", line.strip()) | ||
| 162 | if "files" not in dist: | ||
| 163 | logger.info("Cannot locate installed-files.txt") | ||
| 164 | return results_printed | ||
