Skip to content

Commit 5f48290

Browse files
committed
WIP: buf build
1 parent ecb686b commit 5f48290

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

buf-build/list_wheel_modules.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

buf-build/uv_buf_build_script.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,9 @@ mkdir -p "${TEST_ROOT}" || { echo "Unable to create test root directory" ; exit
9696
cd "${TEST_ROOT}" || { echo "Unable to change to test root directory" ; exit 1; }
9797
uv venv .venv-wheel
9898
source "${TEST_ROOT}/.venv-wheel/bin/activate"
99+
100+
echo "Ensuring wheel can be installed"
99101
uv pip install ${BUILD_ROOT}/dist/*.whl || { echo "Failed to install wheel" ; exit 1; }
102+
103+
loud_print "Listing all wheel modules"
104+
uvx python list_wheel_modules.py ${BUILD_ROOT}/dist/*.whl || { echo "Failed to list wheel modules" ; exit 1; }

0 commit comments

Comments
 (0)