]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Improve the primitives for relative file names.
authorBruno Haible <bruno@clisp.org>
Sun, 31 Jul 2022 16:35:58 +0000 (18:35 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 31 Jul 2022 21:52:25 +0000 (23:52 +0200)
* pygnulib/constants.py (relativize): Don't attempt to handle absolute
file names. Fix bug with relativize('../foo/bar', '../foo/bla/zut').
(relconcat): New function.

ChangeLog
pygnulib/constants.py

index e827c496b672cbd7dedbc7ee6c28a97ed2ba5bd2..78ba9e6b796fcc03e6405390fb74960a9557f9bb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2022-07-31  Bruno Haible  <bruno@clisp.org>
 
+       gnulib-tool.py: Improve the primitives for relative file names.
+       * pygnulib/constants.py (relativize): Don't attempt to handle absolute
+       file names. Fix bug with relativize('../foo/bar', '../foo/bla/zut').
+       (relconcat): New function.
+
        gnulib-tool.py: Follow gnulib-tool changes, part 18.
        Follow gnulib-tool change
        2005-09-20  Bruno Haible  <bruno@clisp.org>
index e951c906d0c71e5e8d26f28b5ce7916e5598011e..9ef2e01089c1b44bd5c82366eccdf49134042609 100644 (file)
@@ -272,25 +272,20 @@ def joinpath(head, *tail):
 
 
 def relativize(dir1, dir2):
-    '''Compute a relative pathname reldir such that dir1/reldir = dir2.'''
+    '''Compute a relative pathname reldir such that dir1/reldir = dir2.
+    dir1 and dir2 must be relative pathnames.'''
     dir0 = os.getcwd()
     while dir1:
         dir1 = '%s%s' % (os.path.normpath(dir1), os.path.sep)
         dir2 = '%s%s' % (os.path.normpath(dir2), os.path.sep)
-        if dir1.startswith(os.path.sep):
-            first = dir1[:dir1.find(os.path.sep, 1)]
-        else:  # if not dir1.startswith('/')
-            first = dir1[:dir1.find(os.path.sep)]
+        first = dir1[:dir1.find(os.path.sep)]
         if first != '.':
             if first == '..':
-                dir2 = os.path.basename(joinpath(dir0, dir2))
+                dir2 = joinpath(os.path.basename(dir0), dir2)
                 dir0 = os.path.dirname(dir0)
             else:  # if first != '..'
                 # Get first component of dir2
-                if dir2.startswith(os.path.sep):
-                    first2 = dir2[:dir2.find(os.path.sep, 1)]
-                else:  # if not dir1.startswith('/')
-                    first2 = dir2[:dir2.find(os.path.sep)]
+                first2 = dir2[:dir2.find(os.path.sep)]
                 if first == first2:
                     dir2 = dir2[dir2.find(os.path.sep) + 1:]
                 else:  # if first != first2
@@ -301,6 +296,13 @@ def relativize(dir1, dir2):
     return result
 
 
+def relconcat(dir1, dir2):
+    '''Compute a relative pathname dir1/dir2, with obvious simplifications.
+    dir1 and dir2 must be relative pathnames.
+    dir2 is considered to be relative to dir1.'''
+    return os.path.normpath(os.path.join(dir1, dir2))
+
+
 def link_relative(src, dest):
     '''Like ln -s, except that src is given relative to the current directory
     (or absolute), not given relative to the directory of dest.'''