Skip to content

Commit a2e93d6

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 a2e93d6

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_TO_EXCLUDE = ["__pycache__"]
17+
DIST_INFO_FILES_TO_EXCLUDE = ["RECORD", "direct_url.json"]
18+
EXTENSIONS_TO_EXCLUDE = [".pyc", ".pyo"]
1619

1720
def __init__(self, venv_path: Path, project_path: Path, output_dir: Path, output_file: Path | None):
1821
self.project = PythonProject(project_path)
@@ -45,10 +48,18 @@ def zip_all_dependencies(self, target_path: Path) -> None:
4548
def zip_dir(path: Path) -> None:
4649
for item in path.iterdir():
4750
if item.is_dir():
48-
zip_dir(item)
51+
if item.name not in self.DIRS_TO_EXCLUDE:
52+
zip_dir(item)
4953
else:
50-
self._uncompressed_bytes += item.stat().st_size
51-
zip_file.write_reproducibly(item, item.relative_to(self.input_path))
54+
excluded_by_extension = item.suffix in self.EXTENSIONS_TO_EXCLUDE
55+
excluded_dist_info_file = (
56+
item.name in self.DIST_INFO_FILES_TO_EXCLUDE
57+
if "dist-info" in item.parent.name
58+
else False
59+
)
60+
if not (excluded_by_extension or excluded_dist_info_file):
61+
self._uncompressed_bytes += item.stat().st_size
62+
zip_file.write_reproducibly(item, item.relative_to(self.input_path))
5263

5364
zip_dir(self.input_path)
5465

0 commit comments

Comments
 (0)