Skip to content

Commit 21012cb

Browse files
authored
Pre-compile all python code and remove original .py files. (#476)
* Remove urllib3 dist-info files as well. * Pre-compile all python code and remove original .py files. * Compress at optimization level 2. * Add PYTHONNODEBUGRANGES=1 to compile step. * Add comment. * Use different commandline option to support py3.8 * Update size limits. * Leave __init__.py contrib files. * Remove text like files. * oops
1 parent f80f83f commit 21012cb

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

Dockerfile

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@ WORKDIR /build
1111
COPY . .
1212
RUN pip install . -t ./python/lib/$runtime/site-packages
1313

14-
# Remove *.pyc files
15-
RUN find ./python/lib/$runtime/site-packages -name \*.pyc -delete
16-
1714
# Remove botocore (40MB) to reduce package size. aws-xray-sdk
1815
# installs it, while it's already provided by the Lambda Runtime.
1916
RUN rm -rf ./python/lib/$runtime/site-packages/botocore*
2017
RUN rm -rf ./python/lib/$runtime/site-packages/setuptools
2118
RUN rm -rf ./python/lib/$runtime/site-packages/jsonschema/tests
2219
RUN find . -name 'libddwaf.so' -delete
23-
RUN rm -rf ./python/lib/$runtime/site-packages/urllib3
20+
RUN rm -rf ./python/lib/$runtime/site-packages/urllib3*
2421
RUN rm ./python/lib/$runtime/site-packages/ddtrace/appsec/_iast/_taint_tracking/*.so
2522
RUN rm ./python/lib/$runtime/site-packages/ddtrace/appsec/_iast/_stacktrace*.so
2623
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/libdd_wrapper.so
2724
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/ddup/_ddup.*.so
2825
RUN rm ./python/lib/$runtime/site-packages/ddtrace/internal/datadog/profiling/stack_v2/_stack_v2.*.so
2926
RUN find . -name "*.dist-info" -type d | xargs rm -rf
3027

28+
# Precompile all .pyc files and remove .py files. This speeds up load time.
29+
# Compile with optimization level 2 (-OO) and PYTHONNODEBUGRANGES=1 to redtce
30+
# size of .pyc files.
31+
# See https://docs.python.org/3/tutorial/modules.html#compiled-python-files
32+
# https://docs.python.org/3.11/using/cmdline.html#cmdoption-O
33+
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONNODEBUGRANGES
34+
RUN PYTHONNODEBUGRANGES=1 python -OO -m compileall -b ./python/lib/$runtime/site-packages
35+
# remove all .py files except ddtrace/contrib/*/__init__.py which are necessary
36+
# for ddtrace.patch to discover instrumationation packages.
37+
RUN find ./python/lib/$runtime/site-packages -name \*.py | grep -v ddtrace/contrib | xargs rm -rf
38+
RUN find ./python/lib/$runtime/site-packages/ddtrace/contrib -name \*.py | grep -v __init__ | xargs rm -rf
39+
RUN find ./python/lib/$runtime/site-packages -name __pycache__ -type d -exec rm -r {} \+
3140

3241
FROM scratch
3342
COPY --from=builder /build/python /

scripts/check_layer_size.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
# Compares layer size to threshold, and fails if below that threshold
99

10-
# 7 mb size limit
1110
set -e
12-
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 4 \* 1024)
13-
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 13 \* 1024)
11+
MAX_LAYER_COMPRESSED_SIZE_KB=$(expr 5 \* 1024)
12+
MAX_LAYER_UNCOMPRESSED_SIZE_KB=$(expr 12 \* 1024)
1413

1514

1615
LAYER_FILES_PREFIX="datadog_lambda_py"

0 commit comments

Comments
 (0)