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 098F9158064 for ; Wed, 8 May 2024 17:15:48 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id F36CAE2C40; Wed, 8 May 2024 17:15:26 +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) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 4AA11E2C04 for ; Wed, 8 May 2024 17:15:26 +0000 (UTC) From: Florian Schmaus To: gentoo-dev@lists.gentoo.org Cc: qa@gentoo.org, Florian Schmaus Subject: [gentoo-dev] [PATCH 1/2] edo.eclass: enhace edob for usage with nosiy commands Date: Wed, 8 May 2024 19:15:04 +0200 Message-ID: <20240508171505.48392-2-flow@gentoo.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240508171505.48392-1-flow@gentoo.org> References: <20240508171505.48392-1-flow@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-Transfer-Encoding: 8bit X-Archives-Salt: d56405cc-c375-442a-8541-d6aa446417fd X-Archives-Hash: 752fb960db8809e8dc9465c3472a7680 Normally, edob can, or rather should, not be used with noisy commands, i.e., commands that produce an output. This is because the output destroys the concept of ebegin and eend, where the eend marker is shown on the same line that is produced by ebegin. However, it sometimes would be nice to use edob with noisy commands, but this means to redirect stdout and stderr of those commands. Instead of redirecting the output to /dev/null, we save the output in a log file under T. This allows us to present the output to the user in case the command fails, making it futhermore part of the build.log, which we expect users to attach to bug reports. Signed-off-by: Florian Schmaus --- eclass/edo.eclass | 66 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/eclass/edo.eclass b/eclass/edo.eclass index c2e7ed60083f..0d410719675c 100644 --- a/eclass/edo.eclass +++ b/eclass/edo.eclass @@ -1,4 +1,4 @@ -# Copyright 2022 Gentoo Authors +# Copyright 2022-2024 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: edo.eclass @@ -12,10 +12,14 @@ # This eclass provides the 'edo' command, and an 'edob' variant for ebegin/eend, # which logs the command used verbosely and dies (exits) on failure. # -# This eclass should be used only where needed to give a more verbose log, e.g. -# for invoking non-standard ./configure scripts, or building objects/binaries -# directly within ebuilds via compiler invocations. It is NOT to be used -# in place of generic 'command || die' where verbosity is unnecessary. +# The 'edo' command should be used only where needed to give a more verbose log, +# e.g. for invoking non-standard ./configure scripts, or building +# objects/binaries directly within ebuilds via compiler invocations. It is NOT +# to be used in place of generic 'command || die' where verbosity is +# unnecessary. +# +# The 'edob' command should be used if its output is not of general interest, +# as it will be only shown if the command returns a non-zero exit status. case ${EAPI} in 7|8) ;; *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; @@ -35,14 +39,56 @@ edo() { } # @FUNCTION: edob -# @USAGE: [...] +# @USAGE: [-m ] [-l ] [...] # @DESCRIPTION: # Executes 'command' with ebegin & eend with any given arguments and exits -# on failure unless called under 'nonfatal'. +# on failure unless called under 'nonfatal'. This function redirects +# stdout and stderr to a log file. The content of the log file is shown +# if the command returns with a non-zero exit status. +# +# If -m is provided, then invokes ebegin with , otherwise +# a default message is used. If -l is provided, then is +# used to construct the name of the log file where stdout and stderr of the +# command is redirected to. edob() { - ebegin "Running $@" - "$@" - eend $? || die -n "Failed to run command: $@" + local message + local log_name + + while true; do + case "${1}" in + -m|-n) + [[ $# -lt 2 ]] && die "Must provide an argument to ${1}" + case "${1}" in + -m) + message="${2}" + ;; + -n) + log="${2}" + ;; + esac + shift 2 + ;; + *) + break + ;; + esac + done + + [[ -z ${message} ]] && message="Running $@" + [[ -z ${log_name} ]] && log_name="$(basename ${1})" + + local log_file="${T}/${log_name}.log" + [[ -f ${log_file} ]] && die "Log file ${log_file} exists. Consider using \"edob -l\"" + + ebegin "${message}" + + "$@" &> "${log_file}" + local ret=$? + + if ! eend $ret; then + cat "${log_file}" + die -n "Command \"$@\" failed with exit status $ret" + fi } fi -- 2.43.2