|
2 | 2 | import logging
|
3 | 3 | import optparse
|
4 | 4 | import os
|
| 5 | +import pkg_resources |
5 | 6 | import sys
|
6 | 7 |
|
7 | 8 | from packaging.utils import canonicalize_name
|
8 |
| -from pip._internal.commands.show import search_packages_info |
| 9 | +#from pip._internal.commands.show import search_packages_info |
9 | 10 | from pip._internal.download import PipSession
|
10 | 11 | from pip._internal.req.req_file import parse_requirements
|
11 | 12 | from pip._internal.utils.misc import get_installed_distributions
|
|
15 | 16 | log = logging.getLogger(__name__)
|
16 | 17 |
|
17 | 18 |
|
| 19 | +def search_packages_info(query): |
| 20 | + """ |
| 21 | + Gather details from installed distributions. Print distribution name, |
| 22 | + version, location, and installed files. Installed files requires a |
| 23 | + pip generated 'installed-files.txt' in the distributions '.egg-info' |
| 24 | + directory. |
| 25 | + """ |
| 26 | + installed = {} |
| 27 | + for p in pkg_resources.working_set: |
| 28 | + installed[canonicalize_name(p.project_name)] = p |
| 29 | + |
| 30 | + query_names = [canonicalize_name(name) for name in query] |
| 31 | + |
| 32 | + for dist in [installed[pkg] for pkg in query_names if pkg in installed]: |
| 33 | + package = { |
| 34 | + 'name': dist.project_name, |
| 35 | + 'version': dist.version, |
| 36 | + 'location': dist.location, |
| 37 | + 'requires': [dep.project_name for dep in dist.requires()], |
| 38 | + } |
| 39 | + file_list = None |
| 40 | + if isinstance(dist, pkg_resources.DistInfoDistribution): |
| 41 | + # RECORDs should be part of .dist-info metadatas |
| 42 | + if dist.has_metadata('RECORD'): |
| 43 | + lines = dist.get_metadata_lines('RECORD') |
| 44 | + paths = [l.split(',')[0] for l in lines] |
| 45 | + paths = [os.path.join(dist.location, p) for p in paths] |
| 46 | + file_list = [os.path.relpath(p, dist.location) for p in paths] |
| 47 | + else: |
| 48 | + # Otherwise use pip's log for .egg-info's |
| 49 | + if dist.has_metadata('installed-files.txt'): |
| 50 | + paths = dist.get_metadata_lines('installed-files.txt') |
| 51 | + paths = [os.path.join(dist.egg_info, p) for p in paths] |
| 52 | + file_list = [os.path.relpath(p, dist.location) for p in paths] |
| 53 | + |
| 54 | + if file_list: |
| 55 | + package['files'] = sorted(file_list) |
| 56 | + yield package |
| 57 | + |
| 58 | + |
18 | 59 | def find_missing_reqs(options):
|
19 | 60 | # 1. find files used by imports in the code (as best we can without
|
20 | 61 | # executing)
|
|
0 commit comments