The InWheelCtx context manager allows you to unpack a wheel, perform modifications on its contents in a temporary directory, and automatically repack it into a new wheel file upon exit.
Key features:
- Automatic Repacking: If
out_wheel is provided, the context manager rewrites the RECORD file (to ensure hashes match the new contents) and zips the directory back into a .whl file. - File Iteration: Use
iter_files() to iterate over the files listed in the wheel's RECORD file. - Context Return: Unlike the standard
InWheel, InWheelCtx returns itself from __enter__, allowing you to configure properties like out_wheel after entering the context.
Note: If you modify files, the RECORD file is automatically updated to reflect new hashes and file sizes, and any existing RECORD.jws signatures are removed to prevent invalidation errors.
from auditwheel.wheeltools import InWheelCtx
from pathlib import Path
in_wheel = Path("my_package-1.0-cp39-cp39-linux_x86_64.whl")
with InWheelCtx(in_wheel) as ctx:
# Set the output path after entering the context
ctx.out_wheel = Path("my_package-1.0-cp39-cp39-manylinux_2_28_x86_64.whl")
# Iterate through files listed in the RECORD
for file_path in ctx.iter_files():
print(f"Processing {file_path}")
# Perform modifications here...
# Upon exiting, the new wheel is written to ctx.out_wheel