| 1 | # Module 'os2emxpath' -- common operations on OS/2 pathnames
|
|---|
| 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 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 | "samefile","sameopenfile","samestat",
|
|---|
| 19 | "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
|
|---|
| 20 | "extsep","devnull","realpath","supports_unicode_filenames"]
|
|---|
| 21 |
|
|---|
| 22 | # strings representing various path-related bits and pieces
|
|---|
| 23 | curdir = '.'
|
|---|
| 24 | pardir = '..'
|
|---|
| 25 | extsep = '.'
|
|---|
| 26 | sep = '/'
|
|---|
| 27 | altsep = '\\'
|
|---|
| 28 | pathsep = ';'
|
|---|
| 29 | defpath = '.;C:\\bin'
|
|---|
| 30 | devnull = 'nul'
|
|---|
| 31 |
|
|---|
| 32 | # Normalize the case of a pathname and map slashes to backslashes.
|
|---|
| 33 | # Other normalizations (such as optimizing '../' away) are not done
|
|---|
| 34 | # (this is done by normpath).
|
|---|
| 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 | # Join two (or more) paths.
|
|---|
| 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 | # Parse UNC paths
|
|---|
| 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 an UNC part.
|
|---|
| 66 | """
|
|---|
| 67 | if p[1:2] == ':':
|
|---|
| 68 | return '', p # Drive letter present
|
|---|
| 69 | firstTwo = p[0:2]
|
|---|
| 70 | if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
|
|---|
| 71 | # is a UNC path:
|
|---|
| 72 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
|
|---|
| 73 | # \\machine\mountpoint\directories...
|
|---|
| 74 | # directory ^^^^^^^^^^^^^^^
|
|---|
| 75 | normp = normcase(p)
|
|---|
| 76 | index = normp.find('/', 2)
|
|---|
| 77 | if index == -1:
|
|---|
| 78 | ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
|
|---|
| 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 | # Return the tail (basename) part of a path.
|
|---|
| 88 |
|
|---|
| 89 | def basename(p):
|
|---|
| 90 | """Returns the final component of a pathname"""
|
|---|
| 91 | return split(p)[1]
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | # Return the head (dirname) part of a path.
|
|---|
| 95 |
|
|---|
| 96 | def dirname(p):
|
|---|
| 97 | """Returns the directory component of a pathname"""
|
|---|
| 98 | return split(p)[0]
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | # Is a path a directory?
|
|---|
| 102 |
|
|---|
| 103 | # Is a path a mount point? Either a root (with or without drive letter)
|
|---|
| 104 | # or an UNC path with at most a / or \ after the mount point.
|
|---|
| 105 |
|
|---|
| 106 | def ismount(path):
|
|---|
| 107 | """Test whether a path is a mount point (defined as root of drive)"""
|
|---|
| 108 | unc, rest = splitunc(path)
|
|---|
| 109 | if unc:
|
|---|
| 110 | return rest in ("", "/", "\\")
|
|---|
| 111 | p = splitdrive(path)[1]
|
|---|
| 112 | return len(p) == 1 and p[0] in '/\\'
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
|
|---|
| 116 |
|
|---|
| 117 | def normpath(path):
|
|---|
| 118 | """Normalize path, eliminating double slashes, etc."""
|
|---|
| 119 | path = path.replace('\\', '/')
|
|---|
| 120 | prefix, path = splitdrive(path)
|
|---|
| 121 | while path[:1] == '/':
|
|---|
| 122 | prefix = prefix + '/'
|
|---|
| 123 | path = path[1:]
|
|---|
| 124 | comps = path.split('/')
|
|---|
| 125 | i = 0
|
|---|
| 126 | while i < len(comps):
|
|---|
| 127 | if comps[i] == '.':
|
|---|
| 128 | del comps[i]
|
|---|
| 129 | elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
|
|---|
| 130 | del comps[i-1:i+1]
|
|---|
| 131 | i = i - 1
|
|---|
| 132 | elif comps[i] == '' and i > 0 and comps[i-1] != '':
|
|---|
| 133 | del comps[i]
|
|---|
| 134 | else:
|
|---|
| 135 | i = i + 1
|
|---|
| 136 | # If the path is now empty, substitute '.'
|
|---|
| 137 | if not prefix and not comps:
|
|---|
| 138 | comps.append('.')
|
|---|
| 139 | return prefix + '/'.join(comps)
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | # Return an absolute path.
|
|---|
| 143 | def abspath(path):
|
|---|
| 144 | """Return the absolute version of a path"""
|
|---|
| 145 | if not isabs(path):
|
|---|
| 146 | path = join(os.getcwd(), path)
|
|---|
| 147 | return normpath(path)
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | # Return a canonical path (i.e. the absolute location of a file on the
|
|---|
| 151 | # filesystem).
|
|---|
| 152 |
|
|---|
| 153 | def realpath(filename):
|
|---|
| 154 | """Return the canonical path of the specified filename, eliminating any
|
|---|
| 155 | symbolic links encountered in the path."""
|
|---|
| 156 | if isabs(filename):
|
|---|
| 157 | bits = ['/'] + filename.split('/')[1:]
|
|---|
| 158 | else:
|
|---|
| 159 | bits = [''] + filename.split('/')
|
|---|
| 160 |
|
|---|
| 161 | for i in range(2, len(bits)+1):
|
|---|
| 162 | component = join(*bits[0:i])
|
|---|
| 163 | # Resolve symbolic links.
|
|---|
| 164 | if islink(component):
|
|---|
| 165 | resolved = _resolve_link(component)
|
|---|
| 166 | if resolved is None:
|
|---|
| 167 | # Infinite loop -- return original component + rest of the path
|
|---|
| 168 | return abspath(join(*([component] + bits[i:])))
|
|---|
| 169 | else:
|
|---|
| 170 | newpath = join(*([resolved] + bits[i:]))
|
|---|
| 171 | return realpath(newpath)
|
|---|
| 172 |
|
|---|
| 173 | return abspath(filename)
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | def _resolve_link(path):
|
|---|
| 177 | """Internal helper function. Takes a path and follows symlinks
|
|---|
| 178 | until we either arrive at something that isn't a symlink, or
|
|---|
| 179 | encounter a path we've seen before (meaning that there's a loop).
|
|---|
| 180 | """
|
|---|
| 181 | paths_seen = []
|
|---|
| 182 | while islink(path):
|
|---|
| 183 | if path in paths_seen:
|
|---|
| 184 | # Already seen this path, so we must have a symlink loop
|
|---|
| 185 | return None
|
|---|
| 186 | paths_seen.append(path)
|
|---|
| 187 | # Resolve where the link points to
|
|---|
| 188 | resolved = os.readlink(path)
|
|---|
| 189 | if not isabs(resolved):
|
|---|
| 190 | dir = dirname(path)
|
|---|
| 191 | path = normpath(join(dir, resolved))
|
|---|
| 192 | else:
|
|---|
| 193 | path = normpath(resolved)
|
|---|
| 194 | return path
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | # Is a path a symbolic link?
|
|---|
| 198 | # This will always return false on systems where os.lstat doesn't exist.
|
|---|
| 199 |
|
|---|
| 200 | def islink(path):
|
|---|
| 201 | """Test whether a path is a symbolic link"""
|
|---|
| 202 | try:
|
|---|
| 203 | st = os.lstat(path)
|
|---|
| 204 | except (os.error, AttributeError):
|
|---|
| 205 | return False
|
|---|
| 206 | return stat.S_ISLNK(st.st_mode)
|
|---|
| 207 |
|
|---|
| 208 | # Being true for dangling symbolic links is also useful.
|
|---|
| 209 |
|
|---|
| 210 | def lexists(path):
|
|---|
| 211 | """Test whether a path exists. Returns True for broken symbolic links"""
|
|---|
| 212 | try:
|
|---|
| 213 | st = os.lstat(path)
|
|---|
| 214 | except os.error:
|
|---|
| 215 | return False
|
|---|
| 216 | return True
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | # Are two filenames really pointing to the same file?
|
|---|
| 220 |
|
|---|
| 221 | def samefile(f1, f2):
|
|---|
| 222 | """Test whether two pathnames reference the same actual file"""
|
|---|
| 223 | s1 = os.stat(f1)
|
|---|
| 224 | s2 = os.stat(f2)
|
|---|
| 225 | return samestat(s1, s2)
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 | # Are two open files really referencing the same file?
|
|---|
| 229 | # (Not necessarily the same file descriptor!)
|
|---|
| 230 |
|
|---|
| 231 | def sameopenfile(fp1, fp2):
|
|---|
| 232 | """Test whether two open file objects reference the same file"""
|
|---|
| 233 | s1 = os.fstat(fp1)
|
|---|
| 234 | s2 = os.fstat(fp2)
|
|---|
| 235 | return samestat(s1, s2)
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 | # Are two stat buffers (obtained from stat, fstat or lstat)
|
|---|
| 239 | # describing the same file?
|
|---|
| 240 |
|
|---|
| 241 | def samestat(s1, s2):
|
|---|
| 242 | """Test whether two stat buffers reference the same file"""
|
|---|
| 243 | return s1.st_ino == s2.st_ino and \
|
|---|
| 244 | s1.st_dev == s2.st_dev
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | supports_unicode_filenames = False
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 | def relpath(path, start=curdir):
|
|---|
| 251 | """Return a relative version of a path"""
|
|---|
| 252 |
|
|---|
| 253 | if not path:
|
|---|
| 254 | raise ValueError("no path specified")
|
|---|
| 255 | start_list = abspath(start).split(sep)
|
|---|
| 256 | path_list = abspath(path).split(sep)
|
|---|
| 257 | # Remove empty components after trailing slashes
|
|---|
| 258 | if (start_list[-1] == ''):
|
|---|
| 259 | start_list.pop()
|
|---|
| 260 | if (path_list[-1] == ''):
|
|---|
| 261 | path_list.pop()
|
|---|
| 262 | if start_list[0].lower() != path_list[0].lower():
|
|---|
| 263 | unc_path, rest = splitunc(path)
|
|---|
| 264 | unc_start, rest = splitunc(start)
|
|---|
| 265 | if bool(unc_path) ^ bool(unc_start):
|
|---|
| 266 | raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
|
|---|
| 267 | % (path, start))
|
|---|
| 268 | else:
|
|---|
| 269 | raise ValueError("path is on drive %s, start on drive %s"
|
|---|
| 270 | % (path_list[0], start_list[0]))
|
|---|
| 271 | # Work out how much of the filepath is shared by start and path.
|
|---|
| 272 | for i in range(min(len(start_list), len(path_list))):
|
|---|
| 273 | if start_list[i].lower() != path_list[i].lower():
|
|---|
| 274 | break
|
|---|
| 275 | else:
|
|---|
| 276 | i += 1
|
|---|
| 277 |
|
|---|
| 278 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
|
|---|
| 279 | if not rel_list:
|
|---|
| 280 | return curdir
|
|---|
| 281 | return join(*rel_list)
|
|---|