From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 8E48B159C9B for ; Fri, 2 Aug 2024 19:31:39 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id AC6F5E2A6D; Fri, 2 Aug 2024 19:31:19 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 61D53E2A69 for ; Fri, 2 Aug 2024 19:31:19 +0000 (UTC) From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= To: gentoo-dev@lists.gentoo.org Cc: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Subject: [gentoo-dev] [PATCH 2/2] python-utils-r1.eclass: Add a sanity check for PYTHONPATH Date: Fri, 2 Aug 2024 21:31:11 +0200 Message-ID: <20240802193111.1884409-2-mgorny@gentoo.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240802193111.1884409-1-mgorny@gentoo.org> References: <20240802193111.1884409-1-mgorny@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Archives-Salt: 71b079f5-a414-49e9-b1d9-3deceed0ce7c X-Archives-Hash: 230b1e08d66c160171f6419649011c95 Add a sanity check that ensures that PYTHONPATH does not contain any relative paths. While at it, also explicitly warn about PYTHONPATH being set, so we know about it in build logs. Signed-off-by: Michał Górny --- eclass/distutils-r1.eclass | 1 + eclass/python-any-r1.eclass | 1 + eclass/python-r1.eclass | 2 ++ eclass/python-single-r1.eclass | 1 + eclass/python-utils-r1.eclass | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+) diff --git a/eclass/distutils-r1.eclass b/eclass/distutils-r1.eclass index 4afee3d45b1c..5d9f97474b29 100644 --- a/eclass/distutils-r1.eclass +++ b/eclass/distutils-r1.eclass @@ -936,6 +936,7 @@ _distutils-r1_print_package_versions() { # distutils patches and/or quirks. distutils-r1_python_prepare_all() { debug-print-function ${FUNCNAME} "${@}" + _python_sanity_checks _distutils-r1_check_all_phase_mismatch if [[ ! ${DISTUTILS_OPTIONAL} ]]; then diff --git a/eclass/python-any-r1.eclass b/eclass/python-any-r1.eclass index a21df8e89498..65fffb798003 100644 --- a/eclass/python-any-r1.eclass +++ b/eclass/python-any-r1.eclass @@ -272,6 +272,7 @@ python_gen_any_dep() { # This function will call python_check_deps() if defined. python_setup() { debug-print-function ${FUNCNAME} "${@}" + _python_sanity_checks # support developer override if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then diff --git a/eclass/python-r1.eclass b/eclass/python-r1.eclass index c5fa6770558f..adf87c2c52f7 100644 --- a/eclass/python-r1.eclass +++ b/eclass/python-r1.eclass @@ -617,6 +617,7 @@ _python_multibuild_wrapper() { # locally, and the former two are exported to the command environment. python_foreach_impl() { debug-print-function ${FUNCNAME} "${@}" + _python_sanity_checks if [[ ${_DISTUTILS_R1_ECLASS} ]]; then if has "${EBUILD_PHASE}" prepare configure compile test install && @@ -708,6 +709,7 @@ python_foreach_impl() { # @CODE python_setup() { debug-print-function ${FUNCNAME} "${@}" + _python_sanity_checks local has_check_deps declare -f python_check_deps >/dev/null && has_check_deps=1 diff --git a/eclass/python-single-r1.eclass b/eclass/python-single-r1.eclass index 4d61f08c06f0..337404df1681 100644 --- a/eclass/python-single-r1.eclass +++ b/eclass/python-single-r1.eclass @@ -393,6 +393,7 @@ python_gen_impl_dep() { # the Python build environment up for it. python_setup() { debug-print-function ${FUNCNAME} "${@}" + _python_sanity_checks unset EPYTHON diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass index c47565fa1db2..cc33a1c1bffd 100644 --- a/eclass/python-utils-r1.eclass +++ b/eclass/python-utils-r1.eclass @@ -1544,4 +1544,38 @@ python_has_version() { return 0 } +# @FUNCTION: _python_sanity_checks +# @INTERNAL +# @DESCRIPTION: +# Perform additional environment sanity checks. +_python_sanity_checks() { + debug-print-function ${FUNCNAME} "${@}" + + [[ ${_PYTHON_SANITY_CHECKED} ]] && return + + if [[ -v PYTHONPATH ]]; then + local x paths=() + mapfile -d ':' -t paths <<<${PYTHONPATH} + + for x in "${paths[@]}"; do + if [[ ${x} != /* ]]; then + eerror "Relative path found in PYTHONPATH:" + eerror + eerror " PYTHONPATH=${PYTHONPATH@Q}" + eerror + eerror "This is guaranteed to cause random breakage. Please make sure that" + eerror "your PYTHONPATH contains absolute paths only (and only if necessary)." + eerror "Note that empty values (including ':' at either end and an empty" + eerror "PYTHONPATH) count as the current directory. If no PYTHONPATH" + eerror "is intended, it needs to be unset instead." + die "Relative paths in PYTHONPATH are forbidden: ${x@Q}" + fi + done + + elog "PYTHONPATH=${PYTHONPATH@Q}" + fi + + _PYTHON_SANITY_CHECKED=1 +} + fi -- 2.45.2