]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Reduce code duplication.
authorBruno Haible <bruno@clisp.org>
Sat, 13 Aug 2022 11:18:06 +0000 (13:18 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 13 Aug 2022 11:18:06 +0000 (13:18 +0200)
* pygnulib/constants.py (relinverse): New function.
* pygnulib/GLEmiter.py (GLEmiter.po_Makevars,
GLEmiter.tests_Makefile_am): Use it.
* pygnulib/GLTestDir.py (GLTestDir.execute): Likewise.

ChangeLog
pygnulib/GLEmiter.py
pygnulib/GLTestDir.py
pygnulib/constants.py

index 0d04cb70e70aaa8d9a9af1299b6df0152254f45f..1687348abec4db6d6abb79d025a4b964311d4319 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-08-13  Bruno Haible  <bruno@clisp.org>
+
+       gnulib-tool.py: Reduce code duplication.
+       * pygnulib/constants.py (relinverse): New function.
+       * pygnulib/GLEmiter.py (GLEmiter.po_Makevars,
+       GLEmiter.tests_Makefile_am): Use it.
+       * pygnulib/GLTestDir.py (GLTestDir.execute): Likewise.
+
 2022-08-12  Marc Nieper-Wißkirchen  <marc@nieper-wisskirchen.de>
 
        hamt: fix technically undefined behavior
index c0fd053077ce5db78021fa439f8e69926dc7ab91..4c8f0d90f246bf6310667e1c0eced8d25a0aa2d2 100644 (file)
@@ -42,6 +42,7 @@ __copyright__ = constants.__copyright__
 #===============================================================================
 TESTS = constants.TESTS
 joinpath = constants.joinpath
+relinverse = constants.relinverse
 isfile = os.path.isfile
 normpath = os.path.normpath
 
@@ -390,13 +391,7 @@ class GLEmiter(object):
         emit = ''
         pobase = self.config['pobase']
         podomain = self.config['podomain']
-        top_subdir = ''
-        source = '%s/' % os.path.normpath(pobase)
-        if os.path.sep in source:
-            for directory in source.split(os.path.sep):
-                if directory != '':
-                    top_subdir += '../'
-        top_subdir = os.path.normpath(top_subdir)
+        top_subdir = relinverse(pobase)
         emit += "## DO NOT EDIT! GENERATED AUTOMATICALLY!\n"
         emit += "%s\n" % self.copyright_notice()
         emit += "# Usually the message domain is the same as the package name.\n"
@@ -941,13 +936,8 @@ AC_DEFUN([%V1%_LIBSOURCES], [
         else:  # if not for_test
             edit_check_PROGRAMS = False
 
-        # Calculate testsbase_inverse
-        counter = int()
-        testsbase_inverse = ''
-        while counter < len(testsbase.split('/')):
-            testsbase_inverse += '../'
-            counter += 1
-        testsbase_inverse = os.path.normpath(testsbase_inverse)
+        # Compute testsbase_inverse
+        testsbase_inverse = relinverse(testsbase)
 
         # Begin the generation.
         emit += "## DO NOT EDIT! GENERATED AUTOMATICALLY!\n"
index bf0009928319ba95a391560d3ca07601c1cc5307..68cc5ce41113e5472d8fc3d06fe8552bedf308b6 100644 (file)
@@ -48,6 +48,7 @@ DIRS = constants.DIRS
 UTILS = constants.UTILS
 TESTS = constants.TESTS
 joinpath = constants.joinpath
+relinverse = constants.relinverse
 copyfile = constants.copyfile
 movefile = constants.movefile
 isdir = os.path.isdir
@@ -405,16 +406,7 @@ class GLTestDir(object):
                     file.write(emit)
                 # Viewed from the $testsbase subdirectory, $auxdir is different.
                 emit = ''
-                saved_auxdir = self.config['auxdir']
-                testsbase = '%s/' % os.path.normpath(testsbase)
-                counter = int()
-                auxdir = ''
-                finish = (len(testsbase.split('/')) - 1)
-                while counter < finish:
-                    auxdir += '../'
-                    counter += 1
-                auxdir = os.path.normpath(joinpath(auxdir, saved_auxdir))
-                testsbase = os.path.normpath(testsbase)
+                auxdir = os.path.normpath(joinpath(relinverse(testsbase), auxdir))
                 self.config.setAuxDir(auxdir)
                 # Create $testsbase/configure.ac.
                 emit += '# Process this file with autoconf '
index ae27d8d41a087ad1d6f4d63da0927ae5c5aac76f..10e11623b4d9083bacde9a069baa4232ae26b190 100644 (file)
@@ -299,6 +299,21 @@ def relconcat(dir1, dir2):
     return os.path.normpath(os.path.join(dir1, dir2))
 
 
+def relinverse(dir):
+    '''Compute the inverse of dir. Namely, a relative pathname consisting only
+    of '..' components, such that dir/relinverse = '.'.
+    dir must be a relative pathname.'''
+    if False:
+        # This should work too.
+        return relativize(dir, '.')
+    else:
+        inverse = ''
+        for component in dir.split('/'):
+            if component != '':
+                inverse += '../'
+        return os.path.normpath(inverse)
+
+
 def copyfile(src, dest):
     '''Copy file src to file dest. Like shutil.copy, but ignore errors e.g. on
     VFAT file systems.'''