|
| 1 | +import zipfile |
| 2 | +import sys |
| 3 | +import argparse # For better command-line argument handling |
| 4 | + |
| 5 | + |
| 6 | +def list_modules_in_wheel(wheel_path): |
| 7 | + """ |
| 8 | + Lists all Python modules (.py files) contained within a wheel file. |
| 9 | +
|
| 10 | + Args: |
| 11 | + wheel_path (str): The path to the .whl file. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + list: A list of strings, where each string is the path to a .py file |
| 15 | + within the wheel archive. Returns None if an error occurs. |
| 16 | + """ |
| 17 | + modules = [] |
| 18 | + try: |
| 19 | + # Check if the file exists and is a file |
| 20 | + # (zipfile might raise different errors depending on the OS for directories) |
| 21 | + import os |
| 22 | + |
| 23 | + if not os.path.isfile(wheel_path): |
| 24 | + print( |
| 25 | + f"Error: Path '{wheel_path}' does not exist or is not a file.", |
| 26 | + file=sys.stderr, |
| 27 | + ) |
| 28 | + return None |
| 29 | + |
| 30 | + # Open the wheel file (which is essentially a zip archive) in read mode ('r') |
| 31 | + with zipfile.ZipFile(wheel_path, "r") as zf: |
| 32 | + # Get a list of all archive members (files and directories) |
| 33 | + all_files = zf.namelist() |
| 34 | + |
| 35 | + # Filter the list to include only files ending with '.py' |
| 36 | + modules = [name for name in all_files if name.endswith(".py")] |
| 37 | + |
| 38 | + return modules |
| 39 | + |
| 40 | + except zipfile.BadZipFile: |
| 41 | + print( |
| 42 | + f"Error: '{wheel_path}' is not a valid zip file or wheel file.", |
| 43 | + file=sys.stderr, |
| 44 | + ) |
| 45 | + return None |
| 46 | + except FileNotFoundError: |
| 47 | + # This might be redundant due to the os.path.isfile check, but good practice |
| 48 | + print(f"Error: File not found at '{wheel_path}'", file=sys.stderr) |
| 49 | + return None |
| 50 | + except Exception as e: |
| 51 | + print(f"An unexpected error occurred: {e}", file=sys.stderr) |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +# --- Example Usage (when running the script directly) --- |
| 56 | +if __name__ == "__main__": |
| 57 | + # Set up command-line argument parsing |
| 58 | + parser = argparse.ArgumentParser( |
| 59 | + description="List Python modules (.py files) found inside a wheel (.whl) file." |
| 60 | + ) |
| 61 | + parser.add_argument("wheel_file", help="The path to the .whl file to inspect.") |
| 62 | + args = parser.parse_args() |
| 63 | + |
| 64 | + # Get the list of modules |
| 65 | + module_list = list_modules_in_wheel(args.wheel_file) |
| 66 | + |
| 67 | + # Print the results |
| 68 | + if module_list is not None: # Check if the function execution was successful |
| 69 | + if module_list: |
| 70 | + print(f"Found {len(module_list)} module(s) in '{args.wheel_file}':") |
| 71 | + for module_path in module_list: |
| 72 | + print(f"- {module_path}") |
| 73 | + else: |
| 74 | + print(f"No .py modules found in '{args.wheel_file}'.") |
| 75 | + sys.exit(0) # Exit with success code |
| 76 | + else: |
| 77 | + sys.exit(1) # Exit with error code |
0 commit comments