Skip to content

Commit c4163fe

Browse files
committed
feat: implement strip mode
Strips all `.py{c,o}`, `RECORD`, `direct_url.json`, and `__pycache__` files to ensure reproducible builds.
1 parent c5613ab commit c4163fe

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

package_python_function/packager.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
class Packager:
1515
AWS_LAMBDA_MAX_UNZIP_SIZE = 262_144_000
16+
DIRS_NON_GRATA= ["__pycache__"]
17+
EXTENSIONS_NON_GRATA= [".pyc", ".pyo"]
18+
FILES_NON_GRATA= ["RECORD", "direct_url.json"]
1619

1720
def __init__(self, venv_path: Path, project_path: Path, output_dir: Path, output_file: Path | None):
1821
self.project = PythonProject(project_path)
@@ -42,13 +45,21 @@ def zip_all_dependencies(self, target_path: Path) -> None:
4245
logger.info(f"Zipping to {target_path}...")
4346

4447
with ZipFile(target_path, "w", ZIP_DEFLATED) as zip_file:
48+
4549
def zip_dir(path: Path) -> None:
4650
for item in path.iterdir():
4751
if item.is_dir():
48-
zip_dir(item)
52+
if item.name not in self.DIRS_NON_GRATA:
53+
zip_dir(item)
4954
else:
50-
self._uncompressed_bytes += item.stat().st_size
51-
zip_file.write_reproducibly(item, item.relative_to(self.input_path))
55+
if (
56+
item.name not in self.FILES_NON_GRATA
57+
and item.suffix not in self.EXTENSIONS_NON_GRATA
58+
):
59+
self._uncompressed_bytes += item.stat().st_size
60+
zip_file.write_reproducibly(
61+
item, item.relative_to(self.input_path)
62+
)
5263

5364
zip_dir(self.input_path)
5465

0 commit comments

Comments
 (0)