66# Copyright 2019 Datadog, Inc.
77
88# Builds datadog-lambda-python layers for Lambda functions
9-
10- # Usage: PYTHON_VERSION=3.11 ./build_layers.sh
11- # If PYTHON_VERSION is not specified, all versions will be built.
9+ #
10+ # Usage:
11+ # PYTHON_VERSION=12 ARCH=arm ./scripts/build_layers.sh
12+ #
13+ # Environment variables:
14+ # PYTHON_VERSION Python minor version. Accepts shorthand (e.g. 12) or full (e.g. 3.12).
15+ # If not set, all supported versions are built.
16+ # ARCH Target architecture. Accepts shorthand: arm, amd, x86, aarch64
17+ # or full: arm64, amd64. If not set, both are built.
18+ #
19+ # dd-trace-py overrides (mutually exclusive, highest priority first):
20+ # DD_TRACE_COMMIT Specific dd-trace-py commit SHA to build from GitHub.
21+ # DD_TRACE_COMMIT_BRANCH dd-trace-py branch name to build from GitHub.
22+ # DD_TRACE_WHEEL Path to a pre-built ddtrace .whl file.
23+ #
24+ # Examples:
25+ # # Build a single layer for Python 3.12 on arm64
26+ # PYTHON_VERSION=12 ARCH=arm ./scripts/build_layers.sh
27+ #
28+ # # Build with a specific dd-trace-py commit (for git bisect)
29+ # DD_TRACE_COMMIT=abc123 PYTHON_VERSION=12 ARCH=arm ./scripts/build_layers.sh
1230
1331set -e
1432
@@ -17,6 +35,13 @@ LAYER_FILES_PREFIX="datadog_lambda_py"
1735AVAILABLE_PYTHON_VERSIONS=(" 3.8" " 3.9" " 3.10" " 3.11" " 3.12" " 3.13" " 3.14" )
1836AVAILABLE_ARCHS=(" arm64" " amd64" )
1937
38+ if [ -n " $ARCH " ]; then
39+ case " $ARCH " in
40+ arm|arm64|aarch64) ARCH=" arm64" ;;
41+ amd|amd64|x86|x86_64) ARCH=" amd64" ;;
42+ esac
43+ fi
44+
2045if [ -z " $ARCH " ]; then
2146 echo " No architectures specified, building layers for all architectures."
2247 ARCHS=(" ${AVAILABLE_ARCHS[@]} " )
2853 echo " EXITING SCRIPT."
2954 exit 1
3055 fi
31- ARCHS=$ARCH
56+ ARCHS=(" $ARCH " )
57+ fi
58+
59+ # Normalize Python version shorthand (e.g. 12 -> 3.12, 3.12 -> 3.12)
60+ if [ -n " $PYTHON_VERSION " ]; then
61+ if [[ " $PYTHON_VERSION " =~ ^[0-9]+$ ]]; then
62+ PYTHON_VERSION=" 3.${PYTHON_VERSION} "
63+ fi
3264fi
3365
3466# Determine which Python versions to build layers for
4375 echo " EXITING SCRIPT."
4476 exit 1
4577 fi
46- PYTHON_VERSIONS=$PYTHON_VERSION
78+ PYTHON_VERSIONS=( " $PYTHON_VERSION " )
4779fi
4880
49- # Replace ddtrace wheel used if necessary
50- if [ -n " $DD_TRACE_COMMIT_BRANCH " ]; then
51- sed -z -E -i ' s|(ddtrace = )\[[^]]*]|\1{ git = "https://github.com/DataDog/dd-trace-py.git", branch = \"' " $DD_TRACE_COMMIT_BRANCH " ' \" }|g' pyproject.toml
52- else
53- if [ -n " $DD_TRACE_WHEEL " ]; then
54- sed -z -E -i ' s|(ddtrace = )\[[^]]*]|\1{ file = "' " $DD_TRACE_WHEEL " ' " }|g' pyproject.toml
55- fi
81+ # Backup pyproject.toml so modifications don't persist across runs
82+ cp pyproject.toml pyproject.toml.bak
83+ cleanup () {
84+ mv pyproject.toml.bak pyproject.toml 2> /dev/null || true
85+ }
86+ trap cleanup EXIT
87+
88+ # Helper: replace the multi-line ddtrace dependency in pyproject.toml.
89+ # Uses perl instead of sed -z for macOS/Linux portability.
90+ replace_ddtrace_dep () {
91+ perl -i -0777 -pe " s|ddtrace = \[[^\]]*\]|$1 |gs" pyproject.toml
92+ }
93+
94+ # Replace ddtrace source if necessary
95+ if [ -n " $DD_TRACE_COMMIT " ]; then
96+ replace_ddtrace_dep " ddtrace = { git = \" https://github.com/DataDog/dd-trace-py.git\" , rev = \" $DD_TRACE_COMMIT \" }"
97+ elif [ -n " $DD_TRACE_COMMIT_BRANCH " ]; then
98+ replace_ddtrace_dep " ddtrace = { git = \" https://github.com/DataDog/dd-trace-py.git\" , branch = \" $DD_TRACE_COMMIT_BRANCH \" }"
99+ elif [ -n " $DD_TRACE_WHEEL " ]; then
100+ replace_ddtrace_dep " ddtrace = { file = \" $DD_TRACE_WHEEL \" }"
56101fi
57102
58103function make_path_absolute {
0 commit comments