summaryrefslogtreecommitdiff
path: root/pygnulib/constants.py
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2022-07-31 18:35:58 +0200
committerBruno Haible <bruno@clisp.org>2022-07-31 23:52:25 +0200
commit18dcc627ddeced96bc192e169bf19e1a38c53374 (patch)
treec6598d1c1b79331748a4bdcd641469ba8b161329 /pygnulib/constants.py
parentcc88acbb6555991c2ee1ed22ad11215a904c0761 (diff)
downloadgnulib-18dcc627ddeced96bc192e169bf19e1a38c53374.tar.gz
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.
Diffstat (limited to 'pygnulib/constants.py')
-rw-r--r--pygnulib/constants.py22
1 files changed, 12 insertions, 10 deletions
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.'''