本文整理汇总了Python中posixpath.commonprefix函数的典型用法代码示例。如果您正苦于以下问题:Python commonprefix函数的具体用法?Python commonprefix怎么用?Python commonprefix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了commonprefix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Canonicalize
def Canonicalize(self, path):
'''Returns the canonical path for |path|.
'''
canonical_paths, simplified_paths_map = self._LoadCache().Get()
# Path may already be the canonical path.
if path in canonical_paths:
return path
# Path not found. Our single heuristic: find |base| in the directory
# structure with the longest common prefix of |path|.
_, base = SplitParent(path)
potential_paths = simplified_paths_map.get(_SimplifyFileName(base))
if not potential_paths:
# There is no file with anything close to that name.
return path
# The most likely canonical file is the one with the longest common prefix
# with |path|. This is slightly weaker than it could be; |path| is
# compared, not the simplified form of |path|, which may matter.
max_prefix = potential_paths[0]
max_prefix_length = len(posixpath.commonprefix((max_prefix, path)))
for path_for_file in potential_paths[1:]:
prefix_length = len(posixpath.commonprefix((path_for_file, path)))
if prefix_length > max_prefix_length:
max_prefix, max_prefix_length = path_for_file, prefix_length
return max_prefix
示例2: test_commonprefix
def test_commonprefix(self):
self.assertEqual(
posixpath.commonprefix([]),
""
)
self.assertEqual(
posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
"/home/swen"
)
self.assertEqual(
posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
"/home/swen/"
)
self.assertEqual(
posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
"/home/swen/spam"
)
testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', 'aXc', 'abd', 'ab', 'aX', 'abcX']
for s1 in testlist:
for s2 in testlist:
p = posixpath.commonprefix([s1, s2])
self.assert_(s1.startswith(p))
self.assert_(s2.startswith(p))
if s1 != s2:
n = len(p)
self.assertNotEqual(s1[n:n+1], s2[n:n+1])
示例3: test_set_lifecycle_wildcard
def test_set_lifecycle_wildcard(self):
bucket1_uri = self.CreateBucket()
bucket2_uri = self.CreateBucket()
# This just double checks that the common prefix of the two buckets is what
# we think it should be (based on implementation detail of CreateBucket).
# We want to be careful when setting a wildcard on buckets to make sure we
# don't step outside the test buckets to effect other buckets.
common_prefix = posixpath.commonprefix([suri(bucket1_uri),
suri(bucket2_uri)])
self.assertTrue(common_prefix.startswith(
'gs://gsutil-test-test_set_lifecycle_wildcard'))
wildcard = '%s*' % common_prefix
fpath = self.CreateTempFile(contents=self.valid_doc)
stderr = self.RunGsUtil(['lifecycle', 'set', fpath, wildcard],
return_stderr=True)
self.assertIn('Setting lifecycle configuration on %s/...' %
suri(bucket1_uri), stderr)
self.assertIn('Setting lifecycle configuration on %s/...' %
suri(bucket2_uri), stderr)
self.assertEqual(stderr.count('Setting lifecycle configuration'), 2)
stdout = self.RunGsUtil(['lifecycle', 'get', suri(bucket1_uri)],
return_stdout=True)
self.assertEqual(stdout, self.valid_doc)
stdout = self.RunGsUtil(['lifecycle', 'get', suri(bucket2_uri)],
return_stdout=True)
self.assertEqual(stdout, self.valid_doc)
示例4: _relpath
def _relpath(path, start=None):
"""Return a relative version of a path.
Implementation by James Gardner in his BareNecessities
package, under MIT licence.
With a fix for Windows where posixpath.sep (and functions like
join) use the Unix slash not the Windows slash.
"""
import posixpath
if start is None:
start = posixpath.curdir
else:
start = start.replace(os.path.sep, posixpath.sep)
if not path:
raise ValueError("no path specified")
else:
path = path.replace(os.path.sep, posixpath.sep)
start_list = posixpath.abspath(start).split(posixpath.sep)
path_list = posixpath.abspath(path).split(posixpath.sep)
# Work out how much of the filepath is shared by start and path.
i = len(posixpath.commonprefix([start_list, path_list]))
rel_list = [posixpath.pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return posixpath.curdir.replace(posixpath.sep, os.path.sep)
return posixpath.join(*rel_list).replace(posixpath.sep, os.path.sep)
示例5: commit
def commit(self, url, commit):
if commit.type != 'svn' or commit.format != 1:
logging.info("SKIP unknown commit format (%s.%d)",
commit.type, commit.format)
return
logging.info("COMMIT r%d (%d paths) from %s"
% (commit.id, len(commit.changed), url))
paths = map(self._normalize_path, commit.changed)
if len(paths):
pre = posixpath.commonprefix(paths)
if pre == "/websites/":
# special case for svnmucc "dynamic content" buildbot commits
# just take the first production path to avoid updating all cms working copies
for p in paths:
m = PRODUCTION_RE_FILTER.match(p)
if m:
pre = m.group(0)
break
#print "Common Prefix: %s" % (pre)
wcs = [wc for wc in self.watch if wc.update_applies(commit.repository, pre)]
logging.info("Updating %d WC for r%d" % (len(wcs), commit.id))
for wc in wcs:
self.worker.add_work(OP_UPDATE, wc)
示例6: fill_in_extra_args
def fill_in_extra_args(self, commit):
# Set any empty members to the string "<null>"
v = vars(commit)
for k in v.keys():
if not v[k]:
v[k] = '<null>'
self._generate_dirs_changed(commit)
# Add entries to the commit object that are useful for
# formatting.
commit.log_firstline = commit.log.split("\n",1)[0]
commit.log_firstparagraph = re.split("\r?\n\r?\n",commit.log,1)[0]
commit.log_firstparagraph = re.sub("\r?\n"," ",commit.log_firstparagraph)
if commit.dirs_changed:
commit.dirs_root = posixpath.commonprefix(commit.dirs_changed)
if commit.dirs_root == '':
commit.dirs_root = '/'
commit.dirs_count = len(commit.dirs_changed)
if commit.dirs_count > 1:
commit.dirs_count_s = " (%d dirs)" %(commit.dirs_count)
else:
commit.dirs_count_s = ""
commit.subdirs_count = commit.dirs_count
if commit.dirs_root in commit.dirs_changed:
commit.subdirs_count -= 1
if commit.subdirs_count >= 1:
commit.subdirs_count_s = " + %d subdirs" % (commit.subdirs_count)
else:
commit.subdirs_count_s = ""
示例7: test_bucket_list_wildcard
def test_bucket_list_wildcard(self):
"""Tests listing multiple buckets with a wildcard."""
random_prefix = self.MakeRandomTestString()
bucket1_name = self.MakeTempName("bucket", prefix=random_prefix)
bucket2_name = self.MakeTempName("bucket", prefix=random_prefix)
bucket1_uri = self.CreateBucket(bucket_name=bucket1_name)
bucket2_uri = self.CreateBucket(bucket_name=bucket2_name)
# This just double checks that the common prefix of the two buckets is what
# we think it should be (based on implementation detail of CreateBucket).
# We want to be careful when setting a wildcard on buckets to make sure we
# don't step outside the test buckets to affect other buckets.
common_prefix = posixpath.commonprefix([suri(bucket1_uri), suri(bucket2_uri)])
self.assertTrue(
common_prefix.startswith(
"%s://%sgsutil-test-test_bucket_list_wildcard-bucket-" % (self.default_provider, random_prefix)
)
)
wildcard = "%s*" % common_prefix
# Use @Retry as hedge against bucket listing eventual consistency.
@Retry(AssertionError, tries=3, timeout_secs=1)
def _Check1():
stdout = self.RunGsUtil(["ls", "-b", wildcard], return_stdout=True)
expected = set([suri(bucket1_uri) + "/", suri(bucket2_uri) + "/"])
actual = set(stdout.split())
self.assertEqual(expected, actual)
_Check1()
示例8: commonPaths
def commonPaths(paths):
""" Returns the common component and the stripped paths
It expects that directories do always end with a trailing slash and
paths never begin with a slash (except root).
:param paths: The list of paths (``[str, str, ...]``)
:type paths: ``list``
:return: The common component (always a directory) and the stripped
paths (``(str, [str, str, ...])``)
:rtype: ``tuple``
"""
import posixpath
common = ''
if len(paths) > 1 and "/" not in paths:
common = posixpath.commonprefix(paths)
if common[-1:] != "/":
common = common[:common.rfind("/") + 1]
idx = len(common)
if idx > 0:
paths = [path[idx:] or "./" for path in paths]
common = common[:-1] # chop the trailing slash
return (common, paths)
示例9: consolidateFiles
def consolidateFiles(self, xmlFiles):
"""Given a <files> element, find the directory common to all files
and return a 2-tuple with that directory followed by
a list of files within that directory.
"""
files = []
if xmlFiles:
for fileTag in XML.getChildElements(xmlFiles):
if fileTag.nodeName == 'file':
files.append(XML.shallowText(fileTag))
# If we only have one file, return it as the prefix.
# This prevents the below regex from deleting the filename
# itself, assuming it was a partial filename.
if len(files) == 1:
return files[0], []
# Start with the prefix found by commonprefix,
# then actually make it end with a directory rather than
# possibly ending with part of a filename.
prefix = re.sub("[^/]*$", "", posixpath.commonprefix(files))
endings = []
for file in files:
ending = file[len(prefix):].strip()
if ending == '':
ending = '.'
endings.append(ending)
return prefix, endings
示例10: GetCommonPrefix
def GetCommonPrefix(args):
"""Returns the common prefix between two paths (no partial paths).
e.g.: /tmp/bar, /tmp/baz will return /tmp/ (and not /tmp/ba as the dumb
posixpath.commonprefix implementation would do)
"""
parts = posixpath.commonprefix(args).rpartition(posixpath.sep)[0]
return parts + posixpath.sep if parts else ''
示例11: test_commonprefix
def test_commonprefix(self):
self.assertEqual(
posixpath.commonprefix([]),
""
)
self.assertEqual(
posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
"/home/swen"
)
self.assertEqual(
posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
"/home/swen/"
)
self.assertEqual(
posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
"/home/swen/spam"
)
示例12: is_suburi
def is_suburi(self, base, test):
if base == test:
return True
if base[0] != test[0]:
return False
common = posixpath.commonprefix((base[1], test[1]))
if len(common) == len(base[1]):
return True
return False
示例13: relpath
def relpath(path, start=curdir):
"""Return a relative version of a path"""
if not path:
raise ValueError("no path specified")
start_list = abspath(start).split(sep)
path_list = abspath(path).split(sep)
# Work out how much of the filepath is shared by start and path.
i = len(commonprefix([start_list, path_list]))
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
return curdir if not rel_list else join(*rel_list)
示例14: relpath
def relpath(path, start=posixpath.curdir): # NOQA
"""Return a relative version of a path"""
if not path:
raise ValueError("no path specified")
start_list = posixpath.abspath(start).split(posixpath.sep)
path_list = posixpath.abspath(path).split(posixpath.sep)
# Work out how much of the filepath is shared by start and path.
i = len(posixpath.commonprefix([start_list, path_list]))
rel_list = [posixpath.pardir] * (len(start_list) - i) + path_list[i:]
if not rel_list:
return posixpath.curdir
return posixpath.join(*rel_list)
示例15: posix_relpath
def posix_relpath(path, start):
sep = posixpath.sep
start_list = [x for x in posixpath.abspath(start).split(sep) if x]
path_list = [x for x in posixpath.abspath(path).split(sep) if x]
# Work out how much of the filepath is shared by start and path.
i = len(posixpath.commonprefix([start_list, path_list]))
rel_list = [posixpath.pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return posixpath.curdir
return posixpath.join(*rel_list)