|
8 | 8 | from zipfile import ZipFile
|
9 | 9 |
|
10 | 10 | from pip._vendor.packaging.utils import canonicalize_name
|
| 11 | +from pip._vendor.pkg_resources import DistInfoDistribution |
11 | 12 | from pip._vendor.six import PY2, ensure_str
|
12 | 13 |
|
13 | 14 | from pip._internal.exceptions import UnsupportedWheel
|
| 15 | +from pip._internal.utils.pkg_resources import DictMetadata |
14 | 16 | from pip._internal.utils.typing import MYPY_CHECK_RUNNING
|
15 | 17 |
|
16 | 18 | if MYPY_CHECK_RUNNING:
|
17 | 19 | from email.message import Message
|
18 |
| - from typing import Tuple |
| 20 | + from typing import Dict, Tuple |
| 21 | + |
| 22 | + from pip._vendor.pkg_resources import Distribution |
19 | 23 |
|
20 | 24 | if PY2:
|
21 | 25 | from zipfile import BadZipfile as BadZipFile
|
|
29 | 33 | logger = logging.getLogger(__name__)
|
30 | 34 |
|
31 | 35 |
|
| 36 | +def pkg_resources_distribution_for_wheel(wheel_zip, name, location): |
| 37 | + # type: (ZipFile, str, str) -> Distribution |
| 38 | + """Get a pkg_resources distribution given a wheel. |
| 39 | +
|
| 40 | + :raises UnsupportedWheel on any errors |
| 41 | + """ |
| 42 | + info_dir, _ = parse_wheel(wheel_zip, name) |
| 43 | + |
| 44 | + metadata_files = [ |
| 45 | + p for p in wheel_zip.namelist() if p.startswith("{}/".format(info_dir)) |
| 46 | + ] |
| 47 | + |
| 48 | + metadata_text = {} # type: Dict[str, str] |
| 49 | + for path in metadata_files: |
| 50 | + # If a flag is set, namelist entries may be unicode in Python 2. |
| 51 | + # We coerce them to native str type to match the types used in the rest |
| 52 | + # of the code. This cannot fail because unicode can always be encoded |
| 53 | + # with UTF-8. |
| 54 | + full_path = ensure_str(path) |
| 55 | + _, metadata_name = full_path.split("/", 1) |
| 56 | + |
| 57 | + try: |
| 58 | + metadata_text[metadata_name] = read_wheel_metadata_file( |
| 59 | + wheel_zip, full_path |
| 60 | + ) |
| 61 | + except UnsupportedWheel as e: |
| 62 | + raise UnsupportedWheel( |
| 63 | + "{} has an invalid wheel, {}".format(name, str(e)) |
| 64 | + ) |
| 65 | + |
| 66 | + metadata = DictMetadata(metadata_text) |
| 67 | + |
| 68 | + return DistInfoDistribution( |
| 69 | + location=location, metadata=metadata, project_name=name |
| 70 | + ) |
| 71 | + |
| 72 | + |
32 | 73 | def parse_wheel(wheel_zip, name):
|
33 | 74 | # type: (ZipFile, str) -> Tuple[str, Message]
|
34 | 75 | """Extract information from the provided wheel, ensuring it meets basic
|
|
0 commit comments