Пример #1
0
  def fileSystemPath(path):
    """Look up the correctly cased path from the file system.
  
    This is only relevant on file systems that are case insensitive such
    as Windows.
    
    '/' and '\\' will be left intact.
  
    '.' and '..' will be left intact.
    
    A drive letter will be capitalized.
    
    @param path: The path to look up.
    @type path: string
    
    @return: The correctly cased file system path.
    @rtype: string
    """
    seps = frozenset([os.path.sep, os.path.altsep])

    parts = list()
    while path:
      stem, leaf = os.path.split(path)
      if leaf != '.' and leaf != '..':
        try:
          leaf = _fileSystemBaseName(path, stem, leaf)
        except Exception:
          pass
      parts.append(leaf)
  
      if stem and len(path) > len(stem):
        sep = path[len(stem)]
        if sep in seps:
          parts.append(sep)
          
      path = stem
      
      if not leaf:
        # Reached root path
        break
  
    if path:
      # Capitalise drive letter if found
      if len(path) >= 2 and path[1] == ':':
        path = path.capitalize()
      parts.append(path)
  
    return "".join(reversed(parts))
Пример #2
0
def relative_path(path, parent_path):
    path = normalize_path(path)
    parent_path = normalize_path(parent_path)

    if os_utils.is_win():
        path = path.capitalize()
        parent_path = parent_path.capitalize()

    if not path.startswith(parent_path):
        raise ValueError(path + ' is not subpath of ' + parent_path)

    relative_path = path[len(parent_path):]

    if relative_path.startswith(os.path.sep):
        return relative_path[1:]

    return relative_path