|
| 1 | +""" |
| 2 | +Display information about wheel files. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import email.policy |
| 8 | +import sys |
| 9 | +from email.parser import BytesParser |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from ..wheelfile import WheelFile |
| 13 | + |
| 14 | + |
| 15 | +def info(path: str, verbose: bool = False) -> None: |
| 16 | + """Display information about a wheel file. |
| 17 | +
|
| 18 | + :param path: The path to the wheel file |
| 19 | + :param verbose: Show detailed file listing |
| 20 | + """ |
| 21 | + wheel_path = Path(path) |
| 22 | + if not wheel_path.exists(): |
| 23 | + raise FileNotFoundError(f"Wheel file not found: {path}") |
| 24 | + |
| 25 | + with WheelFile(path) as wf: |
| 26 | + # Extract basic wheel information from filename |
| 27 | + parsed = wf.parsed_filename |
| 28 | + name = parsed.group("name") |
| 29 | + version = parsed.group("ver") |
| 30 | + build_tag = parsed.group("build") |
| 31 | + |
| 32 | + print(f"Name: {name}") |
| 33 | + print(f"Version: {version}") |
| 34 | + if build_tag: |
| 35 | + print(f"Build: {build_tag}") |
| 36 | + |
| 37 | + # Read WHEEL metadata |
| 38 | + try: |
| 39 | + with wf.open(f"{wf.dist_info_path}/WHEEL") as wheel_file: |
| 40 | + wheel_metadata = BytesParser(policy=email.policy.compat32).parse( |
| 41 | + wheel_file |
| 42 | + ) |
| 43 | + |
| 44 | + print( |
| 45 | + f"Wheel-Version: {wheel_metadata.get('Wheel-Version', 'Unknown')}" |
| 46 | + ) |
| 47 | + print( |
| 48 | + f"Root-Is-Purelib: {wheel_metadata.get('Root-Is-Purelib', 'Unknown')}" |
| 49 | + ) |
| 50 | + |
| 51 | + # Get all tags |
| 52 | + tags = wheel_metadata.get_all("Tag", []) |
| 53 | + if tags: |
| 54 | + print("Tags:") |
| 55 | + for tag in sorted(tags): # Sort tags for consistent output |
| 56 | + print(f" {tag}") |
| 57 | + |
| 58 | + generators = wheel_metadata.get_all("Generator", []) |
| 59 | + for generator in generators: |
| 60 | + print(f"Generator: {generator}") |
| 61 | + except KeyError: |
| 62 | + print("Warning: WHEEL metadata file not found", file=sys.stderr) |
| 63 | + |
| 64 | + # Read package METADATA |
| 65 | + try: |
| 66 | + with wf.open(f"{wf.dist_info_path}/METADATA") as metadata_file: |
| 67 | + pkg_metadata = BytesParser(policy=email.policy.compat32).parse( |
| 68 | + metadata_file |
| 69 | + ) |
| 70 | + |
| 71 | + summary = pkg_metadata.get("Summary", "") |
| 72 | + if summary and summary != "UNKNOWN": |
| 73 | + print(f"Summary: {summary}") |
| 74 | + |
| 75 | + author = pkg_metadata.get("Author", "") |
| 76 | + if author and author != "UNKNOWN": |
| 77 | + print(f"Author: {author}") |
| 78 | + |
| 79 | + author_email = pkg_metadata.get("Author-email") |
| 80 | + if author_email and author_email != "UNKNOWN": |
| 81 | + print(f"Author-email: {author_email}") |
| 82 | + |
| 83 | + homepage = pkg_metadata.get("Home-page") |
| 84 | + if homepage and homepage != "UNKNOWN": |
| 85 | + print(f"Home-page: {homepage}") |
| 86 | + |
| 87 | + license_info = pkg_metadata.get("License") |
| 88 | + if license_info and license_info != "UNKNOWN": |
| 89 | + print(f"License: {license_info}") |
| 90 | + |
| 91 | + # Show classifiers |
| 92 | + classifiers = pkg_metadata.get_all("Classifier", []) |
| 93 | + if classifiers: |
| 94 | + print("Classifiers:") |
| 95 | + for classifier in sorted( |
| 96 | + classifiers[:5] |
| 97 | + ): # Sort and limit to first 5 |
| 98 | + print(f" {classifier}") |
| 99 | + |
| 100 | + if len(classifiers) > 5: |
| 101 | + print(f" ... and {len(classifiers) - 5} more") |
| 102 | + |
| 103 | + # Show dependencies |
| 104 | + requires_dist = pkg_metadata.get_all("Requires-Dist", []) |
| 105 | + if requires_dist: |
| 106 | + print("Requires-Dist:") |
| 107 | + for req in sorted(requires_dist): # Sort dependencies |
| 108 | + print(f" {req}") |
| 109 | + except KeyError: |
| 110 | + print("Warning: METADATA file not found", file=sys.stderr) |
| 111 | + |
| 112 | + # File information |
| 113 | + file_count = len(wf.filelist) |
| 114 | + total_size = sum(zinfo.file_size for zinfo in wf.filelist) |
| 115 | + |
| 116 | + print(f"Files: {file_count}") |
| 117 | + print(f"Size: {total_size:,} bytes") |
| 118 | + |
| 119 | + # Show file listing if verbose |
| 120 | + if verbose: |
| 121 | + print("\nFile listing:") |
| 122 | + for zinfo in wf.filelist: |
| 123 | + size_str = f"{zinfo.file_size:,}" if zinfo.file_size > 0 else "0" |
| 124 | + print(f" {zinfo.filename:60} {size_str:>10} bytes") |
0 commit comments