From patchwork Thu Apr 10 06:37:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukas Woodtli X-Patchwork-Id: 61098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8DDA2C3601E for ; Thu, 10 Apr 2025 06:38:08 +0000 (UTC) Received: from amphora.sui-inter.net (amphora.sui-inter.net [80.74.147.33]) by mx.groups.io with SMTP id smtpd.web11.28371.1744267076458108851 for ; Wed, 09 Apr 2025 23:37:57 -0700 Authentication-Results: mx.groups.io; dkim=none (message not signed); spf=none, err=permanent DNS error (domain: hqv.ch, ip: 80.74.147.33, mailfrom: lw@hqv.ch) Received: from [192.168.1.129] (localhost [127.0.0.1]) by amphora.sui-inter.net (Postfix) with ESMTPSA id 31B559681351 for ; Thu, 10 Apr 2025 08:37:54 +0200 (CEST) Authentication-Results: amphora.sui-inter.net; spf=pass (sender IP is 185.132.16.78) smtp.mailfrom=lw@hqv.ch smtp.helo=[192.168.1.129] Received-SPF: pass (amphora.sui-inter.net: connection is authenticated) Message-ID: <2c0885e1-8821-4470-8e54-dcad6a241834@hqv.ch> Date: Thu, 10 Apr 2025 08:37:54 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: openembedded-core@lists.openembedded.org From: Lukas Woodtli Subject: python3: Allow to specify which pyc files to keep List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Thu, 10 Apr 2025 06:38:08 -0000 X-Groupsio-URL: https://lists.openembedded.org/g/openembedded-core/message/214621 From d7159cd842fd2a10ba5aeff62877be2ebb2eab87 Mon Sep 17 00:00:00 2001 From: Lukas Woodtli 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 Signed-off-by: Lukas Woodtli ---  .../recipes-devtools/python/python3_3.13.2.bb | 32 +++++++++++++++----  1 file changed, 25 insertions(+), 7 deletions(-)              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 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