|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build a test wheel with files that have no read permissions. |
| 3 | +
|
| 4 | +This simulates wheels that are created with incorrect file permissions, |
| 5 | +which can cause extraction failures when Bazel tries to read the files. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +import zipfile |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +def create_bad_perms_wheel(output_path: str): |
| 14 | + """Create a wheel file with files that have no read permissions.""" |
| 15 | + with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as whl: |
| 16 | + # Add __init__.py with no read permissions (mode 000) |
| 17 | + info = zipfile.ZipInfo('bad_perms_pkg/__init__.py') |
| 18 | + info.external_attr = 0o000 << 16 # No permissions |
| 19 | + whl.writestr(info, 'def test():\n return "hello"\n') |
| 20 | + |
| 21 | + # Add a module file with no read permissions |
| 22 | + module_info = zipfile.ZipInfo('bad_perms_pkg/module.py') |
| 23 | + module_info.external_attr = 0o000 << 16 # No permissions |
| 24 | + whl.writestr(module_info, 'VALUE = 42\n') |
| 25 | + |
| 26 | + # Add METADATA with no read permissions |
| 27 | + metadata = zipfile.ZipInfo('bad_perms_pkg-1.0.dist-info/METADATA') |
| 28 | + metadata.external_attr = 0o000 << 16 # No permissions |
| 29 | + whl.writestr(metadata, '''Metadata-Version: 2.1 |
| 30 | +Name: bad-perms-pkg |
| 31 | +Version: 1.0 |
| 32 | +Summary: Test package with bad file permissions |
| 33 | +Author: Test |
| 34 | +License: Apache-2.0 |
| 35 | +''') |
| 36 | + |
| 37 | + # Add WHEEL with normal permissions (so the wheel can be opened) |
| 38 | + wheel_info = zipfile.ZipInfo('bad_perms_pkg-1.0.dist-info/WHEEL') |
| 39 | + wheel_info.external_attr = 0o644 << 16 |
| 40 | + whl.writestr(wheel_info, '''Wheel-Version: 1.0 |
| 41 | +Generator: test |
| 42 | +Root-Is-Purelib: true |
| 43 | +Tag: py3-none-any |
| 44 | +''') |
| 45 | + |
| 46 | + # Add RECORD with normal permissions |
| 47 | + record = zipfile.ZipInfo('bad_perms_pkg-1.0.dist-info/RECORD') |
| 48 | + record.external_attr = 0o644 << 16 |
| 49 | + whl.writestr(record, '''bad_perms_pkg/__init__.py,, |
| 50 | +bad_perms_pkg/module.py,, |
| 51 | +bad_perms_pkg-1.0.dist-info/METADATA,, |
| 52 | +bad_perms_pkg-1.0.dist-info/WHEEL,, |
| 53 | +bad_perms_pkg-1.0.dist-info/RECORD,, |
| 54 | +''') |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == '__main__': |
| 58 | + if len(sys.argv) != 2: |
| 59 | + print(f"Usage: {sys.argv[0]} <output_wheel_path>", file=sys.stderr) |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + output_path = sys.argv[1] |
| 63 | + create_bad_perms_wheel(output_path) |
| 64 | + print(f"Created wheel with bad permissions: {output_path}") |
0 commit comments