| 1 |
|
| 2 |
"""Common pathname manipulations, OS/2 EMX version. |
| 3 |
|
| 4 |
Instead of importing this module directly, import os and refer to this |
| 5 |
module as os.path. |
| 6 |
""" |
| 7 |
|
| 8 |
import os |
| 9 |
import stat |
| 10 |
from genericpath import * |
| 11 |
from genericpath import _unicode |
| 12 |
from ntpath import (expanduser, expandvars, isabs, islink, splitdrive, |
| 13 |
splitext, split, walk) |
| 14 |
|
| 15 |
__all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 16 |
"basename","dirname","commonprefix","getsize","getmtime", |
| 17 |
"getatime","getctime", "islink","exists","lexists","isdir","isfile", |
| 18 |
"ismount","walk","expanduser","expandvars","normpath","abspath", |
| 19 |
"splitunc","curdir","pardir","sep","pathsep","defpath","altsep", |
| 20 |
"extsep","devnull","realpath","supports_unicode_filenames"] |
| 21 |
|
| 22 |
|
| 23 |
curdir = '.' |
| 24 |
pardir = '..' |
| 25 |
extsep = '.' |
| 26 |
sep = '/' |
| 27 |
altsep = '\\' |
| 28 |
pathsep = ';' |
| 29 |
defpath = '.;C:\\bin' |
| 30 |
devnull = 'nul' |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
def normcase(s): |
| 37 |
"""Normalize case of pathname. |
| 38 |
|
| 39 |
Makes all characters lowercase and all altseps into seps.""" |
| 40 |
return s.replace('\\', '/').lower() |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
def join(a, *p): |
| 46 |
"""Join two or more pathname components, inserting sep as needed""" |
| 47 |
path = a |
| 48 |
for b in p: |
| 49 |
if isabs(b): |
| 50 |
path = b |
| 51 |
elif path == '' or path[-1:] in '/\\:': |
| 52 |
path = path + b |
| 53 |
else: |
| 54 |
path = path + '/' + b |
| 55 |
return path |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
def splitunc(p): |
| 60 |
"""Split a pathname into UNC mount point and relative path specifiers. |
| 61 |
|
| 62 |
Return a 2-tuple (unc, rest); either part may be empty. |
| 63 |
If unc is not empty, it has the form '//host/mount' (or similar |
| 64 |
using backslashes). unc+rest is always the input path. |
| 65 |
Paths containing drive letters never have a UNC part. |
| 66 |
""" |
| 67 |
if p[1:2] == ':': |
| 68 |
return '', p |
| 69 |
firstTwo = p[0:2] |
| 70 |
if firstTwo == '/' * 2 or firstTwo == '\\' * 2: |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
normp = normcase(p) |
| 76 |
index = normp.find('/', 2) |
| 77 |
if index == -1: |
| 78 |
|
| 79 |
return ("", p) |
| 80 |
index = normp.find('/', index + 1) |
| 81 |
if index == -1: |
| 82 |
index = len(p) |
| 83 |
return p[:index], p[index:] |
| 84 |
return '', p |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
def basename(p): |
| 90 |
"""Returns the final component of a pathname""" |
| 91 |
return split(p)[1] |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
def dirname(p): |
| 97 |
"""Returns the directory component of a pathname""" |
| 98 |
return split(p)[0] |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
lexists = exists |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
def ismount(path): |
| 111 |
"""Test whether a path is a mount point (defined as root of drive)""" |
| 112 |
unc, rest = splitunc(path) |
| 113 |
if unc: |
| 114 |
return rest in ("", "/", "\\") |
| 115 |
p = splitdrive(path)[1] |
| 116 |
return len(p) == 1 and p[0] in '/\\' |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
def normpath(path): |
| 122 |
"""Normalize path, eliminating double slashes, etc.""" |
| 123 |
path = path.replace('\\', '/') |
| 124 |
prefix, path = splitdrive(path) |
| 125 |
while path[:1] == '/': |
| 126 |
prefix = prefix + '/' |
| 127 |
path = path[1:] |
| 128 |
comps = path.split('/') |
| 129 |
i = 0 |
| 130 |
while i < len(comps): |
| 131 |
if comps[i] == '.': |
| 132 |
del comps[i] |
| 133 |
elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'): |
| 134 |
del comps[i-1:i+1] |
| 135 |
i = i - 1 |
| 136 |
elif comps[i] == '' and i > 0 and comps[i-1] != '': |
| 137 |
del comps[i] |
| 138 |
else: |
| 139 |
i = i + 1 |
| 140 |
|
| 141 |
if not prefix and not comps: |
| 142 |
comps.append('.') |
| 143 |
return prefix + '/'.join(comps) |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
def abspath(path): |
| 148 |
"""Return the absolute version of a path""" |
| 149 |
if not isabs(path): |
| 150 |
if isinstance(path, _unicode): |
| 151 |
cwd = os.getcwdu() |
| 152 |
else: |
| 153 |
cwd = os.getcwd() |
| 154 |
path = join(cwd, path) |
| 155 |
return normpath(path) |
| 156 |
|
| 157 |
|
| 158 |
realpath = abspath |
| 159 |
|
| 160 |
supports_unicode_filenames = False |