>From 501feeb5d595d8b4cea9a1a8e404277c27d2f5bf Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 8 Aug 2022 21:22:15 +0200 Subject: [PATCH 2/5] gnulib-tool.py: Fixes for conditional dependencies. * pygnulib/GLModuleSystem.py (GLModule.shell_id_chars): New constant. (GLModule.getShellFunc): Don't use md5 just because of an '_' character. (GLModule.getShellVar): Likewise. (GLModule.getConditionalName): Include a newline in the md5 input. * pygnulib/constants.py (ALPHANUMERIC): Remove constant. --- ChangeLog | 7 ++++ pygnulib/GLModuleSystem.py | 70 ++++++++++++++++++++------------------ pygnulib/constants.py | 3 -- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 596f629316..091af1f974 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2022-08-09 Bruno Haible + gnulib-tool.py: Fixes for conditional dependencies. + * pygnulib/GLModuleSystem.py (GLModule.shell_id_chars): New constant. + (GLModule.getShellFunc): Don't use md5 just because of an '_' character. + (GLModule.getShellVar): Likewise. + (GLModule.getConditionalName): Include a newline in the md5 input. + * pygnulib/constants.py (ALPHANUMERIC): Remove constant. + gnulib-tool.py: Refactor. * pygnulib/GLModuleSystem.py (GLModule.getLicense): Separate the warning logic from the result logic. diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py index 13d6a72231..9bfd5bd266 100644 --- a/pygnulib/GLModuleSystem.py +++ b/pygnulib/GLModuleSystem.py @@ -183,6 +183,9 @@ class GLModule(object): + 'Makefile\\.am|Include|Link|License|Maintainer):$', re.M) + # List of characters allowed in shell identifiers. + shell_id_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' + def __init__(self, config, path, patched=False): '''GLModule.__init__(config, path[, patched]) -> GLModule @@ -329,20 +332,19 @@ class GLModule(object): Computes the shell function name that will contain the m4 macros for the module.''' - isalnum = True macro_prefix = self.config['macro_prefix'] - for char in str(module): - if char not in constants.ALPHANUMERIC: - isalnum = False + valid_shell_id = True + for char in self.getName(): + if char not in GLModule.shell_id_chars: + valid_shell_id = False break - if isalnum: - module = str(self) - else: # if not isalnum - module = '%s\n' % str(self) - if type(module) is str: - module = module.encode(ENCS['default']) - module = hashlib.md5(module).hexdigest() - result = 'func_%s_gnulib_m4code_%s' % (macro_prefix, module) + identifier = None + if valid_shell_id: + identifier = self.getName() + else: + hash_input = '%s\n' % self.getName() + identifier = hashlib.md5(hash_input.encode(ENCS['default'])).hexdigest() + result = 'func_%s_gnulib_m4code_%s' % (macro_prefix, identifier) return result def getShellVar(self): @@ -350,20 +352,19 @@ class GLModule(object): Compute the shell variable name the will be set to true once the m4 macros for the module have been executed.''' - isalnum = True macro_prefix = self.config['macro_prefix'] - for char in str(module): - if char not in constants.ALPHANUMERIC: - isalnum = False + valid_shell_id = True + for char in self.getName(): + if char not in GLModule.shell_id_chars: + valid_shell_id = False break - if isalnum: - module = str(self) - else: # if not isalnum - module = '%s\n' % str(self) - if type(module) is str: - module = module.encode(ENCS['default']) - module = hashlib.md5(module).hexdigest() - result = '%s_gnulib_enabled_%s' % (macro_prefix, module) + identifier = None + if valid_shell_id: + identifier = self.getName() + else: + hash_input = '%s\n' % self.getName() + identifier = hashlib.md5(hash_input.encode(ENCS['default'])).hexdigest() + result = '%s_gnulib_enabled_%s' % (macro_prefix, identifier) return result def getConditionalName(self): @@ -372,15 +373,18 @@ class GLModule(object): Return the automake conditional name. GLConfig: macro_prefix.''' macro_prefix = self.config['macro_prefix'] - nonascii = [ char - for char in self.getName() - if char not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_' ] - if nonascii: - name = self.getName().encode(ENCS['default']) - name = hashlib.md5(name).hexdigest() - conditional = '%s_GNULIB_ENABLED_%s' % (macro_prefix, name) - else: # if not nonascii - result = '%s_GNULIB_ENABLED_%s' % (macro_prefix, name) + valid_shell_id = True + for char in self.getName(): + if char not in GLModule.shell_id_chars: + valid_shell_id = False + break + identifier = None + if valid_shell_id: + identifier = self.getName() + else: + hash_input = '%s\n' % self.getName() + identifier = hashlib.md5(hash_input.encode(ENCS['default'])).hexdigest() + result = '%s_GNULIB_ENABLED_%s' % (macro_prefix, identifier) return result def getDescription(self): diff --git a/pygnulib/constants.py b/pygnulib/constants.py index ba0ebc9942..ae27d8d41a 100644 --- a/pygnulib/constants.py +++ b/pygnulib/constants.py @@ -57,9 +57,6 @@ MODES = dict() # Modes TESTS = dict() # Tests NL = ''' ''' # Newline character -ALPHANUMERIC = 'abcdefghijklmnopqrstuvwxyz\ -ABCDEFGHIJKLMNOPQRSTUVWXYZ\ -0123456789' # Alphanumeric characters # Set ENCS dictionary if not hasattr(interpreter, '__file__'): -- 2.34.1