Guido van Rossum | 99bf06b | 1995-08-10 19:34:50 +0000 | [diff] [blame] | 1 | # Module 'ntpath' -- common operations on DOS pathnames |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 2 | |
| 3 | import os |
| 4 | import stat |
| 5 | import string |
| 6 | |
| 7 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 8 | # Normalize the case of a pathname and map slashes to backslashes. |
| 9 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 10 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 11 | |
| 12 | _normtable = string.maketrans(string.uppercase + "\\/", |
| 13 | string.lowercase + os.sep * 2) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 14 | |
| 15 | def normcase(s): |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 16 | return string.translate(s, _normtable) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 19 | # Return wheter a path is absolute. |
| 20 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 21 | # For DOS it is absolute if it starts with a slash or backslash (current |
| 22 | # volume), or if a pathname after the volume letter and colon starts with |
| 23 | # a slash or backslash. |
| 24 | |
| 25 | def isabs(s): |
| 26 | s = splitdrive(s)[1] |
| 27 | return s != '' and s[:1] in '/\\' |
| 28 | |
| 29 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 30 | # Join two (or more) paths. |
| 31 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 32 | def join(a, *p): |
| 33 | path = a |
| 34 | for b in p: |
| 35 | if isabs(b): |
| 36 | path = b |
| 37 | elif path == '' or path[-1:] in '/\\': |
| 38 | path = path + b |
| 39 | else: |
| 40 | path = path + os.sep + b |
| 41 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | # Split a path in a drive specification (a drive letter followed by a |
| 45 | # colon) and the path specification. |
| 46 | # It is always true that drivespec + pathspec == p |
| 47 | def splitdrive(p): |
| 48 | if p[1:2] == ':': |
| 49 | return p[0:2], p[2:] |
| 50 | return '', p |
| 51 | |
| 52 | |
| 53 | # Split a path in head (everything up to the last '/') and tail (the |
| 54 | # rest). If the original path ends in '/' but is not the root, this |
| 55 | # '/' is stripped. After the trailing '/' is stripped, the invariant |
| 56 | # join(head, tail) == p holds. |
| 57 | # The resulting head won't end in '/' unless it is the root. |
| 58 | |
| 59 | def split(p): |
| 60 | d, p = splitdrive(p) |
| 61 | slashes = '' |
| 62 | while p and p[-1:] in '/\\': |
| 63 | slashes = slashes + p[-1] |
| 64 | p = p[:-1] |
| 65 | if p == '': |
| 66 | p = p + slashes |
| 67 | head, tail = '', '' |
| 68 | for c in p: |
| 69 | tail = tail + c |
| 70 | if c in '/\\': |
| 71 | head, tail = head + tail, '' |
| 72 | slashes = '' |
| 73 | while head and head[-1:] in '/\\': |
| 74 | slashes = slashes + head[-1] |
| 75 | head = head[:-1] |
| 76 | if head == '': |
| 77 | head = head + slashes |
| 78 | return d + head, tail |
| 79 | |
| 80 | |
| 81 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 82 | # The extension is everything starting at the last dot in the last |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 83 | # pathname component; the root is everything before that. |
| 84 | # It is always true that root + ext == p. |
| 85 | |
| 86 | def splitext(p): |
| 87 | root, ext = '', '' |
| 88 | for c in p: |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 89 | if c in ['/','\\']: |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 90 | root, ext = root + ext + c, '' |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 91 | elif c == '.': |
| 92 | if ext: |
| 93 | root, ext = root + ext, c |
| 94 | else: |
| 95 | ext = c |
| 96 | elif ext: |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 97 | ext = ext + c |
| 98 | else: |
| 99 | root = root + c |
| 100 | return root, ext |
| 101 | |
| 102 | |
| 103 | # Return the tail (basename) part of a path. |
| 104 | |
| 105 | def basename(p): |
| 106 | return split(p)[1] |
| 107 | |
| 108 | |
| 109 | # Return the head (dirname) part of a path. |
| 110 | |
| 111 | def dirname(p): |
| 112 | return split(p)[0] |
| 113 | |
| 114 | |
| 115 | # Return the longest prefix of all list elements. |
| 116 | |
| 117 | def commonprefix(m): |
| 118 | if not m: return '' |
| 119 | prefix = m[0] |
| 120 | for item in m: |
| 121 | for i in range(len(prefix)): |
| 122 | if prefix[:i+1] <> item[:i+1]: |
| 123 | prefix = prefix[:i] |
| 124 | if i == 0: return '' |
| 125 | break |
| 126 | return prefix |
| 127 | |
| 128 | |
| 129 | # Is a path a symbolic link? |
| 130 | # This will always return false on systems where posix.lstat doesn't exist. |
| 131 | |
| 132 | def islink(path): |
Guido van Rossum | 0523d63 | 1996-08-08 18:32:15 +0000 | [diff] [blame] | 133 | return 0 |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | # Does a path exist? |
| 137 | # This is false for dangling symbolic links. |
| 138 | |
| 139 | def exists(path): |
| 140 | try: |
| 141 | st = os.stat(path) |
| 142 | except os.error: |
| 143 | return 0 |
| 144 | return 1 |
| 145 | |
| 146 | |
| 147 | # Is a path a dos directory? |
| 148 | # This follows symbolic links, so both islink() and isdir() can be true |
| 149 | # for the same path. |
| 150 | |
| 151 | def isdir(path): |
| 152 | try: |
| 153 | st = os.stat(path) |
| 154 | except os.error: |
| 155 | return 0 |
| 156 | return stat.S_ISDIR(st[stat.ST_MODE]) |
| 157 | |
| 158 | |
| 159 | # Is a path a regular file? |
| 160 | # This follows symbolic links, so both islink() and isdir() can be true |
| 161 | # for the same path. |
| 162 | |
| 163 | def isfile(path): |
| 164 | try: |
| 165 | st = os.stat(path) |
| 166 | except os.error: |
| 167 | return 0 |
| 168 | return stat.S_ISREG(st[stat.ST_MODE]) |
| 169 | |
| 170 | |
| 171 | # Are two filenames really pointing to the same file? |
| 172 | |
| 173 | def samefile(f1, f2): |
| 174 | s1 = os.stat(f1) |
| 175 | s2 = os.stat(f2) |
| 176 | return samestat(s1, s2) |
| 177 | |
| 178 | |
| 179 | # Are two open files really referencing the same file? |
| 180 | # (Not necessarily the same file descriptor!) |
| 181 | # XXX THIS IS BROKEN UNDER DOS! ST_INO seems to indicate number of reads? |
| 182 | |
| 183 | def sameopenfile(fp1, fp2): |
| 184 | s1 = os.fstat(fp1.fileno()) |
| 185 | s2 = os.fstat(fp2.fileno()) |
| 186 | return samestat(s1, s2) |
| 187 | |
| 188 | |
| 189 | # Are two stat buffers (obtained from stat, fstat or lstat) |
| 190 | # describing the same file? |
| 191 | |
| 192 | def samestat(s1, s2): |
| 193 | return s1[stat.ST_INO] == s2[stat.ST_INO] and \ |
| 194 | s1[stat.ST_DEV] == s2[stat.ST_DEV] |
| 195 | |
| 196 | |
| 197 | # Is a path a mount point? |
| 198 | # XXX This degenerates in: 'is this the root?' on DOS |
| 199 | |
| 200 | def ismount(path): |
| 201 | return isabs(splitdrive(path)[1]) |
| 202 | |
| 203 | |
| 204 | # Directory tree walk. |
| 205 | # For each directory under top (including top itself, but excluding |
| 206 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 207 | # dirname is the name of the directory and filenames is the list |
| 208 | # files files (and subdirectories etc.) in the directory. |
| 209 | # The func may modify the filenames list, to implement a filter, |
| 210 | # or to impose a different order of visiting. |
| 211 | |
| 212 | def walk(top, func, arg): |
| 213 | try: |
| 214 | names = os.listdir(top) |
| 215 | except os.error: |
| 216 | return |
| 217 | func(arg, top, names) |
| 218 | exceptions = ('.', '..') |
| 219 | for name in names: |
| 220 | if name not in exceptions: |
| 221 | name = join(top, name) |
| 222 | if isdir(name): |
| 223 | walk(name, func, arg) |
| 224 | |
| 225 | |
| 226 | # Expand paths beginning with '~' or '~user'. |
| 227 | # '~' means $HOME; '~user' means that user's home directory. |
| 228 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 229 | # the path is returned unchanged (leaving error reporting to whatever |
| 230 | # function is called with the expanded path as argument). |
| 231 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 232 | # (A function should also be defined to do full *sh-style environment |
| 233 | # variable expansion.) |
| 234 | |
| 235 | def expanduser(path): |
| 236 | if path[:1] <> '~': |
| 237 | return path |
| 238 | i, n = 1, len(path) |
| 239 | while i < n and path[i] not in '/\\': |
| 240 | i = i+1 |
| 241 | if i == 1: |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 242 | if os.environ.has_key('HOME'): |
| 243 | userhome = os.environ['HOME'] |
| 244 | elif not os.environ.has_key('HOMEPATH'): |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 245 | return path |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 246 | else: |
| 247 | try: |
| 248 | drive=os.environ['HOMEDRIVE'] |
| 249 | except KeyError: |
| 250 | drive = '' |
| 251 | userhome = join(drive, os.environ['HOMEPATH']) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 252 | else: |
| 253 | return path |
| 254 | return userhome + path[i:] |
| 255 | |
| 256 | |
| 257 | # Expand paths containing shell variable substitutions. |
| 258 | # The following rules apply: |
| 259 | # - no expansion within single quotes |
| 260 | # - no escape character, except for '$$' which is translated into '$' |
| 261 | # - ${varname} is accepted. |
| 262 | # - varnames can be made out of letters, digits and the character '_' |
| 263 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 264 | # XXX except '^|<>='. |
| 265 | |
| 266 | varchars = string.letters + string.digits + '_-' |
| 267 | |
| 268 | def expandvars(path): |
| 269 | if '$' not in path: |
| 270 | return path |
| 271 | res = '' |
| 272 | index = 0 |
| 273 | pathlen = len(path) |
| 274 | while index < pathlen: |
| 275 | c = path[index] |
| 276 | if c == '\'': # no expansion within single quotes |
| 277 | path = path[index + 1:] |
| 278 | pathlen = len(path) |
| 279 | try: |
| 280 | index = string.index(path, '\'') |
| 281 | res = res + '\'' + path[:index + 1] |
| 282 | except string.index_error: |
| 283 | res = res + path |
| 284 | index = pathlen -1 |
| 285 | elif c == '$': # variable or '$$' |
| 286 | if path[index + 1:index + 2] == '$': |
| 287 | res = res + c |
| 288 | index = index + 1 |
| 289 | elif path[index + 1:index + 2] == '{': |
| 290 | path = path[index+2:] |
| 291 | pathlen = len(path) |
| 292 | try: |
| 293 | index = string.index(path, '}') |
| 294 | var = path[:index] |
| 295 | if os.environ.has_key(var): |
| 296 | res = res + os.environ[var] |
| 297 | except string.index_error: |
| 298 | res = res + path |
| 299 | index = pathlen - 1 |
| 300 | else: |
| 301 | var = '' |
| 302 | index = index + 1 |
| 303 | c = path[index:index + 1] |
| 304 | while c != '' and c in varchars: |
| 305 | var = var + c |
| 306 | index = index + 1 |
| 307 | c = path[index:index + 1] |
| 308 | if os.environ.has_key(var): |
| 309 | res = res + os.environ[var] |
| 310 | if c != '': |
| 311 | res = res + c |
| 312 | else: |
| 313 | res = res + c |
| 314 | index = index + 1 |
| 315 | return res |
| 316 | |
| 317 | |
| 318 | # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
Guido van Rossum | 3df7b5a | 1996-08-26 16:35:26 +0000 | [diff] [blame] | 319 | # Previously, this function also truncated pathnames to 8+3 format, |
| 320 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 321 | |
| 322 | def normpath(path): |
| 323 | path = normcase(path) |
| 324 | prefix, path = splitdrive(path) |
| 325 | while path[:1] == os.sep: |
| 326 | prefix = prefix + os.sep |
| 327 | path = path[1:] |
| 328 | comps = string.splitfields(path, os.sep) |
| 329 | i = 0 |
| 330 | while i < len(comps): |
| 331 | if comps[i] == '.': |
| 332 | del comps[i] |
| 333 | elif comps[i] == '..' and i > 0 and \ |
| 334 | comps[i-1] not in ('', '..'): |
| 335 | del comps[i-1:i+1] |
| 336 | i = i-1 |
| 337 | elif comps[i] == '' and i > 0 and comps[i-1] <> '': |
| 338 | del comps[i] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 339 | else: |
| 340 | i = i+1 |
| 341 | # If the path is now empty, substitute '.' |
| 342 | if not prefix and not comps: |
| 343 | comps.append('.') |
| 344 | return prefix + string.joinfields(comps, os.sep) |