This patch is just one added function. Can you double check that I
understood your 'sed' wizardry correctly Bruno?

I read the GNU sed info page a bit, but I could have missed something.
My normal usage of sed is using 's/pattern/replacement/g' and hoping
that it doesn't break too much. :)

I placed the sed command and a modified version of my Python function in
two scripts and compared their output using 'modules/stdio'. They
produced the same output.

The cleansed.append('') call seemed to make them output the same
number of newlines. It looks a bit strange, but that is my reasoning
for it

Collin
From 6b1cd6387931af81388574826ba38fe466da7233 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Sun, 17 Mar 2024 14:51:18 -0700
Subject: [PATCH] gnulib-tool.py: Follow gnulib-tool changes, part 63.

Follow gnulib-tool change
2020-12-28  Bruno Haible  <br...@clisp.org>
gnulib-tool: Fix logic whether to add a dummy.c.

* pygnulib/GLModuleSystem.py (GLModuleTable.remove_if_blocks): New
function.
(GLModuleTable.add_dummy): Use it to eliminate all conditional
statements from the automake snippet.
---
 ChangeLog                  | 11 +++++++++++
 gnulib-tool.py.TODO        | 12 ------------
 pygnulib/GLModuleSystem.py | 23 ++++++++++++++++++++++-
 3 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b1f7a3d1d3..9471f25a6e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-03-17  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Follow gnulib-tool changes, part 63.
+	Follow gnulib-tool change
+	2020-12-28  Bruno Haible  <br...@clisp.org>
+	gnulib-tool: Fix logic whether to add a dummy.c.
+	* pygnulib/GLModuleSystem.py (GLModuleTable.remove_if_blocks): New
+	function.
+	(GLModuleTable.add_dummy): Use it to eliminate all conditional
+	statements from the automake snippet.
+
 2024-03-17  Collin Funk  <collin.fu...@gmail.com>
 
 	gnulib-tool.py: Don't print extra newlines.
diff --git a/gnulib-tool.py.TODO b/gnulib-tool.py.TODO
index 9a22e0ee77..8178b2fbfd 100644
--- a/gnulib-tool.py.TODO
+++ b/gnulib-tool.py.TODO
@@ -116,18 +116,6 @@ Date:   Sun Dec 19 12:49:16 2021 +0100
 
 --------------------------------------------------------------------------------
 
-commit 0be855ee827bf7e9043eeb626c4fd847704be2e6
-Author: Bruno Haible <br...@clisp.org>
-Date:   Tue Dec 29 02:48:31 2020 +0100
-
-    gnulib-tool: Fix logic whether to add a dummy.c.
-
-    * gnulib-tool (func_remove_if_blocks): New function.
-    (func_modules_add_dummy): Use it to eliminate all conditional statements
-    from the automake snippet.
-
---------------------------------------------------------------------------------
-
 commit 30459fe101541698ec704acb224946d73676750e
 Author: Bruno Haible <br...@clisp.org>
 Date:   Thu Jun 8 15:09:31 2017 +0200
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index 37433a42db..8bc1108944 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -1049,6 +1049,26 @@ class GLModuleTable(object):
         result = tuple([main_modules, tests_modules])
         return result
 
+    def remove_if_blocks(self, snippet: str) -> str:
+        '''Removes if...endif blocks from an automake snippet.'''
+        lines = snippet.splitlines()
+        cleansed = []
+        depth = 0
+        for line in lines:
+            if line.startswith('if '):
+                depth += 1
+                # Make sure gnulib-tool.py and gnulib-tool.sh produce the same
+                # output.
+                cleansed.append('')
+            elif line.startswith('endif'):
+                depth -= 1
+            elif depth == 0:
+                cleansed.append(line)
+        if len(cleansed) > 0:
+            return '\n'.join(cleansed)
+        else:
+            return ''
+
     def add_dummy(self, modules):
         '''GLModuleTable.add_dummy(modules) -> list
 
@@ -1071,8 +1091,9 @@ class GLModuleTable(object):
                     pass
                 else:
                     snippet = module.getAutomakeSnippet()
-                    # Extract the value of "lib_SOURCES += ...".
+                    # Extract the value of unconditional "lib_SOURCES += ..." augmentations.
                     snippet = constants.remove_backslash_newline(snippet)
+                    snippet = self.remove_if_blocks(snippet)
                     pattern = re.compile('^lib_SOURCES[\t ]*\\+=([^#]*).*$', re.M)
                     for matching_rhs in pattern.findall(snippet):
                         files = matching_rhs.split(' ')
-- 
2.44.0

Reply via email to