mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
fixed semantics of commonprefix to work by path elements instead of
characters.
This commit is contained in:
parent
03657cfdb0
commit
97bc98aea7
4 changed files with 53 additions and 12 deletions
|
@ -102,18 +102,26 @@ def dirname(p):
|
||||||
return split(p)[0]
|
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 ''
|
if not m: return ''
|
||||||
prefix = m[0]
|
n = m[:]
|
||||||
for item in 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)):
|
for i in range(len(prefix)):
|
||||||
if prefix[:i+1] <> item[:i+1]:
|
if prefix[:i+1] <> item[:i+1]:
|
||||||
prefix = prefix[:i]
|
prefix = prefix[:i]
|
||||||
if i == 0: return ''
|
if i == 0: return ''
|
||||||
break
|
break
|
||||||
return prefix
|
return os.sep.join(prefix)
|
||||||
|
|
||||||
|
|
||||||
# Get size, mtime, atime of files.
|
# Get size, mtime, atime of files.
|
||||||
|
|
|
@ -89,6 +89,29 @@ def dirname(s): return split(s)[0]
|
||||||
def basename(s): return split(s)[1]
|
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):
|
def isdir(s):
|
||||||
"""Return true if the pathname refers to an existing directory."""
|
"""Return true if the pathname refers to an existing directory."""
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ module as os.path.
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
import string
|
import string
|
||||||
|
import copy
|
||||||
|
|
||||||
# Normalize the case of a pathname and map slashes to backslashes.
|
# Normalize the case of a pathname and map slashes to backslashes.
|
||||||
# Other normalizations (such as optimizing '../' away) are not done
|
# Other normalizations (such as optimizing '../' away) are not done
|
||||||
|
@ -158,14 +158,17 @@ def dirname(p):
|
||||||
def commonprefix(m):
|
def commonprefix(m):
|
||||||
"Given a list of pathnames, returns the longest common leading component"
|
"Given a list of pathnames, returns the longest common leading component"
|
||||||
if not m: return ''
|
if not m: return ''
|
||||||
prefix = m[0]
|
n = copy.copy(m)
|
||||||
for item in 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)):
|
for i in range(len(prefix)):
|
||||||
if prefix[:i+1] <> item[:i+1]:
|
if prefix[:i+1] <> item[:i+1]:
|
||||||
prefix = prefix[:i]
|
prefix = prefix[:i]
|
||||||
if i == 0: return ''
|
if i == 0: return ''
|
||||||
break
|
break
|
||||||
return prefix
|
return os.sep.join(prefix)
|
||||||
|
|
||||||
|
|
||||||
# Get size, mtime, atime of files.
|
# Get size, mtime, atime of files.
|
||||||
|
|
|
@ -118,14 +118,21 @@ def dirname(p):
|
||||||
def commonprefix(m):
|
def commonprefix(m):
|
||||||
"Given a list of pathnames, returns the longest common leading component"
|
"Given a list of pathnames, returns the longest common leading component"
|
||||||
if not m: return ''
|
if not m: return ''
|
||||||
prefix = m[0]
|
n = m[:]
|
||||||
for item in 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)):
|
for i in range(len(prefix)):
|
||||||
if prefix[:i+1] <> item[:i+1]:
|
if prefix[:i+1] <> item[:i+1]:
|
||||||
prefix = prefix[:i]
|
prefix = prefix[:i]
|
||||||
if i == 0: return ''
|
if i == 0: return ''
|
||||||
break
|
break
|
||||||
return prefix
|
return os.sep.join(prefix)
|
||||||
|
|
||||||
|
|
||||||
# Get size, mtime, atime of files.
|
# Get size, mtime, atime of files.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue