utils¶
- aws_lambda_artifact_builder.utils.ensure_exact_one_true(lst: List[bool])[source]¶
Ensure that exactly one element in the list is True.
- aws_lambda_artifact_builder.utils.write_bytes(path: Path, content: bytes)[source]¶
Write bytes to a file, creating parent directories if they don’t exist.
- aws_lambda_artifact_builder.utils.is_match(relpath_parts: list[str], include: list[str], exclude: list[str]) bool[source]¶
Based on the include and exclude pattern, do we ignore this file?
Explicit exclude > Explicit include > Implicit include
- Parameters:
relpath_parts – relative path parts of the file to be checked For example, if the file is /a/b/c/d.txt, and the base dir is /a, then relpath_parts should be [‘b’, ‘c’, ‘d.txt’]
include – list of glob patterns to include
exclude – list of glob patterns to exclude
- aws_lambda_artifact_builder.utils.normalize_glob_patterns(patterns: str | list[str] | None) list[str][source]¶
Normalize glob pattern input to a list of strings.
Handles flexible input types for include/exclude patterns, converting single strings to lists and None to empty lists for consistent processing.
- Parameters:
patterns – Glob patterns as string, list of strings, or None
- Returns:
Normalized list of glob pattern strings
Examples:
normalize_glob_patterns(None) # [] normalize_glob_patterns("*.py") # ["*.py"] normalize_glob_patterns(["*.py", "*.txt"]) # ["*.py", "*.txt"]
- aws_lambda_artifact_builder.utils.copy_source_for_lambda_deployment(source_dir: str | Path, target_dir: str | Path, include: str | list[str] | None = None, exclude: str | list[str] | None = None)[source]¶
Selectively copy Python source code for AWS Lambda deployment packaging.
This function prepares Python library source code for Lambda deployment by copying files from a source directory to a target directory with selective filtering. It’s designed to create clean deployment packages that exclude unnecessary files like tests, cache files, and development artifacts.
Lambda Deployment Context:
Lambda deployment packages must be optimized for size and contain only the necessary runtime files. This function helps create such packages by:
Filtering source files based on include/exclude patterns
Automatically excluding Python cache files (
__pycache__,*.pyc,*.pyo)Preserving directory structure for proper module imports
Creating a clean target directory for packaging
- Parameters:
source_dir – Source directory containing Python library code
target_dir – Target directory where filtered files will be copied
include – Glob patterns to include (if None, includes all files)
exclude – Glob patterns to exclude (auto-excludes Python cache files)
Examples:
# Copy only Python files, excluding tests copy_source_for_lambda_deployment( source_dir="./my_package", target_dir="./build/my_package", include=["*.py"], exclude=["*test*", "*dev*"] ) # Copy all files except specific patterns copy_source_for_lambda_deployment( source_dir="./src", target_dir="./lambda_build/src", exclude=["*.md", "docs/*", "examples/*"] )
Note
The target directory is completely replaced if it exists. Python cache files (
__pycache__,*.pyc,*.pyo) are always excluded regardless of the exclude parameter.See also
is_match()for the pattern matching logic used in file filtering
- aws_lambda_artifact_builder.utils.prompt_to_confirm_before_remove_dir(dir_path: Path) bool[source]¶
Prompt user to confirm before removing a directory and its contents.
- aws_lambda_artifact_builder.utils.clean_build_directory(dir_build: Path, folder_alias: str, skip_prompt: bool = False)[source]¶
Prepare the temporary build directory for building artifacts.
This function ensures that the build directory is clean by removing it if it already exists, optionally prompting the user for confirmation. It is a common utility used by multiple methods that build Lambda artifacts, regardless of the specific build tool or approach.
- Parameters:
dir_build – The temporary build directory for Lambda artifacts.
folder_alias – A human-readable alias for the directory, used in prompts and error messages.
skip_prompt – If True, skips the confirmation prompt before removing an existing directory.