diff options
author | Florian Schmaus <flow@gentoo.org> | 2024-04-04 17:22:25 +0200 |
---|---|---|
committer | Florian Schmaus <flow@gentoo.org> | 2024-05-14 09:58:12 +0200 |
commit | fa90907e9d23cbbaa15567eb9924b604740aacd6 (patch) | |
tree | 8ec5cf05e3e19d47ad875f538970e49c924f8ca9 /eclass/edo.eclass | |
parent | net-misc/pedro: remove unused patch (diff) | |
download | gentoo-fa90907e9d23cbbaa15567eb9924b604740aacd6.tar.gz gentoo-fa90907e9d23cbbaa15567eb9924b604740aacd6.tar.bz2 gentoo-fa90907e9d23cbbaa15567eb9924b604740aacd6.zip |
edo.eclass: enhance edob for usage with noisy commands
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 furthermore part of the build.log, which we
expect users to attach to bug reports.
Closes: https://github.com/gentoo/gentoo/pull/36117
Signed-off-by: Florian Schmaus <flow@gentoo.org>
Diffstat (limited to 'eclass/edo.eclass')
-rw-r--r-- | eclass/edo.eclass | 68 |
1 files changed, 58 insertions, 10 deletions
diff --git a/eclass/edo.eclass b/eclass/edo.eclass index c2e7ed60083f..ed8ec8d3201e 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,16 @@ # 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 can be used for long running commands, even if +# those commands produce output. The 'edob' command will suppress the +# command's output and only present it if the command returned with a +# non-zero exit status. case ${EAPI} in 7|8) ;; *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; @@ -35,14 +41,56 @@ edo() { } # @FUNCTION: edob -# @USAGE: <command> [<args>...] +# @USAGE: [-l <log-name>] [-m <message>] <command> [<args>...] # @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 <message> is provided, then invokes ebegin with <message>, otherwise +# a default message is used. If -l <log-name> is provided, then <log-name> 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 + -l|-m) + [[ $# -lt 2 ]] && die "Must provide an argument to ${1}" + case "${1}" in + -l) + log_name="${2}" + ;; + -m) + message="${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 |