Re: [OE-core] python3: Allow to specify which pyc files to keep

Alexander Kanavin
2025-04-10T08:02:59Z

Should we rather just package the higher level .pyc into their own packages?

Alex

On Thu, 10 Apr 2025 at 08:37, Lukas Woodtli via lists.openembedded.org
<lw=hqv.ch@lists.openembedded.org> wrote:
>
> From d7159cd842fd2a10ba5aeff62877be2ebb2eab87 Mon Sep 17 00:00:00 2001
> From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> Date: Mon, 7 Apr 2025 11:55:33 +0200
> Subject: [PATCH] python3: Allow to specify which pyc files to keep
>
> The pyc files for a specific optimization level can be kept and are
> installed in the final image.
>
> Signed-off-by: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> ---
> .../recipes-devtools/python/python3_3.13.2.bb | 32 +++++++++++++++----
> 1 file changed, 25 insertions(+), 7 deletions(-)
>
> diff --git a/meta/recipes-devtools/python/python3_3.13.2.bb
> b/meta/recipes-devtools/python/python3_3.13.2.bb
> index 7c36fd92ed..ac74432a2a 100644
> --- a/meta/recipes-devtools/python/python3_3.13.2.bb
> +++ b/meta/recipes-devtools/python/python3_3.13.2.bb
> @@ -217,6 +217,12 @@ do_install:append:class-native() {
> mv ${D}/${bindir}/${PN}/python*config ${D}/${bindir}/
> }
>
> +# We want bytecode precompiled .py files (.pyc's) by default
> +# but the user may set it on their own conf
> +INCLUDE_PYCS ?= "1"
> +
> +PYCS_OPT_LEVEL ?= "0"
> +
> do_install:append() {
> for c in
> ${D}/${libdir}/python${PYTHON_MAJMIN}/_sysconfigdata*.py; do
> python3 ${UNPACKDIR}/reformat_sysconfig.py $c
> @@ -249,10 +255,25 @@ do_install:append() {
> # so remove it too
> rm -f
> ${D}${libdir}/python${PYTHON_MAJMIN}/__pycache__/traceback.cpython*
>
> - # Remove the opt-1.pyc and opt-2.pyc files. They effectively
> waste space on embedded
> - # style targets as they're only used when python is called with
> the -O or -OO options
> - # which is rare.
> - find ${D} -name *opt-*.pyc -delete
> + if [ "${INCLUDE_PYCS}" -eq "1" ]; then
> + # Remove only the .pyc files of unused optimization level.
> + if [ "${PYCS_OPT_LEVEL}" -eq "0" ]; then
> + # keep only unoptimized .pyc files
> + find ${D} -name *.opt-1.pyc -exec rm -f {} \;
> + find ${D} -name *.opt-2.pyc -exec rm -f {} \;
> + elif [ "${PYCS_OPT_LEVEL}" -eq "1" ]; then
> + # keep only .pyc files with optimization level 1
> + find ${D} -name *.pyc -and -not -name *.opt-1.pyc -exec
> rm -f {} \;
> + elif [ "${PYCS_OPT_LEVEL}" -eq "2" ]; then
> + # keep only .pyc files with optimization level 2
> + find ${D} -name *.pyc -and -not -name *.opt-2.pyc -exec
> rm -f {} \;
> + else
> + bberror "Python optimization level ${PYCS_OPT_LEVEL} is
> not supported"
> + fi
> + else
> + # remove all .pyc files
> + find ${D} -name *.pyc -delete
> + fi
> }
>
> do_install:append:class-nativesdk () {
> @@ -326,9 +347,6 @@ py_package_preprocess () {
> rm -rf ${PKGD}/${libdir}/python-sysconfigdata
> }
>
> -# We want bytecode precompiled .py files (.pyc's) by default
> -# but the user may set it on their own conf
> -INCLUDE_PYCS ?= "1"
>
> python(){
> import collections, json
> --
> 2.43.0
>
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#214621): https://lists.openembedded.org/g/openembedded-core/message/214621
> Mute This Topic: https://lists.openembedded.org/mt/112186823/1686489
> Group Owner: openembedded-core+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [alex.kanavin@gmail.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>