Not sure how this didn't get found sooner, to be honest. Here is an
example of the bug:

    $ git clone https://git.savannah.gnu.org/git/rcs.git
    $ cd rcs
    $ git checkout next
    $ sh -x autogen.sh
    [...]
    Traceback (most recent call last):
    File "/home/collin/.local/src/gnulib/.gnulib-tool.py", line 30, in <module>
      main.main_with_exception_handling()
    File "/home/collin/.local/src/gnulib/pygnulib/main.py", line 1382, in 
main_with_exception_handling
      main(temporary_directory)
    File "/home/collin/.local/src/gnulib/pygnulib/main.py", line 990, in main
      data = data.split('ACLOCAL_AMFLAGS')[1]
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
    IndexError: list index out of range

I don't remember if I wrote this or not. But regular expressions work
better here than str.find() and indexing. That variable is limited to a
single line so no need to worry about backslash continuations [1].

I've applied this patch fixing it and confirmed it passes with
GNULIB_TOOL_IMPL=sh+py.

Collin

[1] 
https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.72/autoconf.html#index-AC_005fCONFIG_005fMACRO_005fDIR-1

>From 80ed61e8cd8a447dbe1f16e63ac94e6762a795bf Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Sun, 2 Jun 2024 01:06:32 -0700
Subject: [PATCH] gnulib-tool.py: Fix crash when no ACLOCAL_AMFLAGS is found.

* pygnulib/main.py (main) [import]: Use a regular expression to match
the ACLOCAL_AMFLAGS Makefile.am variable. Properly handle the case where
none is found.
---
 ChangeLog        | 7 +++++++
 pygnulib/main.py | 9 ++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 67b0ea66ef..3a982c4988 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-06-02  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Fix crash when no ACLOCAL_AMFLAGS is found.
+	* pygnulib/main.py (main) [import]: Use a regular expression to match
+	the ACLOCAL_AMFLAGS Makefile.am variable. Properly handle the case where
+	none is found.
+
 2024-05-31  Bruno Haible  <br...@clisp.org>
 
 	windows-once: Improve comments.
diff --git a/pygnulib/main.py b/pygnulib/main.py
index 68be0ba28f..6167135858 100644
--- a/pygnulib/main.py
+++ b/pygnulib/main.py
@@ -987,9 +987,12 @@ def main(temp_directory: str) -> None:
                 if os.path.isfile(filepath):
                     with open(filepath, mode='r', newline='\n', encoding='utf-8') as file:
                         data = file.read()
-                    data = data.split('ACLOCAL_AMFLAGS')[1]
-                    data = data[data.find('=') + 1 : data.find('\n')]
-                    aclocal_amflags = data.split()
+                    pattern = re.compile(r'^ACLOCAL_AMFLAGS[\t ]*=[\t ]*([^#]+?)$', re.MULTILINE)
+                    match = re.search(pattern, data)
+                    if match:
+                        aclocal_amflags = match.group(1).split()
+                    else:
+                        aclocal_amflags = []
                     for aclocal_amflag in aclocal_amflags:
                         if dirisnext:
                             # Ignore absolute directory pathnames, like /usr/local/share/aclocal.
-- 
2.45.1

Reply via email to