当前位置: 首页>>代码示例>>Python>>正文


Python posixpath.commonprefix函数代码示例

本文整理汇总了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
开发者ID:HTshandou,项目名称:chromium,代码行数:28,代码来源:path_canonicalizer.py

示例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])
开发者ID:CaoYouXin,项目名称:myfirstapicloudapp,代码行数:27,代码来源:test_posixpath.py

示例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)
开发者ID:marineam,项目名称:gsutil,代码行数:28,代码来源:test_lifecycle.py

示例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)
开发者ID:DavidCain,项目名称:biopython,代码行数:26,代码来源:_paml.py

示例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)
开发者ID:lysine,项目名称:wasp,代码行数:25,代码来源:svnwcsub.py

示例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 = ""
开发者ID:Ranga123,项目名称:test1,代码行数:30,代码来源:irkerbridge.py

示例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()
开发者ID:mohadian,项目名称:interstellar,代码行数:28,代码来源:test_ls.py

示例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)
开发者ID:m-tmatma,项目名称:svnmailer,代码行数:27,代码来源:util.py

示例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
开发者ID:Justasic,项目名称:cia-vc,代码行数:29,代码来源:Commit.py

示例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 ''
开发者ID:dianalow,项目名称:chromium,代码行数:8,代码来源:native_heap_classifier.py

示例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"
     )
开发者ID:Alex-CS,项目名称:sonify,代码行数:17,代码来源:test_posixpath.py

示例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
开发者ID:connoryang,项目名称:dec-eve-serenity,代码行数:9,代码来源:urllib2.py

示例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)
开发者ID:achuwilson,项目名称:openrave,代码行数:11,代码来源:misc.py

示例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)
开发者ID:kevin-zhangsen,项目名称:badam,代码行数:12,代码来源:py3.py

示例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)
开发者ID:croach,项目名称:Frozen-Flask,代码行数:12,代码来源:__init__.py


注:本文中的posixpath.commonprefix函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。