repro-lambda 0.8.1__tar.gz → 0.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. repro_lambda-0.9.0/.release-please-manifest.json +3 -0
  2. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/CHANGELOG.md +14 -0
  3. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/PKG-INFO +1 -1
  4. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/SETUP.md +17 -0
  5. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/USAGE.md +26 -3
  6. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/pyproject.toml +1 -1
  7. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/__init__.py +1 -1
  8. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/build.py +11 -6
  9. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/cli.py +18 -3
  10. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/docker_runner.py +8 -2
  11. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/hasher.py +12 -0
  12. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/manifest.py +14 -0
  13. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/verify.py +1 -0
  14. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/zip_packager.py +44 -0
  15. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_build_integration.py +3 -0
  16. repro_lambda-0.9.0/tests/test_compile_bytecode.py +114 -0
  17. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_extra_files.py +28 -0
  18. repro_lambda-0.9.0/tests/test_hash_contract.py +83 -0
  19. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/uv.lock +2 -2
  20. repro_lambda-0.8.1/.release-please-manifest.json +0 -3
  21. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.github/workflows/build.yml +0 -0
  22. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.github/workflows/ci.yml +0 -0
  23. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.github/workflows/move-major-tag.yml +0 -0
  24. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.github/workflows/promote.yml +0 -0
  25. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.github/workflows/publish.yml +0 -0
  26. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.github/workflows/release.yml +0 -0
  27. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.gitignore +0 -0
  28. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/.pre-commit-config.yaml +0 -0
  29. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/LICENSE +0 -0
  30. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/README.md +0 -0
  31. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/assets/social-preview.html +0 -0
  32. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/assets/social-preview.png +0 -0
  33. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/README.md +0 -0
  34. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/builds/catalog.json +0 -0
  35. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/lambdas.toml +0 -0
  36. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/main.tf +0 -0
  37. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/outputs.tf +0 -0
  38. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/src/app/app.py +0 -0
  39. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/src/app/requirements.arm64.lock +0 -0
  40. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/variables.tf +0 -0
  41. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/examples/complete/versions.tf +0 -0
  42. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/release-please-config.json +0 -0
  43. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/__main__.py +0 -0
  44. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/catalog.py +0 -0
  45. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/git_guard.py +0 -0
  46. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/promote.py +0 -0
  47. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/s3_uploader.py +0 -0
  48. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/source_locker.py +0 -0
  49. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/source_stager.py +0 -0
  50. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/src/repro_lambda/sources.py +0 -0
  51. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/__init__.py +0 -0
  52. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/conftest.py +0 -0
  53. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_nodejs_lambda/handler/index.js +0 -0
  54. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_nodejs_lambda/handler/package-lock.json +0 -0
  55. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_nodejs_lambda/handler/package.json +0 -0
  56. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_nodejs_lambda/lambdas.toml +0 -0
  57. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_python_lambda/handler/app.py +0 -0
  58. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_python_lambda/handler/requirements.arm64.lock +0 -0
  59. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_python_lambda/handler/requirements.in +0 -0
  60. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/fixtures/sample_python_lambda/lambdas.toml +0 -0
  61. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_build_nodejs.py +0 -0
  62. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_catalog.py +0 -0
  63. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_cli_build.py +0 -0
  64. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_cli_lock.py +0 -0
  65. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_cli_smoke.py +0 -0
  66. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_docker_runner.py +0 -0
  67. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_docker_runner_nodejs.py +0 -0
  68. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_e2e_nodejs_lambda.py +0 -0
  69. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_e2e_python_lambda.py +0 -0
  70. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_git_guard.py +0 -0
  71. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_hasher.py +0 -0
  72. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_manifest.py +0 -0
  73. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_per_lambda_builder.py +0 -0
  74. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_promote.py +0 -0
  75. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_python_byte_compat_regression.py +0 -0
  76. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_s3_uploader.py +0 -0
  77. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_source_locker.py +0 -0
  78. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_source_stager.py +0 -0
  79. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_sources.py +0 -0
  80. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_sources_hash.py +0 -0
  81. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_sources_schema.py +0 -0
  82. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_verify.py +0 -0
  83. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_zip_excludes.py +0 -0
  84. {repro_lambda-0.8.1 → repro_lambda-0.9.0}/tests/test_zip_packager.py +0 -0
