PageRenderTime 73ms CodeModel.GetById 40ms app.highlight 14ms RepoModel.GetById 16ms app.codeStats 0ms

/Lib/os2emxpath.py

http://unladen-swallow.googlecode.com/
Python | 155 lines | 135 code | 6 blank | 14 comment | 13 complexity | afd588642268741f28eb58014b065c52 MD5 | raw file
  1# Module 'os2emxpath' -- common operations on OS/2 pathnames
  2"""Common pathname manipulations, OS/2 EMX version.
  3
  4Instead of importing this module directly, import os and refer to this
  5module as os.path.
  6"""
  7
  8import os
  9import stat
 10from genericpath import *
 11from ntpath import (expanduser, expandvars, isabs, islink, splitdrive,
 12                    splitext, split, walk)
 13
 14__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
 15           "basename","dirname","commonprefix","getsize","getmtime",
 16           "getatime","getctime", "islink","exists","lexists","isdir","isfile",
 17           "ismount","walk","expanduser","expandvars","normpath","abspath",
 18           "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
 19           "extsep","devnull","realpath","supports_unicode_filenames"]
 20
 21# strings representing various path-related bits and pieces
 22curdir = '.'
 23pardir = '..'
 24extsep = '.'
 25sep = '/'
 26altsep = '\\'
 27pathsep = ';'
 28defpath = '.;C:\\bin'
 29devnull = 'nul'
 30
 31# Normalize the case of a pathname and map slashes to backslashes.
 32# Other normalizations (such as optimizing '../' away) are not done
 33# (this is done by normpath).
 34
 35def normcase(s):
 36    """Normalize case of pathname.
 37
 38    Makes all characters lowercase and all altseps into seps."""
 39    return s.replace('\\', '/').lower()
 40
 41
 42# Join two (or more) paths.
 43
 44def join(a, *p):
 45    """Join two or more pathname components, inserting sep as needed"""
 46    path = a
 47    for b in p:
 48        if isabs(b):
 49            path = b
 50        elif path == '' or path[-1:] in '/\\:':
 51            path = path + b
 52        else:
 53            path = path + '/' + b
 54    return path
 55
 56
 57# Parse UNC paths
 58def splitunc(p):
 59    """Split a pathname into UNC mount point and relative path specifiers.
 60
 61    Return a 2-tuple (unc, rest); either part may be empty.
 62    If unc is not empty, it has the form '//host/mount' (or similar
 63    using backslashes).  unc+rest is always the input path.
 64    Paths containing drive letters never have an UNC part.
 65    """
 66    if p[1:2] == ':':
 67        return '', p # Drive letter present
 68    firstTwo = p[0:2]
 69    if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
 70        # is a UNC path:
 71        # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
 72        # \\machine\mountpoint\directories...
 73        #           directory ^^^^^^^^^^^^^^^
 74        normp = normcase(p)
 75        index = normp.find('/', 2)
 76        if index == -1:
 77            ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
 78            return ("", p)
 79        index = normp.find('/', index + 1)
 80        if index == -1:
 81            index = len(p)
 82        return p[:index], p[index:]
 83    return '', p
 84
 85
 86# Return the tail (basename) part of a path.
 87
 88def basename(p):
 89    """Returns the final component of a pathname"""
 90    return split(p)[1]
 91
 92
 93# Return the head (dirname) part of a path.
 94
 95def dirname(p):
 96    """Returns the directory component of a pathname"""
 97    return split(p)[0]
 98
 99
100# alias exists to lexists
101lexists = exists
102
103
104# Is a path a directory?
105
106# Is a path a mount point?  Either a root (with or without drive letter)
107# or an UNC path with at most a / or \ after the mount point.
108
109def ismount(path):
110    """Test whether a path is a mount point (defined as root of drive)"""
111    unc, rest = splitunc(path)
112    if unc:
113        return rest in ("", "/", "\\")
114    p = splitdrive(path)[1]
115    return len(p) == 1 and p[0] in '/\\'
116
117
118# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
119
120def normpath(path):
121    """Normalize path, eliminating double slashes, etc."""
122    path = path.replace('\\', '/')
123    prefix, path = splitdrive(path)
124    while path[:1] == '/':
125        prefix = prefix + '/'
126        path = path[1:]
127    comps = path.split('/')
128    i = 0
129    while i < len(comps):
130        if comps[i] == '.':
131            del comps[i]
132        elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
133            del comps[i-1:i+1]
134            i = i - 1
135        elif comps[i] == '' and i > 0 and comps[i-1] != '':
136            del comps[i]
137        else:
138            i = i + 1
139    # If the path is now empty, substitute '.'
140    if not prefix and not comps:
141        comps.append('.')
142    return prefix + '/'.join(comps)
143
144
145# Return an absolute path.
146def abspath(path):
147    """Return the absolute version of a path"""
148    if not isabs(path):
149        path = join(os.getcwd(), path)
150    return normpath(path)
151
152# realpath is a no-op on systems without islink support
153realpath = abspath
154
155supports_unicode_filenames = False