Skip to content

Commit fb38ff6

Browse files
eli-507 add clean lambda script
1 parent 1833e70 commit fb38ff6

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ build: dist/lambda.zip # Build lambda.zip in dist/
4444

4545
dist/lambda.zip: $(MAKE_DIR)/pyproject.toml $(MAKE_DIR)/poetry.lock $(shell find src -type f)
4646
poetry build-lambda -vv
47+
poetry run clean-lambda
4748

4849
deploy: # Deploy the project artefact to the target environment @Pipeline
4950
# TODO: Implement the artefact deployment step

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ docker-platform = "linux/x86_64"
7373
package-artifact-path = "dist/lambda.zip"
7474
without = "dev"
7575

76+
[tool.poetry.scripts]
77+
clean-lambda = "scripts.lambda.clean_lambda:main"
78+
7679
[tool.ruff]
7780
line-length = 120
7881
exclude = ["docs/", "scripts/"]

scripts/lambda/__init__.py

Whitespace-only changes.

scripts/lambda/clean_lambda.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import zipfile
2+
import os
3+
import shutil
4+
import tempfile
5+
import logging
6+
7+
ZIP_PATH = "dist/lambda.zip"
8+
9+
# Configure logging
10+
logging.basicConfig(
11+
level=logging.INFO,
12+
format="%(asctime)s [%(levelname)s] %(message)s"
13+
)
14+
logger = logging.getLogger(__name__)
15+
16+
# Folders to prune (only safe unused folders)
17+
BOTOCORE_SAFE_PRUNE = [
18+
"botocore/data/s3/tests",
19+
"botocore/data/glacier/tests",
20+
]
21+
22+
BOTO3_SAFE_PRUNE = [
23+
"boto3/examples"
24+
]
25+
26+
def main():
27+
if not os.path.exists(ZIP_PATH):
28+
logger.warning("ZIP file %s does not exist. Skipping cleanup.", ZIP_PATH)
29+
return
30+
31+
tmp_dir = tempfile.mkdtemp(prefix="lambda_clean_")
32+
logger.info("Unzipping %s...", ZIP_PATH)
33+
with zipfile.ZipFile(ZIP_PATH, "r") as z:
34+
z.extractall(tmp_dir)
35+
36+
# Remove only safe folders
37+
for folder_list in [BOTOCORE_SAFE_PRUNE, BOTO3_SAFE_PRUNE]:
38+
for rel in folder_list:
39+
target = os.path.join(tmp_dir, rel)
40+
if os.path.exists(target):
41+
logger.info("Removing %s", target)
42+
shutil.rmtree(target)
43+
44+
# Re-create the cleaned zip
45+
logger.info("Re-zipping cleaned Lambda package...")
46+
with zipfile.ZipFile(ZIP_PATH, "w", zipfile.ZIP_DEFLATED) as z:
47+
for root, _, files in os.walk(tmp_dir):
48+
for f in files:
49+
abs_path = os.path.join(root, f)
50+
rel_path = os.path.relpath(abs_path, tmp_dir)
51+
z.write(abs_path, rel_path)
52+
53+
shutil.rmtree(tmp_dir)
54+
logger.info("Cleaned Lambda saved at %s", ZIP_PATH)
55+
56+
if __name__ == "__main__":
57+
logger.info("Running clean_lambda.py...")
58+
main()

0 commit comments

Comments
 (0)