Re: [gentoo-dev] [PATCH 1/2] edo.eclass: enhace edob for usage with nosiy commands

2024-05-19 Thread gentoo
Hello,

08.05.2024 19:15:52 Florian Schmaus :
[..]
> # @FUNCTION: edob
> -# @USAGE:  [...]
> +# @USAGE: [-m ] [-l ]  [...]
 ^^!
[..]
> -l  is provided, then  is
[..]
> edob() {
[..]
> +   while true; do
> +   case "${1}" in
> +   -m|-n)
 ^^! ITYM '-l' here

> +   [[ $# -lt 2 ]] && die "Must provide an argument to ${1}"
> +   case "${1}" in
> +   -m)
> +   message="${2}"
> +   ;;
> +   -n)
  ^^! ITYM '-l' here

> +   log="${2}"
>
[..]

Fix yer option character ;)

-dnh



[gentoo-dev] [PATCH 1/2] edo.eclass: enhace edob for usage with nosiy commands

2024-05-08 Thread Florian Schmaus
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