>From 54bf87fe01941049356177a1902bdf1a3eb49f7f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 7 Aug 2022 13:29:59 +0200 Subject: [PATCH 06/19] gnulib-tool.py: Make regex uses more straightforward. * pygnulib/GLModuleSystem.py: Don't use flag re.S on regular expressions that are meant to match a single line only, and remove the use of the "minimal matching" *? construct whose only purpose was to neutralize the re.S flag. * pygnulib/GLEmiter.py: Likewise. * pygnulib/GLImport.py: Likewise. * pygnulib/GLTestDir.py: Likewise. --- ChangeLog | 9 +++++++++ pygnulib/GLEmiter.py | 18 +++++++++--------- pygnulib/GLImport.py | 4 ++-- pygnulib/GLModuleSystem.py | 6 +++--- pygnulib/GLTestDir.py | 20 ++++++++++---------- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index bcaedf98e4..0e162e8d0a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2022-08-07 Bruno Haible + gnulib-tool.py: Make regex uses more straightforward. + * pygnulib/GLModuleSystem.py: Don't use flag re.S on regular expressions + that are meant to match a single line only, and remove the use of the + "minimal matching" *? construct whose only purpose was to neutralize the + re.S flag. + * pygnulib/GLEmiter.py: Likewise. + * pygnulib/GLImport.py: Likewise. + * pygnulib/GLTestDir.py: Likewise. + gnulib-tool.py: Make regex uses more straightforward. * pygnulib/GLEmiter.py: Don't use flag re.S on regular expressions on regular expressions with no '.'. diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py index f85ef30d52..02fa6654e7 100644 --- a/pygnulib/GLEmiter.py +++ b/pygnulib/GLEmiter.py @@ -153,7 +153,7 @@ class GLEmiter(object): if line.strip() ] snippet = '%s\n' % '\n'.join(lines) transformer = fileassistant.transformers.get('aux', '') - pattern = re.compile('(^.*?$)', re.S | re.M) + pattern = re.compile('^(.*)$', re.M) snippet = pattern.sub('%s\\1' % indentation, snippet) if transformer: args = ['sed', '-e', transformer] @@ -182,9 +182,9 @@ class GLEmiter(object): emit += 'changequote([, ])dnl\n' emit += 'AC_SUBST([LTALLOCA])' if replace_auxdir: - regex = 'AC_CONFIG_FILES\\(\\[(.*?)\\:build-aux/(.*?)\\]\\)' + regex = 'AC_CONFIG_FILES\\(\\[(.*)\\:build-aux/(.*)\\]\\)' repl = 'AC_CONFIG_FILES([\\1:%s/\\2])' % auxdir - pattern = re.compile(regex, re.S | re.M) + pattern = re.compile(regex, re.M) emit = pattern.sub(repl, emit) lines = [ line for line in emit.split('\n') @@ -688,9 +688,9 @@ AC_DEFUN([%V1%_LIBSOURCES], [ amsnippet1 = amsnippet1.replace('lib_LIBRARIES', 'lib%_LIBRARIES') amsnippet1 = amsnippet1.replace('lib_LTLIBRARIES', 'lib%_LTLIBRARIES') if LD_flags: - pattern = re.compile('lib_LDFLAGS[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('lib_LDFLAGS[\t ]*\\+=(.*)$', re.M) amsnippet1 = pattern.sub('', amsnippet1) - pattern = re.compile('lib_([A-Z][A-Z](?:.*?))', re.S | re.M) + pattern = re.compile('lib_([A-Z][A-Z](?:.*))', re.M) amsnippet1 = pattern.sub('%s_%s_\\1' % (libname, libext), amsnippet1) amsnippet1 = amsnippet1.replace('$(GNULIB_', '$(' + module_indicator_prefix + '_GNULIB_') @@ -708,7 +708,7 @@ AC_DEFUN([%V1%_LIBSOURCES], [ # Get unconditional snippet, edit it and save to amsnippet2. amsnippet2 = module.getAutomakeSnippet_Unconditional() - pattern = re.compile('lib_([A-Z][A-Z](?:.*?))', re.S | re.M) + pattern = re.compile('lib_([A-Z][A-Z](?:.*))', re.M) amsnippet2 = pattern.sub('%s_%s_\\1' % (libname, libext), amsnippet2) amsnippet2 = amsnippet2.replace('$(GNULIB_', @@ -802,7 +802,7 @@ AC_DEFUN([%V1%_LIBSOURCES], [ insnippets = False inmakefile = False regex = '^[a-zA-Z0-9_]*_%sLIBRARIES *\\+{0,1}= *%s.%s' % (perhapsLT, libname, libext) - pattern = re.compile(regex, re.S | re.M) + pattern = re.compile(regex, re.M) insnippets = bool(pattern.findall(allsnippets)) # Then test if $sourcebase/Makefile.am (if it exists) specifies it. path = joinpath(sourcebase, 'Makefile.am') @@ -955,9 +955,9 @@ AC_DEFUN([%V1%_LIBSOURCES], [ snippet = snippet.replace('lib_LIBRARIES', 'lib%_LIBRARIES') snippet = snippet.replace('lib_LTLIBRARIES', 'lib%_LTLIBRARIES') if LD_flags: - pattern = re.compile('lib_LDFLAGS[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('lib_LDFLAGS[\t ]*\\+=(.*)$', re.M) snippet = pattern.sub('', snippet) - pattern = re.compile('lib_([A-Z][A-Z](?:.*?))', re.S | re.M) + pattern = re.compile('lib_([A-Z][A-Z](?:.*))', re.M) snippet = pattern.sub('libtests_a_\\1', snippet) snippet = snippet.replace('$(GNULIB_', '$(' + module_indicator_prefix + '_GNULIB_') snippet = snippet.replace('lib%_LIBRARIES', 'lib_LIBRARIES') diff --git a/pygnulib/GLImport.py b/pygnulib/GLImport.py index 7a6312ec39..818f3d57c0 100644 --- a/pygnulib/GLImport.py +++ b/pygnulib/GLImport.py @@ -95,7 +95,7 @@ class GLImport(object): self.config.setAutoconfFile(path) with codecs.open(path, 'rb', 'UTF-8') as file: data = file.read() - pattern = re.compile(r'^AC_CONFIG_AUX_DIR\((.*?)\)$', re.S | re.M) + pattern = re.compile(r'^AC_CONFIG_AUX_DIR\((.*)\)$', re.M) match = pattern.findall(data) if match: result = cleaner(match)[0] @@ -106,7 +106,7 @@ class GLImport(object): self.config.setAuxDir(self.cache['auxdir']) # Guess autoconf version. - pattern = re.compile(r'.*AC_PREREQ\((.*?)\)', re.S | re.M) + pattern = re.compile(r'.*AC_PREREQ\((.*)\)', re.M) versions = cleaner(pattern.findall(data)) if versions: version = sorted(set([ float(version) diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py index 6b8f20e54f..afc7a472cf 100644 --- a/pygnulib/GLModuleSystem.py +++ b/pygnulib/GLModuleSystem.py @@ -705,7 +705,7 @@ Include:|Link:|License:|Maintainer:)' # TODO: unconditional automake snippet for nontests modules snippet = self.getAutomakeSnippet_Conditional() snippet = constants.combine_lines(snippet) - pattern = re.compile('^lib_SOURCES[\t ]*\\+=[\t ]*(.*?)$', re.S | re.M) + pattern = re.compile('^lib_SOURCES[\t ]*\\+=[\t ]*(.*)$', re.M) mentioned_files = pattern.findall(snippet) if mentioned_files != list(): mentioned_files = mentioned_files[-1].split(' ') @@ -780,7 +780,7 @@ Include:|Link:|License:|Maintainer:)' parts += [line] result = ''.join(parts) result = result.strip() - pattern = re.compile('^(["<].*?[>"])', re.S | re.M) + pattern = re.compile('^(["<].*[>"])', re.M) result = pattern.sub('#include \\1', result) self.cache['include'] = result return self.cache['include'] @@ -1164,7 +1164,7 @@ class GLModuleTable(object): raise TypeError('each module must be a GLModule instance') snippet = module.getAutomakeSnippet() snippet = constants.remove_backslash_newline(snippet) - pattern = re.compile('^lib_SOURCES[\t ]*\\+=[\t ]*(.*?)$', re.S | re.M) + pattern = re.compile('^lib_SOURCES[\t ]*\\+=[\t ]*(.*)$', re.M) files = pattern.findall(snippet) if files: # if source files were found files = files[-1].split(' ') diff --git a/pygnulib/GLTestDir.py b/pygnulib/GLTestDir.py index 757c494011..f8d9910ab7 100644 --- a/pygnulib/GLTestDir.py +++ b/pygnulib/GLTestDir.py @@ -299,7 +299,7 @@ class GLTestDir(object): notice = module.getNotice() if notice: print('Notice from module %s:' % str(module)) - pattern = re.compile('^(.*?)$', re.S | re.M) + pattern = re.compile('^(.*)$', re.M) notice = pattern.sub(' \\1', notice) print(notice) else: # if not single_configure @@ -307,7 +307,7 @@ class GLTestDir(object): notice = module.getNotice() if notice: print('Notice from module %s:' % str(module)) - pattern = re.compile('^(.*?)$', re.S | re.M) + pattern = re.compile('^(.*)$', re.M) notice = pattern.sub(' \\1', notice) print(notice) @@ -461,7 +461,7 @@ class GLTestDir(object): for line in snippet.split('\n') if line.strip() ] snippet = '\n'.join(lines) - pattern = re.compile('AC_REQUIRE\\(\\[([^()].*?)\\]\\)', re.S | re.M) + pattern = re.compile('AC_REQUIRE\\(\\[([^()].*)\\]\\)', re.M) snippet = pattern.sub('\\1', snippet) snippet = snippet.strip() snippets += [snippet] @@ -578,7 +578,7 @@ class GLTestDir(object): for line in snippet.split('\n') if line.strip() ] snippet = '\n'.join(lines) - pattern = re.compile('AC_REQUIRE\\(\\[([^()].*?)\\]\\)', re.S | re.M) + pattern = re.compile('AC_REQUIRE\\(\\[([^()].*)\\]\\)', re.M) snippet = pattern.sub('\\1', snippet) snippet = snippet.strip() snippets += [snippet] @@ -749,9 +749,9 @@ class GLTestDir(object): # Extract the value of "CLEANFILES += ..." and "MOSTLYCLEANFILES += ...". regex_find = list() - pattern = re.compile('^CLEANFILES[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('^CLEANFILES[\t ]*\\+=(.*)$', re.M) regex_find += pattern.findall(snippet) - pattern = re.compile('^MOSTLYCLEANFILES[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('^MOSTLYCLEANFILES[\t ]*\\+=(.*)$', re.M) regex_find += pattern.findall(snippet) regex_find = [ line.strip() for line in regex_find @@ -765,7 +765,7 @@ class GLTestDir(object): # Extract the value of "BUILT_SOURCES += ...". Remove variable references # such $(FOO_H) because they don't refer to distributed files. regex_find = list() - pattern = re.compile('^BUILT_SOURCES[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('^BUILT_SOURCES[\t ]*\\+=(.*)$', re.M) regex_find += pattern.findall(snippet) regex_find = [ line.strip() for line in regex_find @@ -791,9 +791,9 @@ class GLTestDir(object): # Extract the value of "CLEANFILES += ..." and "MOSTLYCLEANFILES += ...". regex_find = list() - pattern = re.compile('^CLEANFILES[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('^CLEANFILES[\t ]*\\+=(.*)$', re.M) regex_find += pattern.findall(snippet) - pattern = re.compile('^MOSTLYCLEANFILES[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('^MOSTLYCLEANFILES[\t ]*\\+=(.*)$', re.M) regex_find += pattern.findall(snippet) regex_find = [ line.strip() for line in regex_find @@ -808,7 +808,7 @@ class GLTestDir(object): # such $(FOO_H) because they don't refer to distributed files. regex_find = list() tests_built_sources = list() - pattern = re.compile('^BUILT_SOURCES[\t ]*\\+=(.*?)$', re.S | re.M) + pattern = re.compile('^BUILT_SOURCES[\t ]*\\+=(.*)$', re.M) regex_find += pattern.findall(snippet) regex_find = [ line.strip() for line in regex_find -- 2.34.1