>From 72d3a4615864e9f77109e86ee4c1a6fe5fcb0946 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 31 Jul 2022 18:35:58 +0200 Subject: [PATCH 02/16] 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. --- ChangeLog | 5 +++++ pygnulib/constants.py | 22 ++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9fec245cf5..00ffeffa52 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2022-07-31 Bruno Haible + 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 diff --git a/pygnulib/constants.py b/pygnulib/constants.py index e951c906d0..9ef2e01089 100644 --- a/pygnulib/constants.py +++ b/pygnulib/constants.py @@ -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.''' -- 2.34.1