public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: Florian Schmaus <flow@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Cc: qa@gentoo.org, Florian Schmaus <flow@gentoo.org>
Subject: [gentoo-dev] [PATCH 1/2] edo.eclass: enhace edob for usage with nosiy commands
Date: Wed,  8 May 2024 19:15:04 +0200	[thread overview]
Message-ID: <20240508171505.48392-2-flow@gentoo.org> (raw)
In-Reply-To: <20240508171505.48392-1-flow@gentoo.org>

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 <flow@gentoo.org>
---
 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: <command> [<args>...]
+# @USAGE: [-m <message>] [-l <log-name>] <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
+			-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



  reply	other threads:[~2024-05-08 17:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-08 17:15 [gentoo-dev] [PATCH 0/2] edo.eclass: enhace edob for usage with nosiy commands Florian Schmaus
2024-05-08 17:15 ` Florian Schmaus [this message]
2024-05-19  9:06   ` [gentoo-dev] [PATCH 1/2] " gentoo
2024-05-08 17:15 ` [gentoo-dev] [PATCH 2/2] eftmutil-sys: use edob Florian Schmaus
2024-05-13 15:05 ` [gentoo-dev] [PATCH 0/2] edo.eclass: enhace edob for usage with nosiy commands Sam James

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240508171505.48392-2-flow@gentoo.org \
    --to=flow@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    --cc=qa@gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox