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