>From 276c20ee288873b519be96ffe9b3a98bfe3bb8f1 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 8 Aug 2022 00:02:59 +0200 Subject: [PATCH 17/19] gnulib-tool.py: Fix --extract-dependencies result. * pygnulib/GLModuleSystem.py (GLModule.getDependencies): Return a snippet, not a list. Implement dependency of ${module}-tests on ${module}. (GLModule.getDependenciesWithoutConditions, GLModule.getDependenciesWithConditions): New methods. (GLModuleTable.transitive_closure): Call getDependenciesWithConditions. * pygnulib/GLEmiter.py (GLEmiter.autoconfSnippets): Call getDependenciesWithoutConditions. * gnulib-tool.py (main) [--extract-dependencies]: Update. --- ChangeLog | 11 +++++ gnulib-tool.py | 10 +---- pygnulib/GLEmiter.py | 4 +- pygnulib/GLModuleSystem.py | 83 ++++++++++++++++++++++++++++---------- 4 files changed, 75 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1278d8e4d9..beb60578d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2022-08-07 Bruno Haible + gnulib-tool.py: Fix --extract-dependencies result. + * pygnulib/GLModuleSystem.py (GLModule.getDependencies): Return a + snippet, not a list. Implement dependency of ${module}-tests on + ${module}. + (GLModule.getDependenciesWithoutConditions, + GLModule.getDependenciesWithConditions): New methods. + (GLModuleTable.transitive_closure): Call getDependenciesWithConditions. + * pygnulib/GLEmiter.py (GLEmiter.autoconfSnippets): Call + getDependenciesWithoutConditions. + * gnulib-tool.py (main) [--extract-dependencies]: Update. + gnulib-tool.py: Rename a method. * pygnulib/GLModuleSystem.py (GLModule.getAutoconfEarlySnippet): Renamed from GLModule.getAutoconfSnippet_Early. diff --git a/gnulib-tool.py b/gnulib-tool.py index 0e888e6fd8..0cd5accd21 100755 --- a/gnulib-tool.py +++ b/gnulib-tool.py @@ -1013,7 +1013,6 @@ def main(): print('\n'.join(files)) elif mode == 'extract-dependencies': - result = '' if avoids: message = '%s: *** ' % constants.APP['name'] message += 'cannot combine --avoid and --extract-dependencies\n' @@ -1024,14 +1023,7 @@ def main(): modules = [ modulesystem.find(module) for module in modules ] for module in modules: - dependencies = module.getDependencies() - if dependencies: - for depmodule, condition in dependencies: - if condition == None: - result += '%s\n' % str(depmodule) - else: # if condition != None - result += '%s\t%s' % (str(depmodule), condition) - print(result) + sys.stdout.write(module.getDependencies()) elif mode == 'extract-autoconf-snippet': modulesystem = classes.GLModuleSystem(config) diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py index 61cabc92fa..a02b44bdd5 100644 --- a/pygnulib/GLEmiter.py +++ b/pygnulib/GLEmiter.py @@ -309,9 +309,7 @@ class GLEmiter(object): emit += self.autoconfSnippet(module, fileassistant, toplevel, disable_libtool, disable_gettext, replace_auxdir, ' ') emit += ' %s=true\n' % shellvar - dependencies = module.getDependencies() - depmodules = [ pair[0] - for pair in dependencies ] + depmodules = module.getDependenciesWithoutConditions() # Intersect dependencies with the modules list. depmodules = [ dep for dep in depmodules diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py index 14bc089b27..19d13d213b 100644 --- a/pygnulib/GLModuleSystem.py +++ b/pygnulib/GLModuleSystem.py @@ -452,33 +452,74 @@ class GLModule(object): return self.cache['files'] def getDependencies(self): - '''GLModule.getDependencies() -> list + '''GLModule.getDependencies() -> str - Return list of dependencies. + Return list of dependencies, as a snippet. GLConfig: localpath.''' if 'dependencies' not in self.cache: + result = '' + # ${module}-tests implicitly depends on ${module}, if that module exists. + if self.isTests(): + main_module = subend('-tests', '', self.getName()) + if self.modulesystem.exists(main_module): + result += '%s\n' % main_module + # Then the explicit dependencies listed in the module description. snippet = self.sections.get('Depends-on', '') - modules = [ line.strip() - for line in snippet.split('\n') - if line.strip() ] - modules = [ module - for module in modules - if not module.startswith('#') ] - result = list() - for line in modules: - split = [ part - for part in line.split(' ') - if part.strip() ] - if len(split) == 1: - module = line.strip() - condition = None - else: # if len(split) != 1 - module = split[0] - condition = split[1] - result += [tuple([self.modulesystem.find(module), condition])] + # Remove comment lines. + snippet = re.compile('^#.*$[\n]', re.M).sub('', snippet) + result += snippet self.cache['dependencies'] = result return self.cache['dependencies'] + def getDependenciesWithoutConditions(self): + '''GLModule.getDependenciesWithoutConditions() -> list + + Return list of dependencies, as a list of GLModule objects. + GLConfig: localpath.''' + if 'dependenciesWithoutCond' not in self.cache: + snippet = self.getDependencies() + lines = [ line.strip() + for line in snippet.split('\n') + if line.strip() ] + pattern = re.compile(' *\\[.*$') + lines = [ pattern.sub('', line) + for line in lines ] + result = [ self.modulesystem.find(module) + for module in lines + if module != '' ] + self.cache['dependenciesWithoutCond'] = result + return self.cache['dependenciesWithoutCond'] + + def getDependenciesWithConditions(self): + '''GLModule.getDependenciesWithConditions() -> list + + Return list of dependencies, as a list of pairs (GLModule object, condition). + The "true" condition is denoted by None. + GLConfig: localpath.''' + + if 'dependenciesWithCond' not in self.cache: + snippet = self.getDependencies() + lines = [ line.strip() + for line in snippet.split('\n') + if line.strip() ] + pattern = re.compile(' *\\[') + result = [] + for line in lines: + match = pattern.search(line) + if match: + module = line[0 : match.start()] + condition = line[match.end() :] + condition = subend(']', '', condition) + else: + module = line + condition = None + if module != '': + if condition == 'true': + condition = None + result.append(tuple([self.modulesystem.find(module), condition])) + self.cache['dependenciesWithCond'] = result + return self.cache['dependenciesWithCond'] + def getAutoconfEarlySnippet(self): '''GLModule.getAutoconfEarlySnippet() -> str @@ -798,7 +839,7 @@ class GLModuleTable(object): if not pattern.findall(automake_snippet): self.addUnconditional(module) conditional = self.isConditional(module) - dependencies = module.getDependencies() + dependencies = module.getDependenciesWithConditions() depmodules = [ pair[0] for pair in dependencies ] conditions = [ pair[1] -- 2.34.1