@@ -0,0 +1,3 @@
1
+ {
2
+ ".": "0.9.0"
3
+ }
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.9.0](https://github.com/antonbabenko/repro-lambda/compare/v0.8.2...v0.9.0) (2026-10-30)
4
+
5
+
6
+ ### Features
7
+
8
+ * compile_bytecode ships checked-hash bytecode in python lambdas ([#22](https://github.com/antonbabenko/repro-lambda/issues/22)) ([47677cf](https://github.com/antonbabenko/repro-lambda/commit/47677cfdafe3832e5d0a570c5ce1fa7b509864c1))
9
+
10
+ ## [0.8.2](https://github.com/antonbabenko/repro-lambda/compare/v0.8.1...v0.8.2) (2026-09-23)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * **hasher:** stop re-keying artifacts on every repro-lambda release ([#19](https://github.com/antonbabenko/repro-lambda/issues/19)) ([44a263a](https://github.com/antonbabenko/repro-lambda/commit/44a263a560d6d7d3504a1365c4d2dac9c4dbad4f))
16
+
3
17
  ## [0.8.1](https://github.com/antonbabenko/repro-lambda/compare/v0.8.0...v0.8.1) (2026-09-23)
4
18
 
5
19
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: repro-lambda
3
- Version: 0.8.1
3
+ Version: 0.9.0
4
4
  Summary: Build reproducible AWS Lambda packages outside Terraform, optimized for terraform-aws-lambda by serverless.tf.
5
5
  Project-URL: Homepage, https://github.com/antonbabenko/repro-lambda
6
6
  Project-URL: Repository, https://github.com/antonbabenko/repro-lambda
@@ -32,6 +32,23 @@ Once an artifact with a content-hash key (`lambdas/<name>/<sha256>.zip`) is
32
32
  uploaded, the key is permanently bound to those bytes. `repro-lambda` treats
33
33
  HTTP 412 PreconditionFailed on a duplicate upload as success.
34
34
 
35
+ The key hashes the build inputs (staged source, requirements lock, spec, base
36
+ image, filters, sources) plus a builder hash contract. It does not hash the
37
+ repro-lambda package version, so upgrading repro-lambda keeps every existing key
38
+ unless the release changes the packaged bytes, in which case it bumps
39
+ `HASH_CONTRACT` in `hasher.py` and says so in its changelog entry.
40
+ `tests/test_hash_contract.py` fingerprints the packaging modules, so a change to
41
+ them fails CI until the contract is reviewed.
42
+
43
+ `--verify` builds twice and compares the two results; it does not compare against
44
+ the object already in S3. On a cache hit the build catalog records
45
+ `hash-contract:<value>` as the builder, because the running version did not
46
+ build that object.
47
+
48
+ Releases up to 0.8.1 folded the package version into the key, so 0.8.1 would mint
49
+ keys of its own. 0.8.2 returns to the keys 0.8.0 minted. If a pin was taken from a
50
+ 0.8.1 build, it moves back to the 0.8.0 key on the next build, with identical bytes.
51
+
35
52
  ## Terraform - per-account bootstrap
36
53
 
37
54
  The Terraform below assumes you already have a configured AWS provider in the
@@ -63,6 +63,29 @@ Pin the `base_image_python` to a specific digest with `docker pull <image> &&
63
63
  docker inspect --format='{{index .RepoDigests 0}}' <image>` - never use a
64
64
  floating tag in production.
65
65
 
66
+ ### Shipping bytecode (`compile_bytecode`)
67
+
68
+ A Lambda's `/var/task` is read-only, so a package shipped without `.pyc` files is
69
+ recompiled from source by every new Python process that imports it. For a large
70
+ package (or a handler that starts Python subprocesses) that can cost seconds per
71
+ invocation. Set `compile_bytecode = true` on a pip `[[lambda]]` to compile the whole
72
+ package in the builder image (the runtime's own interpreter) and keep the bytecode
73
+ in the zip:
74
+
75
+ ```toml
76
+ [[lambda]]
77
+ logical_name = "app"
78
+ # ...
79
+ compile_bytecode = true
80
+ ```
81
+
82
+ The zip step removes any bytecode already in the tree, then runs `compileall` with
83
+ `--invalidation-mode checked-hash` (no source mtimes; the interpreter checks each
84
+ `.pyc` against its source hash on import) and `PYTHONHASHSEED=0`, recording file paths
85
+ as `/var/task/...` so the bytes do not depend on the staging directory. The zip stays
86
+ byte-reproducible. The flag is part of the content key (only when set, so lambdas
87
+ without it keep their keys). A file that does not compile ships as source only.
88
+
66
89
  ### Per-lambda builder overrides
67
90
 
68
91
  `[builder]` sets the defaults for every lambda. Any `[[lambda]]` may override
@@ -86,9 +109,9 @@ include_patterns = ["worker/**/*.py", "worker/**/*.json"]
86
109
  exclude_patterns = ["**/tests/**"]
87
110
  ```
88
111
 
89
- The resolved per-lambda builder (base-image digest + include/exclude lists +
90
- builder version) folds into the content hash, so changing an override re-keys
91
- that lambda's artifact while leaving the others untouched.
112
+ The resolved per-lambda builder (base-image digest + include/exclude lists)
113
+ folds into the content hash, so changing an override re-keys that lambda's
114
+ artifact while leaving the others untouched.
92
115
 
93
116
  ## Declarative sources - `[[lambda.source]]`
94
117
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "repro-lambda"
3
- version = "0.8.1"
3
+ version = "0.9.0"
4
4
  description = "Build reproducible AWS Lambda packages outside Terraform, optimized for terraform-aws-lambda by serverless.tf."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -1,3 +1,3 @@
1
1
  """repro-lambda — reproducible AWS Lambda packaging outside Terraform."""
2
2
 
3
- __version__ = "0.8.1" # x-release-please-version
3
+ __version__ = "0.9.0" # x-release-please-version
@@ -11,7 +11,7 @@ from pathlib import Path
11
11
  from repro_lambda import __version__
12
12
  from repro_lambda.catalog import Catalog, CatalogEntry
13
13
  from repro_lambda.docker_runner import build_nodejs_lambda, build_python_lambda
14
- from repro_lambda.hasher import compute_content_hash
14
+ from repro_lambda.hasher import HASH_CONTRACT, compute_content_hash
15
15
  from repro_lambda.manifest import BuilderConfig, LambdaSpec, resolve_builder
16
16
  from repro_lambda.s3_uploader import S3Uploader, UploadResult
17
17
  from repro_lambda.source_stager import stage_source
@@ -83,7 +83,7 @@ def compute_sha_for(
83
83
  requirements_lock=lock_path,
84
84
  spec=spec,
85
85
  base_image=primary_base_image,
86
- builder_version=__version__,
86
+ builder_version=HASH_CONTRACT,
87
87
  extra_files=extras,
88
88
  payload_exec=[(ef.dest, ef.executable) for ef in spec.extra_files],
89
89
  include_patterns=builder.include_patterns,
@@ -130,7 +130,7 @@ def build_one(
130
130
  requirements_lock=lock_path,
131
131
  spec=spec,
132
132
  base_image=primary_base_image,
133
- builder_version=__version__,
133
+ builder_version=HASH_CONTRACT,
134
134
  extra_files=extras,
135
135
  payload_exec=[(ef.dest, ef.executable) for ef in spec.extra_files],
136
136
  include_patterns=builder.include_patterns,
@@ -144,7 +144,7 @@ def build_one(
144
144
 
145
145
  uploader = S3Uploader(region=spec.region)
146
146
  if uploader.exists(bucket=target_bucket, key=bucket_key):
147
- _record(catalog, spec, sha, source_commit, builder)
147
+ _record(catalog, spec, sha, source_commit, builder, built=False)
148
148
  return BuildOutcome(BuildResult.CACHE_HIT, sha, bucket_key)
149
149
 
150
150
  # Cache miss only: fetch + verify + extract declarative sources into the staged
@@ -167,6 +167,7 @@ def build_one(
167
167
  base_image=builder.base_image_python,
168
168
  arch=spec.arch,
169
169
  python_version=spec.runtime.removeprefix("python"),
170
+ compile_bytecode=spec.compile_bytecode,
170
171
  )
171
172
  else: # npm
172
173
  # nodejs22.x -> "22"; nodejs20.x -> "20"
@@ -182,7 +183,7 @@ def build_one(
182
183
 
183
184
  result = uploader.upload(bucket=target_bucket, key=bucket_key, body_path=out_zip)
184
185
  assert result in {UploadResult.UPLOADED, UploadResult.ALREADY_PRESENT}
185
- _record(catalog, spec, sha, source_commit, builder)
186
+ _record(catalog, spec, sha, source_commit, builder, built=True)
186
187
  return BuildOutcome(BuildResult.BUILT_AND_UPLOADED, sha, bucket_key)
187
188
 
188
189
 
@@ -192,6 +193,8 @@ def _record(
192
193
  sha: str,
193
194
  source_commit: str,
194
195
  builder: BuilderConfig,
196
+ *,
197
+ built: bool,
195
198
  ) -> None:
196
199
  if spec.package_manager == "npm":
197
200
  primary_image = builder.base_image_nodejs
@@ -205,7 +208,9 @@ def _record(
205
208
  runtime=spec.runtime,
206
209
  arch=spec.arch,
207
210
  region=spec.region,
208
- builder_version=__version__,
211
+ # A cache hit did not build the object, and the key no longer carries the
212
+ # package version, so only the hash contract is known to match it.
213
+ builder_version=__version__ if built else f"hash-contract:{HASH_CONTRACT}",
209
214
  base_image_digest=primary_image.split("@", 1)[-1],
210
215
  built_at=datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ"),
211
216
  ),
@@ -381,18 +381,33 @@ _LAMBDA_ZIP_EXCLUDES = [
381
381
  ]
382
382
 
383
383
 
384
- def _zip_impl(src: Path, out: Path) -> None:
384
+ _BYTECODE_PATTERNS = ("*__pycache__*", "*.pyc")
385
+
386
+
387
+ def _zip_impl(src: Path, out: Path, compile_bytecode: bool = False) -> None:
385
388
  """Pack a directory into a deterministic zip (used inside container)."""
389
+ from repro_lambda.zip_packager import compile_bytecode as compile_tree
386
390
  from repro_lambda.zip_packager import pack_directory
387
391
 
388
- pack_directory(src, out, exclude_glob=_LAMBDA_ZIP_EXCLUDES)
392
+ excludes = _LAMBDA_ZIP_EXCLUDES
393
+ if compile_bytecode:
394
+ compile_tree(src)
395
+ excludes = [p for p in excludes if p not in _BYTECODE_PATTERNS]
396
+ pack_directory(src, out, exclude_glob=excludes)
389
397
 
390
398
 
391
399
  @app.command(name="zip")
392
400
  def zip_(
393
401
  src: Annotated[Path, typer.Option("--src", help="Staged package directory.")],
394
402
  out: Annotated[Path, typer.Option("--out", help="Output zip path.")],
403
+ compile_bytecode: Annotated[
404
+ bool,
405
+ typer.Option(
406
+ "--compile-bytecode",
407
+ help="Compile checked-hash bytecode with this interpreter and keep it in the zip.",
408
+ ),
409
+ ] = False,
395
410
  ) -> None:
396
411
  """Pack a directory into a deterministic zip (used inside container)."""
397
- _zip_impl(src, out)
412
+ _zip_impl(src, out, compile_bytecode)
398
413
  raise typer.Exit(0)
@@ -91,8 +91,11 @@ pip install \
91
91
 
92
92
  # Byte-output cleanup (caches + non-deterministic dist-info metadata) happens in the
93
93
  # Python zip step below, so this script needs no findutils/xargs (both absent from the
94
- # minimal AWS Lambda base images).
95
- python3 -m repro_lambda zip --src "$PKG" --out /out/lambda.zip
94
+ # minimal AWS Lambda base images). With COMPILE_BYTECODE=1 the zip step first compiles
95
+ # the tree with this image's interpreter (the Lambda runtime) and keeps the bytecode.
96
+ ZIP_ARGS=""
97
+ if [ "$COMPILE_BYTECODE" = "1" ]; then ZIP_ARGS="--compile-bytecode"; fi
98
+ python3 -m repro_lambda zip --src "$PKG" --out /out/lambda.zip $ZIP_ARGS
96
99
  """
97
100
 
98
101
  _NODEJS_INSTALL_SCRIPT = r"""
@@ -161,6 +164,7 @@ def build_python_lambda(
161
164
  base_image: str,
162
165
  arch: str,
163
166
  python_version: str,
167
+ compile_bytecode: bool = False,
164
168
  ) -> None:
165
169
  """v0.1-compatible: install + pack inside the Python container."""
166
170
  if arch not in ARCH_TO_PIP_PLATFORMS:
@@ -196,6 +200,8 @@ def build_python_lambda(
196
200
  f"PIP_ABI=cp{pyver_compact}",
197
201
  "-e",
198
202
  f"PIP_PYVER={pyver}",
203
+ "-e",
204
+ f"COMPILE_BYTECODE={int(compile_bytecode)}",
199
205
  "--entrypoint",
200
206
  "bash",
201
207
  base_image,
@@ -7,6 +7,15 @@ from pathlib import Path
7
7
 
8
8
  from repro_lambda.manifest import LambdaSpec, Source
9
9
 
10
+ # The builder's contribution to every artifact key. NOT the package version: a
11
+ # release that leaves the packaged bytes alone must keep every key, or promote-by-key
12
+ # sees two keys for one zip. Bump it only when the same inputs would produce different
13
+ # zip bytes (packaging, normalisation, install flags, base-image handling), and update
14
+ # the fingerprints in tests/test_hash_contract.py with it. Frozen at "0.8.0", the last
15
+ # package version folded here, so keys minted by 0.8.0 stay valid; the packaging
16
+ # modules did not change between 0.7.2 and 0.8.1.
17
+ HASH_CONTRACT = "0.8.0"
18
+
10
19
 
11
20
  def _sha256_file(path: Path) -> str:
12
21
  h = hashlib.sha256()
@@ -32,6 +41,7 @@ def compute_content_hash(
32
41
  """
33
42
  sha256 over: sorted (relative-path, sha256(content)) tuples for the staged tree
34
43
  + sha256(requirements_lock) + spec scalars + base_image + builder_version
44
+ (the HASH_CONTRACT, not the package version)
35
45
  + optional resolved include/exclude filter lists + optional extra_files keyed by
36
46
  destination relname (e.g. "package.json").
37
47
 
@@ -75,6 +85,8 @@ def compute_content_hash(
75
85
  h.update(f"package_manager={spec.package_manager}\n".encode())
76
86
  h.update(f"lambda_at_edge={int(spec.lambda_at_edge)}\n".encode())
77
87
  h.update(f"hash_extra={spec.hash_extra}\n".encode())
88
+ if spec.compile_bytecode: # only when set, so specs without it keep their hashes
89
+ h.update(b"compile_bytecode=1\n")
78
90
  h.update(f"base_image={base_image}\n".encode())
79
91
  h.update(f"builder_version={builder_version}\n".encode())
80
92
 
@@ -131,6 +131,9 @@ class LambdaSpec:
131
131
  include_patterns: list[str] | None = None
132
132
  exclude_patterns: list[str] | None = None
133
133
  sources: tuple[Source, ...] = ()
134
+ # pip only: ship checked-hash bytecode compiled in the builder image, so a read-only
135
+ # /var/task does not recompile every imported module in every new process.
136
+ compile_bytecode: bool = False
134
137
 
135
138
  @property
136
139
  def resolved_requirements_lock(self) -> str:
@@ -450,6 +453,16 @@ def load_manifest(path: Path) -> Manifest:
450
453
 
451
454
  extra_files = _parse_extra_files(path, entry)
452
455
  sources = _parse_sources(path, entry)
456
+ compile_bytecode = entry.get("compile_bytecode", False)
457
+ if not isinstance(compile_bytecode, bool):
458
+ raise ValueError(
459
+ f"{path}: lambda {entry.get('logical_name')!r} compile_bytecode must be a boolean "
460
+ f"(got {compile_bytecode!r})"
461
+ )
462
+ if compile_bytecode and pkg != "pip":
463
+ raise ValueError(
464
+ f"{path}: lambda {entry.get('logical_name')!r} compile_bytecode is for pip lambdas"
465
+ )
453
466
 
454
467
  override_base_image = entry.get("base_image_python")
455
468
  if override_base_image is not None and "@sha256:" not in override_base_image:
@@ -489,6 +502,7 @@ def load_manifest(path: Path) -> Manifest:
489
502
  include_patterns=list(override_include) if override_include is not None else None,
490
503
  exclude_patterns=list(override_exclude) if override_exclude is not None else None,
491
504
  sources=sources,
505
+ compile_bytecode=compile_bytecode,
492
506
  )
493
507
  )
494
508
 
@@ -68,6 +68,7 @@ def verify_reproducible(
68
68
  base_image=builder.base_image_python,
69
69
  arch=spec.arch,
70
70
  python_version=spec.runtime.removeprefix("python"),
71
+ compile_bytecode=spec.compile_bytecode,
71
72
  )
72
73
  else: # npm
73
74
  node_ver = spec.runtime.removeprefix("nodejs").removesuffix(".x")
@@ -3,6 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import fnmatch
6
+ import os
6
7
  import sys
7
8
  from pathlib import Path
8
9
  from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
@@ -76,3 +77,46 @@ def pack_directory(
76
77
  else:
77
78
  data = p.read_bytes()
78
79
  zf.writestr(zinfo, data)
80
+
81
+
82
+ def compile_bytecode(src: Path, deploy_root: str = "/var/task") -> None:
83
+ """Replace any bytecode under `src` with checked-hash .pyc built by this interpreter.
84
+
85
+ checked-hash bytecode carries no source mtime (the zip resets every mtime) and the
86
+ interpreter checks it against the source hash on import, so it stays valid in a
87
+ read-only deploy dir. Recorded file paths are rewritten from `src` to `deploy_root`
88
+ (tracebacks show the deployed paths, and the bytes do not depend on the staging dir);
89
+ PYTHONHASHSEED=0 keeps the marshalled output stable. A file that does not compile gets
90
+ no bytecode and is compiled at import as before; compileall's report is printed.
91
+ """
92
+ import shutil
93
+ import subprocess
94
+
95
+ root = src.resolve()
96
+ for cache in sorted(root.rglob("__pycache__"), reverse=True):
97
+ if cache.is_dir() and not cache.is_symlink():
98
+ shutil.rmtree(cache)
99
+ for stale in root.rglob("*.pyc"):
100
+ stale.unlink()
101
+ result = subprocess.run(
102
+ [
103
+ sys.executable,
104
+ "-m",
105
+ "compileall",
106
+ "-q",
107
+ "--invalidation-mode",
108
+ "checked-hash",
109
+ "-s",
110
+ str(root),
111
+ "-p",
112
+ deploy_root,
113
+ str(root),
114
+ ],
115
+ env={**os.environ, "PYTHONHASHSEED": "0"},
116
+ capture_output=True,
117
+ text=True,
118
+ )
119
+ if result.returncode != 0:
120
+ print(
121
+ f"warning: some files did not compile:\n{result.stdout}{result.stderr}", file=sys.stderr
122
+ )
@@ -7,6 +7,7 @@ from moto import mock_aws
7
7
 
8
8
  from repro_lambda.build import BuildResult, build_one, compute_sha_for
9
9
  from repro_lambda.catalog import Catalog
10
+ from repro_lambda.hasher import HASH_CONTRACT
10
11
  from repro_lambda.manifest import BuilderConfig, LambdaSpec
11
12
 
12
13
 
@@ -84,6 +85,8 @@ def test_build_one_cache_hit_skips_docker_and_returns_existing_sha(
84
85
  assert outcome.sha256 == sha
85
86
  mock_docker.assert_not_called()
86
87
  assert catalog.lambdas["app"].current == sha
88
+ # This process did not build the object; claim only the hash contract.
89
+ assert catalog.lambdas["app"].history[0].builder_version == f"hash-contract:{HASH_CONTRACT}"
87
90
 
88
91
 
89
92
  def test_build_one_cache_miss_runs_docker_uploads_and_records(git_repo_with_sample: Path, mocker):
@@ -0,0 +1,114 @@
1
+ """compile_bytecode: checked-hash .pyc built in the zip step, keyed into the content hash."""
2
+
3
+ import dataclasses
4
+ import importlib.util
5
+ import marshal
6
+ from pathlib import Path
7
+ from zipfile import ZipFile
8
+
9
+ import pytest
10
+ from typer.testing import CliRunner
11
+
12
+ from repro_lambda.cli import app
13
+ from repro_lambda.hasher import compute_content_hash
14
+ from repro_lambda.manifest import LambdaSpec, load_manifest
15
+
16
+ runner = CliRunner()
17
+
18
+
19
+ def _pkg(root: Path) -> Path:
20
+ pkg = root / "pkg"
21
+ (pkg / "mymod" / "__pycache__").mkdir(parents=True)
22
+ (pkg / "mymod" / "__init__.py").write_text("VALUES = frozenset({'a', 'b', 'c'})\n")
23
+ (pkg / "mymod" / "__pycache__" / "stale.cpython-313.pyc").write_bytes(b"\x00")
24
+ (pkg / "mymod" / "orphan.pyc").write_bytes(b"\x00")
25
+ (pkg / "broken.py").write_text("def broken(:\n")
26
+ return pkg
27
+
28
+
29
+ def _zip(pkg: Path, out: Path, *extra: str) -> list[str]:
30
+ result = runner.invoke(app, ["zip", "--src", str(pkg), "--out", str(out), *extra])
31
+ assert result.exit_code == 0, result.output
32
+ return ZipFile(out).namelist()
33
+
34
+
35
+ def test_zip_compiles_checked_hash_bytecode_with_deployed_paths(tmp_path: Path):
36
+ names = _zip(_pkg(tmp_path), tmp_path / "lambda.zip", "--compile-bytecode")
37
+ pycs = [n for n in names if n.endswith(".pyc")]
38
+ assert len(pycs) == 1 and pycs[0].startswith("mymod/__pycache__/__init__.")
39
+ data = ZipFile(tmp_path / "lambda.zip").read(pycs[0])
40
+ assert data[:4] == importlib.util.MAGIC_NUMBER
41
+ assert int.from_bytes(data[4:8], "little") == 0b11 # hash-based, checked
42
+ assert marshal.loads(data[16:]).co_filename == "/var/task/mymod/__init__.py"
43
+ assert "mymod/orphan.pyc" not in names # stale bytecode is replaced, not shipped
44
+ assert "broken.py" in names # a file that does not compile ships as source only
45
+
46
+
47
+ def test_compiled_zip_is_byte_identical_across_builds(tmp_path: Path):
48
+ a, b = tmp_path / "a", tmp_path / "b" # different staging dirs, same tree
49
+ _zip(_pkg(a), a / "lambda.zip", "--compile-bytecode")
50
+ _zip(_pkg(b), b / "lambda.zip", "--compile-bytecode")
51
+ assert (a / "lambda.zip").read_bytes() == (b / "lambda.zip").read_bytes()
52
+
53
+
54
+ def test_zip_without_the_flag_still_strips_bytecode(tmp_path: Path):
55
+ names = _zip(_pkg(tmp_path), tmp_path / "lambda.zip")
56
+ assert not any(n.endswith(".pyc") or "__pycache__" in n for n in names)
57
+
58
+
59
+ def _spec(**kw) -> LambdaSpec:
60
+ base = LambdaSpec(
61
+ logical_name="app",
62
+ source_dir="handler",
63
+ requirements_lock="handler/requirements.${arch}.lock",
64
+ runtime="python3.13",
65
+ arch="arm64",
66
+ handler="app.lambda_handler",
67
+ )
68
+ return dataclasses.replace(base, **kw)
69
+
70
+
71
+ def test_flag_changes_the_content_hash_and_default_keeps_it(tmp_path: Path):
72
+ src = tmp_path / "source"
73
+ src.mkdir()
74
+ (src / "app.py").write_text("x = 1\n")
75
+ lock = tmp_path / "requirements.arm64.lock"
76
+ lock.write_text("# empty\n")
77
+ off = compute_content_hash(src, lock, _spec(), "x@sha256:0", "1")
78
+ on = compute_content_hash(src, lock, _spec(compile_bytecode=True), "x@sha256:0", "1")
79
+ assert off != on
80
+ assert off == compute_content_hash(src, lock, _spec(compile_bytecode=False), "x@sha256:0", "1")
81
+
82
+
83
+ def _manifest(tmp_path: Path, extra: str, package_manager: str = "pip") -> Path:
84
+ path = tmp_path / "lambdas.toml"
85
+ path.write_text(
86
+ f"""
87
+ [[lambda]]
88
+ logical_name = "app"
89
+ source_dir = "handler"
90
+ requirements_lock = "handler/requirements.${{arch}}.lock"
91
+ runtime = "python3.13"
92
+ arch = "arm64"
93
+ handler = "app.lambda_handler"
94
+ package_manager = "{package_manager}"
95
+ {extra}
96
+
97
+ [builder]
98
+ base_image_python = "public.ecr.aws/lambda/python:3.13@sha256:abc"
99
+ """
100
+ )
101
+ return path
102
+
103
+
104
+ def test_manifest_reads_the_flag(tmp_path: Path):
105
+ assert (
106
+ load_manifest(_manifest(tmp_path, "compile_bytecode = true")).lambdas[0].compile_bytecode
107
+ is True
108
+ )
109
+ assert load_manifest(_manifest(tmp_path, "")).lambdas[0].compile_bytecode is False
110
+
111
+
112
+ def test_manifest_rejects_a_non_boolean_flag(tmp_path: Path):
113
+ with pytest.raises(ValueError, match="compile_bytecode must be a boolean"):
114
+ load_manifest(_manifest(tmp_path, 'compile_bytecode = "yes"'))
@@ -220,3 +220,31 @@ def test_compute_sha_for_reflects_payload_binary(tmp_path: Path):
220
220
  sha_b = compute_sha_for(repo_root=repo, spec=spec, builder=builder)
221
221
 
222
222
  assert sha_a != sha_b, "content hash must track the bundled binary's bytes"
223
+
224
+
225
+ def test_compute_sha_for_ignores_the_package_version(tmp_path: Path, monkeypatch):
226
+ # A release that does not change the packaged bytes must not re-key artifacts:
227
+ # identical zips under two keys break promote-by-key between accounts.
228
+ repo = _git_repo_with_source(tmp_path)
229
+ (repo / "handler" / "requirements.arm64.lock").write_text("")
230
+ builder = BuilderConfig(base_image_python=PINNED_IMAGE)
231
+ spec = _spec()
232
+
233
+ sha_a = compute_sha_for(repo_root=repo, spec=spec, builder=builder)
234
+ monkeypatch.setattr("repro_lambda.build.__version__", "99.0.0")
235
+ sha_b = compute_sha_for(repo_root=repo, spec=spec, builder=builder)
236
+
237
+ assert sha_a == sha_b
238
+
239
+
240
+ def test_compute_sha_for_rekeys_on_hash_contract_bump(tmp_path: Path, monkeypatch):
241
+ repo = _git_repo_with_source(tmp_path)
242
+ (repo / "handler" / "requirements.arm64.lock").write_text("")
243
+ builder = BuilderConfig(base_image_python=PINNED_IMAGE)
244
+ spec = _spec()
245
+
246
+ sha_a = compute_sha_for(repo_root=repo, spec=spec, builder=builder)
247
+ monkeypatch.setattr("repro_lambda.build.HASH_CONTRACT", "bumped", raising=False)
248
+ sha_b = compute_sha_for(repo_root=repo, spec=spec, builder=builder)
249
+
250
+ assert sha_a != sha_b
@@ -0,0 +1,83 @@
1
+ """Guards for HASH_CONTRACT: artifact keys must change exactly when packaged bytes can."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ from pathlib import Path
7
+
8
+ import repro_lambda
9
+ from repro_lambda.hasher import HASH_CONTRACT, compute_content_hash
10
+ from repro_lambda.manifest import LambdaSpec
11
+
12
+ _PKG = Path(repro_lambda.__file__).parent
13
+
14
+ # Modules that shape the zip bytes or the key beyond the hashed inputs. Editing one
15
+ # means deciding whether the same inputs can now produce different bytes: if yes, bump
16
+ # HASH_CONTRACT; either way, record the new fingerprints here under the (possibly new)
17
+ # contract. A fingerprint update is cheap; a key change re-keys every artifact.
18
+ # Known gap: npm packaging uses base_image_python, which is not in the key (issue #20).
19
+ PACKAGING_FINGERPRINTS = {
20
+ "0.8.0": {
21
+ # compile_bytecode: off by default and hashed only when on, so no bump.
22
+ "build.py": "21b10b59acf04dae78431e1c50ebbb89d300dd5d32bbdd36e0ee9c2cb3673ea9",
23
+ "hasher.py": "1061efdcb802598001c9638e34222b25b083c6123ff9b59a18dff996a4e0a70f",
24
+ "zip_packager.py": "40d48633ece8d5d9ffffdb786d7479b8588ec7b1fae99a7db748e3963f9b333a",
25
+ "docker_runner.py": "e53208a62ee7ee40e33068b6c3ba936c5e7d75037b1502f1af8fed92391f8f52",
26
+ "source_stager.py": "38186277fb94ab6feac8a07cad2ae35f5adfaa52bde3d51ac71daccb003d3c68",
27
+ "sources.py": "10415ccc89b85f521fb64ef199f084f0bea1842032d414cb3d3537334f6caa0b",
28
+ },
29
+ }
30
+
31
+
32
+ def test_packaging_modules_match_the_hash_contract():
33
+ expected = PACKAGING_FINGERPRINTS.get(HASH_CONTRACT)
34
+ assert expected, f"no packaging fingerprints recorded for HASH_CONTRACT={HASH_CONTRACT!r}"
35
+ actual = {name: hashlib.sha256((_PKG / name).read_bytes()).hexdigest() for name in expected}
36
+ changed = sorted(n for n in expected if actual[n] != expected[n])
37
+ assert not changed, (
38
+ f"packaging module(s) changed: {changed}. If the same inputs can now produce "
39
+ "different zip bytes, bump HASH_CONTRACT in hasher.py. Then record these "
40
+ f"fingerprints under the contract: {actual}"
41
+ )
42
+
43
+
44
+ def test_content_hash_golden_key(tmp_path: Path):
45
+ # Pins the hash layout: any change to what is folded, or how, re-keys every artifact.
46
+ src = tmp_path / "source"
47
+ src.mkdir()
48
+ (src / "app.py").write_text("ok\n")
49
+ lock = tmp_path / "lock.txt"
50
+ lock.write_text("\n")
51
+ spec = LambdaSpec(
52
+ logical_name="fn",
53
+ source_dir="src/fn",
54
+ requirements_lock="src/fn/requirements.${arch}.lock",
55
+ runtime="python3.13",
56
+ arch="arm64",
57
+ handler="app.handler",
58
+ region="eu-west-1",
59
+ )
60
+ got = compute_content_hash(src, lock, spec, "img@sha256:0", HASH_CONTRACT)
61
+ assert got == "de8da0c8801288758e8dc86edc15b085ffadc6eb915dcc45d875bfb235854dea"
62
+
63
+
64
+ # The zip command's exclusion list lives in cli.py; changing it changes the zip bytes.
65
+ ZIP_EXCLUDES = {
66
+ "0.8.0": [
67
+ "*__pycache__*",
68
+ "*.pyc",
69
+ "*.dist-info/RECORD",
70
+ "*.dist-info/INSTALLER",
71
+ "*.dist-info/direct_url.json",
72
+ "*.dist-info/REQUESTED",
73
+ ],
74
+ }
75
+
76
+
77
+ def test_zip_excludes_match_the_hash_contract():
78
+ from repro_lambda.cli import _LAMBDA_ZIP_EXCLUDES
79
+
80
+ assert ZIP_EXCLUDES.get(HASH_CONTRACT) == _LAMBDA_ZIP_EXCLUDES, (
81
+ "the lambda zip exclusion list changed: bump HASH_CONTRACT in hasher.py "
82
+ "and record the new list under the contract"
83
+ )
@@ -1,5 +1,5 @@
1
1
  version = 1
2
- revision = 3
2
+ revision = 5
3
3
  requires-python = ">=3.11"
4
4
 
5
5
  [[package]]
@@ -646,7 +646,7 @@ wheels = [
646
646
 
647
647
  [[package]]
648
648
  name = "repro-lambda"
649
- version = "0.8.1"
649
+ version = "0.9.0"
650
650
  source = { editable = "." }
651
651
  dependencies = [
652
652
  { name = "boto3" },
@@ -1,3 +0,0 @@
1
- {
2
- ".": "0.8.1"
3
- }
File without changes
File without changes
File without changes