]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Refactor duplicated regular expressions.
authorCollin Funk <collin.funk1@gmail.com>
Mon, 3 Jun 2024 05:32:39 +0000 (22:32 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Mon, 3 Jun 2024 05:32:39 +0000 (22:32 -0700)
* pygnulib/GLModuleSystem.py (_LIB_SOURCES_PATTERN): New variable.
(_extract_lib_SOURCES): New function.
(GLModule.getAutomakeSnippet_Unconditional): Use the new function.
(GLModuleTable.add_dummy): Likewise. Add a second break statement to
stop unnecessary looping.

ChangeLog
pygnulib/GLModuleSystem.py

index 269293ca79ce4fbbf5c110155e88b41d2152ba38..e0ed08a9af414df6264b088dcacbdbe86cda5600 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-06-02  Collin Funk  <collin.funk1@gmail.com>
+
+       gnulib-tool.py: Refactor duplicated regular expressions.
+       * pygnulib/GLModuleSystem.py (_LIB_SOURCES_PATTERN): New variable.
+       (_extract_lib_SOURCES): New function.
+       (GLModule.getAutomakeSnippet_Unconditional): Use the new function.
+       (GLModuleTable.add_dummy): Likewise. Add a second break statement to
+       stop unnecessary looping.
+
 2024-06-02  Bruno Haible  <bruno@clisp.org>
 
        c-strtod, c-strtof, c-strtold: Fix link error on AIX.
index 5b8d331dd647e9c86b5e158bc659f906a9208ebe..5d67c5b6df7eef7f0335d1ef70f055de2d49812e 100644 (file)
@@ -40,6 +40,21 @@ from .GLConfig import GLConfig
 from .GLFileSystem import GLFileSystem
 
 
+# Regular expression matching a 'lib_SOURCES += ...' variable with the file
+# names in capture group 1.
+_LIB_SOURCES_PATTERN = re.compile(r'^lib_SOURCES[\t ]*\+=[\t ]*(.+?)[\t ]*(?:#|$)', re.MULTILINE)
+
+
+def _extract_lib_SOURCES(snippet: str) -> list[str]:
+    '''Return the file names specified in the lib_SOURCES variable of
+    a Makefile.am snippet.'''
+    lines = [ line.group(1)
+              for line in re.finditer(_LIB_SOURCES_PATTERN, snippet) ]
+    return [ file_name
+             for line in lines
+             for file_name in line.split() ]
+
+
 #===============================================================================
 # Define GLModuleSystem class
 #===============================================================================
@@ -571,13 +586,8 @@ class GLModule:
                 # Synthesize an EXTRA_DIST augmentation.
                 snippet = self.getAutomakeSnippet_Conditional()
                 snippet = combine_lines(snippet)
-                pattern = re.compile(r'^lib_SOURCES[\t ]*\+=[\t ]*(.*)$', re.MULTILINE)
-                mentioned_files = set(pattern.findall(snippet))
-                if mentioned_files:
-                    # Get all the file names from 'lib_SOURCES += ...'.
-                    mentioned_files = { filename
-                                        for line in mentioned_files
-                                        for filename in line.split() }
+                # Get all the file names from 'lib_SOURCES += ...'.
+                mentioned_files = _extract_lib_SOURCES(snippet)
                 all_files = self.getFiles()
                 lib_files = filter_filelist('\n', all_files,
                                             'lib/', '', 'lib/', '')
@@ -985,14 +995,14 @@ class GLModuleTable:
                     # Extract the value of unconditional "lib_SOURCES += ..." augmentations.
                     snippet = combine_lines(snippet)
                     snippet = self.remove_if_blocks(snippet)
-                    pattern = re.compile(r'^lib_SOURCES[\t ]*\+=[\t ]*([^#]+?)$', re.MULTILINE)
-                    for matching_rhs in re.finditer(pattern, snippet):
-                        files = matching_rhs.group(1).split()
-                        for file in files:
-                            # Ignore .h files since they are not compiled.
-                            if not file.endswith('.h'):
-                                have_lib_sources = True
-                                break
+                    file_names = _extract_lib_SOURCES(snippet)
+                    for file_name in file_names:
+                        # Ignore .h files since they are not compiled.
+                        if not file_name.endswith('.h'):
+                            have_lib_sources = True
+                            break
+                    if have_lib_sources:
+                        break
         # Add the dummy module, to make sure the library will be non-empty.
         if not have_lib_sources:
             dummy = self.modulesystem.find('dummy')