On 7/4/24 13:38, Paul Eggert wrote:

exec python3 /home/eggert/src/gnu/gnulib-savannah/./.gnulib-tool.py --list

Come to think of it, this can be simplifed to:

exec python3 ./.gnulib-tool.py --list

which avoids the overhead of computing and using the absolute file name. This will be a minor win even after we get rid of the shell implementation of gnulib-tool. I installed the attached to do that.
From 15ec89beae6ded70d4079c5c49861d0b701a353b Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Sat, 6 Jul 2024 16:41:44 +0200
Subject: [PATCH] gnulib-tool: simplify/speed startup

* gnulib-tool, gnulib-tool.py (prog): New var.  Use it to simplify
and speed up startup in common cases.
---
 ChangeLog      |  6 ++++++
 gnulib-tool    | 38 +++++++++++++++++++++++++++-----------
 gnulib-tool.py | 16 ++++++++++++++--
 3 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b68ed39449..340efb2de3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-07-06  Paul Eggert  <egg...@cs.ucla.edu>
+
+	gnulib-tool: simplify/speed startup
+	* gnulib-tool, gnulib-tool.py (prog): New var.  Use it to simplify
+	and speed up startup in common cases.
+
 2024-07-04  Bruno Haible  <br...@clisp.org>
 
 	gitlog-to-changelog: Tweak documentation.
diff --git a/gnulib-tool b/gnulib-tool
index 789fe916a8..5cfc5bbb52 100755
--- a/gnulib-tool
+++ b/gnulib-tool
@@ -137,23 +137,35 @@ func_gnulib_dir ()
   gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
 }
 
-func_gnulib_dir
+# If $progname contains '/' and is not a symlink, it suffices for $prog to be
+# the same as $progname with except with basename 'gnulib-tool’; this
+# speeds startup and might avoid problems in oddball development environments.
+# Otherwise, $prog is the absolute name of the gnulib-tool executable.
+if case $progname in
+     */*) test -h "$0" ;;
+   esac
+then
+  func_gnulib_dir
+  prog=$gnulib_dir/gnulib-tool
+else
+  prog=${progname%/*}/gnulib-tool
+fi
 
 case "$GNULIB_TOOL_IMPL" in
   '')
     # Use the Python implementation if a suitable Python version is found
     # in $PATH. This is the same Python version test as in gnulib-tool.py.
     if (python3 -c 'import sys; sys.exit(not sys.version_info >= (3,7))') 2>/dev/null; then
-      exec "$gnulib_dir/gnulib-tool.py" "$@"
+      exec "$prog.py" "$@"
     else
       echo "gnulib-tool: warning: python3 not found or too old, using the slow shell-based implementation" 1>&2
-      exec "$gnulib_dir/gnulib-tool.sh" "$@"
+      exec "$prog.sh" "$@"
     fi
     ;;
   sh)
-    exec "$gnulib_dir/gnulib-tool.sh" "$@" ;;
+    exec "$prog.sh" "$@" ;;
   py)
-    exec "$gnulib_dir/gnulib-tool.py" "$@" ;;
+    exec "$prog.py" "$@" ;;
   sh+py)
     case " $* " in
       *" --import"* | *" --add-import"* | *" --remove-import"* | *" --update"* | *" --copy-file"*)
@@ -183,10 +195,14 @@ case "$GNULIB_TOOL_IMPL" in
           func_exit 1
         }
         # Execute gnulib-tool.py in the clone directory.
-        (cd "$tmp" && "$gnulib_dir/gnulib-tool.py" "$@" >"$tmp-py-out" 2>"$tmp-py-err")
+        case $prog in
+          /*) absprog=$prog ;;
+          *)  absprog=$PWD/prog ;;
+        esac
+        (cd "$tmp" && "$absprog.py" "$@" >"$tmp-py-out" 2>"$tmp-py-err")
         pyrc=$?
         # Execute gnulib-tool.sh in the current directory.
-        "$gnulib_dir/gnulib-tool.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
+        "$prog.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
         shrc=$?
         if test $shrc != 0; then
           if test $pyrc = 0; then
@@ -233,10 +249,10 @@ case "$GNULIB_TOOL_IMPL" in
         # Find another directory name.
         tmp="$dir-glpy$$"
         # Execute gnulib-tool.py, creating a different directory.
-        "$gnulib_dir/gnulib-tool.py" "$@" --dir="$tmp" >"$tmp-py-out" 2>"$tmp-py-err"
+        "$prog.py" "$@" --dir="$tmp" >"$tmp-py-out" 2>"$tmp-py-err"
         pyrc=$?
         # Execute gnulib-tool.sh, creating the intended directory.
-        "$gnulib_dir/gnulib-tool.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
+        "$prog.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
         shrc=$?
         if test $shrc != 0; then
           if test $pyrc = 0; then
@@ -274,10 +290,10 @@ case "$GNULIB_TOOL_IMPL" in
         # A gnulib-tool invocation that produces only output, no files.
         tmp="glpy$$"
         # Execute gnulib-tool.py.
-        "$gnulib_dir/gnulib-tool.py" "$@" >"$tmp-py-out" 2>"$tmp-py-err"
+        "$prog.py" "$@" >"$tmp-py-out" 2>"$tmp-py-err"
         pyrc=$?
         # Execute gnulib-tool.sh.
-        "$gnulib_dir/gnulib-tool.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
+        "$prog.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
         shrc=$?
         if test $shrc != 0; then
           if test $pyrc = 0; then
diff --git a/gnulib-tool.py b/gnulib-tool.py
index 52389dcd78..bd13a8aa02 100755
--- a/gnulib-tool.py
+++ b/gnulib-tool.py
@@ -128,7 +128,19 @@
   gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
 }
 
-func_gnulib_dir
+# If $progname contains '/' and is not a symlink, it suffices for $prog to be
+# the same as $progname with except with basename '.gnulib-tool.py'; this
+# speeds startup and might avoid problems in oddball development environments.
+# Otherwise, $prog is the absolute name of the .gnulib-tool.py file.
+if case $progname in
+     */*) test -h "$0" ;;
+   esac
+then
+  func_gnulib_dir
+  prog=$gnulib_dir/.gnulib-tool.py
+else
+  prog=${progname%/*}/.gnulib-tool.py
+fi
 
 # Check the Python version.
 if (python3 -c 'import sys; sys.exit(not sys.version_info >= (3,7))') 2>/dev/null; then
@@ -157,4 +169,4 @@
 profiler_args=
 # For profiling, cf. <https://docs.python.org/3/library/profile.html>.
 #profiler_args="-m cProfile -s tottime"
-exec python3 $profiler_args "$gnulib_dir/.gnulib-tool.py" "$@"
+exec python3 $profiler_args -- "$prog" "$@"
-- 
2.34.1

Reply via email to