Source code for aws_lbd_art_builder_uv.layer.container_builder
# -*- coding: utf-8 -*-"""UV-based containerized Lambda layer builder.:class:`UvLambdaLayerContainerBuilder` orchestrates a Docker-based buildthat runs ``uv sync`` inside an official AWS SAM image.See ``docs/source/99-Maintainer-Guide/03-Build-Lambda-Layer-using-UV-in-Container``for the full architecture guide."""importsubprocessimportdataclassesfrompathlibimportPathimportaws_lbd_art_builder_core.apiasaws_lbd_art_builder_corefrom..pathsimportpath_enum
[docs]@dataclasses.dataclass(frozen=True)classUvLambdaLayerContainerBuilder(aws_lbd_art_builder_core.layer_api.BaseLambdaLayerContainerBuilder,):# pragma: no cover""" Build a Lambda layer using uv inside a Docker container. **Mount scheme**: ``{project_root}/build/lambda/layer/`` → ``/var/task/`` The container script (``_build_in_container.py``) is pure stdlib — no third-party packages are installed inside the container except ``uv``. """# fmt: offpath_script:Path=dataclasses.field(default=path_enum.path_build_in_container_script)# credentials is optional because most projects only need public PyPI.# When private repositories are needed, the caller passes a Credentials# object that gets serialized to JSON and read inside the container.credentials:aws_lbd_art_builder_core.layer_api.Credentials|None=dataclasses.field(default=None)skip_prompt:bool=dataclasses.field(default=False)# fmt: ondefrun(self):self.step_1_preflight_check()self.step_2_prepare_environment()self.step_3_execute_build()self.step_4_finalize_artifacts()# --- Step 1 ---------------------------------------------------------------defstep_1_preflight_check(self):self.log_header("Step 1 - Preflight Check")self.step_1_1_print_info()self.step_1_2_check()defstep_1_1_print_info(self):# fmt: offself.log_sub_header("Step 1.1 - Print Build Info")self.log_detail(f"path_pyproject_toml = {self.path_pyproject_toml}")self.log_detail(f"py_ver_major = {self.py_ver_major}")self.log_detail(f"py_ver_minor = {self.py_ver_minor}")self.log_detail(f"is_arm = {self.is_arm}")self.log_detail(f"path_script = {self.path_script}")self.log_detail(f"dir_repo = {self.path_layout.dir_repo}")self.log_detail(f"dir_build_layer = {self.path_layout.dir_build_lambda_layer}")# fmt: on
[docs]defstep_1_2_check(self):""" Check that ``uv.lock`` exists before starting the build. Container builds are slow and resource-heavy (pulling Docker images, spinning up a container). Failing early on a missing lock file saves minutes of wasted time. The local builder skips this check because local builds are fast enough that discovering the missing lock file at ``uv sync`` time is acceptable. """self.log_sub_header("Step 1.2 - Check")path_uv_lock=self.path_layout.dir_project_root/"uv.lock"self.log_detail(f"Check if '{path_uv_lock}' exists ...")ifpath_uv_lock.exists():self.log_detail("Exists!")else:raiseFileNotFoundError(f"UV lock file not found: {path_uv_lock}, "f"cannot proceed with uv-based build. "f"Please run 'uv lock' to generate the lock file.")
[docs]defstep_4_finalize_artifacts(self):""" Move ``repo/.venv/lib/pythonX.Y/site-packages/`` → ``artifacts/python/`` on the host. """self.log_header("Step 4 - Finalize Artifacts")self.step_4_1_move_site_packages_to_python()
defstep_4_1_move_site_packages_to_python(self):self.log_sub_header("Step 4.1 - Move site-packages to python/")dir_source=self.path_layout.dir_build_lambda_layer_repo_venv_site_packagesdir_target=self.path_layout.dir_pythonself.log_detail(f"Move '{dir_source}' to '{dir_target}'")aws_lbd_art_builder_core.layer_api.move_to_dir_python(dir_site_packages=dir_source,dir_python=dir_target,)