|
| 1 | +import os |
1 | 2 | import pkginfo |
2 | 3 | import zipfile |
3 | 4 | import pkg_resources |
| 5 | +import glob |
| 6 | + |
| 7 | +from typing import Dict, Optional |
4 | 8 |
|
5 | 9 |
|
6 | 10 | class Wheel(object): |
@@ -37,3 +41,42 @@ def dependencies(self, extras_requested=None): |
37 | 41 | def unzip(self, directory): |
38 | 42 | with zipfile.ZipFile(self.path(), "r") as whl: |
39 | 43 | whl.extractall(directory) |
| 44 | + |
| 45 | + |
| 46 | +def get_dist_info(extracted_whl_directory) -> str: |
| 47 | + dist_info_dirs = glob.glob(os.path.join(extracted_whl_directory, "*.dist-info")) |
| 48 | + if not dist_info_dirs: |
| 49 | + raise ValueError(f"No *.dist-info directory found. {extracted_whl_directory} is not a valid Wheel.") |
| 50 | + elif len(dist_info_dirs) > 1: |
| 51 | + raise ValueError(f"Found more than 1 *.dist-info directory. {extracted_whl_directory} is not a valid Wheel.") |
| 52 | + else: |
| 53 | + dist_info = dist_info_dirs[0] |
| 54 | + return dist_info |
| 55 | + |
| 56 | + |
| 57 | +def get_dot_data_directory(extracted_whl_directory) -> Optional[str]: |
| 58 | + # See: https://www.python.org/dev/peps/pep-0491/#the-data-directory |
| 59 | + dot_data_dirs = glob.glob(os.path.join(extracted_whl_directory, "*.data")) |
| 60 | + if not dot_data_dirs: |
| 61 | + return None |
| 62 | + elif len(dot_data_dirs) > 1: |
| 63 | + raise ValueError(f"Found more than 1 *.data directory. {extracted_whl_directory} is not a valid Wheel.") |
| 64 | + else: |
| 65 | + dot_data_dir = dot_data_dirs[0] |
| 66 | + return dot_data_dir |
| 67 | + |
| 68 | + |
| 69 | +def parse_WHEEL_file(whl_file_path: str) -> Dict[str, str]: |
| 70 | + contents = {} |
| 71 | + with open(whl_file_path, "r") as f: |
| 72 | + for line in f: |
| 73 | + cleaned = line.strip() |
| 74 | + if not cleaned: |
| 75 | + continue |
| 76 | + try: |
| 77 | + key, value = cleaned.split(":", maxsplit=1) |
| 78 | + contents[key] = value.strip() |
| 79 | + except ValueError: |
| 80 | + raise RuntimeError(f"Encounted invalid line in WHEEL file: '{cleaned}'") |
| 81 | + return contents |
| 82 | + |
0 commit comments