diff options
author | Skip Montanaro <skip@pobox.com> | 2000-07-12 16:55:57 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2000-07-12 16:55:57 (GMT) |
commit | 97bc98aea784c64a74b6656f950ab2dffe84f228 (patch) | |
tree | cd39bcdb671a52e918ce9b893fa380ac23af6e51 /Lib | |
parent | 03657cfdb056dbd36db12cc3db12a6b58a962e20 (diff) | |
download | cpython-97bc98aea784c64a74b6656f950ab2dffe84f228.zip cpython-97bc98aea784c64a74b6656f950ab2dffe84f228.tar.gz cpython-97bc98aea784c64a74b6656f950ab2dffe84f228.tar.bz2 |
fixed semantics of commonprefix to work by path elements instead of
characters.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/dospath.py | 18 | ||||
-rw-r--r-- | Lib/macpath.py | 23 | ||||
-rw-r--r-- | Lib/ntpath.py | 11 | ||||
-rw-r--r-- | Lib/posixpath.py | 13 |
4 files changed, 53 insertions, 12 deletions
diff --git a/Lib/dospath.py b/Lib/dospath.py index 98efe67..5a813d0 100644 --- a/Lib/dospath.py +++ b/Lib/dospath.py @@ -102,18 +102,26 @@ def dirname(p): return split(p)[0] -def commonprefix(m): - """Return the longest prefix of all list elements.""" +# Return the longest prefix of all list elements. +def commonprefix(m): + "Given a list of pathnames, returns the longest common leading component" if not m: return '' - prefix = m[0] - for item in m: + n = m[:] + for i in range(len(n)): + n[i] = n[i].split(os.sep) + # if os.sep didn't have any effect, try os.altsep + if os.altsep and len(n[i]) == 1: + n[i] = n[i].split(os.altsep) + + prefix = n[0] + for item in n: for i in range(len(prefix)): if prefix[:i+1] <> item[:i+1]: prefix = prefix[:i] if i == 0: return '' break - return prefix + return os.sep.join(prefix) # Get size, mtime, atime of files. diff --git a/Lib/macpath.py b/Lib/macpath.py index 464bdd0..899577c 100644 --- a/Lib/macpath.py +++ b/Lib/macpath.py @@ -89,6 +89,29 @@ def dirname(s): return split(s)[0] def basename(s): return split(s)[1] +# Return the longest prefix of all list elements. +# XXX completely untested on Mac!!! + +def commonprefix(m): + "Given a list of pathnames, returns the longest common leading component" + if not m: return '' + n = m[:] + for i in range(len(n)): + n[i] = n[i].split(os.sep) + # if os.sep didn't have any effect, try os.altsep + if os.altsep and len(n[i]) == 1: + n[i] = n[i].split(os.altsep) + + prefix = n[0] + for item in n: + for i in range(len(prefix)): + if prefix[:i+1] <> item[:i+1]: + prefix = prefix[:i] + if i == 0: return '' + break + return os.sep.join(prefix) + + def isdir(s): """Return true if the pathname refers to an existing directory.""" diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 23b7ce3..a1fcaa9 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -8,7 +8,7 @@ module as os.path. import os import stat import string - +import copy # Normalize the case of a pathname and map slashes to backslashes. # Other normalizations (such as optimizing '../' away) are not done @@ -158,14 +158,17 @@ def dirname(p): def commonprefix(m): "Given a list of pathnames, returns the longest common leading component" if not m: return '' - prefix = m[0] - for item in m: + n = copy.copy(m) + for i in range(len(n)): + n[i] = n[i].split(os.sep) + prefix = n[0] + for item in n: for i in range(len(prefix)): if prefix[:i+1] <> item[:i+1]: prefix = prefix[:i] if i == 0: return '' break - return prefix + return os.sep.join(prefix) # Get size, mtime, atime of files. diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 14b9270..1be5d29 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -118,14 +118,21 @@ def dirname(p): def commonprefix(m): "Given a list of pathnames, returns the longest common leading component" if not m: return '' - prefix = m[0] - for item in m: + n = m[:] + for i in range(len(n)): + n[i] = n[i].split(os.sep) + # if os.sep didn't have any effect, try os.altsep + if os.altsep and len(n[i]) == 1: + n[i] = n[i].split(os.altsep) + + prefix = n[0] + for item in n: for i in range(len(prefix)): if prefix[:i+1] <> item[:i+1]: prefix = prefix[:i] if i == 0: return '' break - return prefix + return os.sep.join(prefix) # Get size, mtime, atime of files. |