Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 1 | # Module 'ntpath' -- common operations on WinNT/Win95 pathnames |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 2 | """Common pathname manipulations, WindowsNT/95 version. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 3 | |
| 4 | Instead of importing this module directly, import os and refer to this |
| 5 | module as os.path. |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 6 | """ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import stat |
Skip Montanaro | 4d5d5bf | 2000-07-13 01:01:03 +0000 | [diff] [blame] | 10 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 11 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 12 | "basename","dirname","commonprefix","getsize","getmtime", |
| 13 | "getatime","islink","exists","isdir","isfile","ismount", |
| 14 | "walk","expanduser","expandvars","normpath","abspath","splitunc"] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 16 | # Normalize the case of a pathname and map slashes to backslashes. |
| 17 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 18 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 20 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 21 | """Normalize case of pathname. |
| 22 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 23 | Makes all characters lowercase and all slashes into backslashes.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 24 | return s.replace("/", "\\").lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 26 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 27 | # Return whether a path is absolute. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 28 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 29 | # For DOS it is absolute if it starts with a slash or backslash (current |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 30 | # volume), or if a pathname after the volume letter and colon / UNC resource |
| 31 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 32 | |
| 33 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 34 | """Test whether a path is absolute""" |
| 35 | s = splitdrive(s)[1] |
| 36 | return s != '' and s[:1] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 37 | |
| 38 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 39 | # Join two (or more) paths. |
| 40 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 41 | def join(a, *p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 42 | """Join two or more pathname components, inserting "\\" as needed""" |
| 43 | path = a |
| 44 | for b in p: |
| 45 | if isabs(b): |
| 46 | path = b |
Tim Peters | 0eeba5b | 2000-09-19 20:39:32 +0000 | [diff] [blame] | 47 | elif path == '' or path[-1:] in '/\\:': |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 48 | path = path + b |
| 49 | else: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 50 | path = path + "\\" + b |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 51 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | # Split a path in a drive specification (a drive letter followed by a |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 55 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 56 | # It is always true that drivespec + pathspec == p |
| 57 | def splitdrive(p): |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 58 | """Split a pathname into drive and path specifiers. Returns a 2-tuple |
| 59 | "(drive,path)"; either part may be empty""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 60 | if p[1:2] == ':': |
| 61 | return p[0:2], p[2:] |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 62 | return '', p |
| 63 | |
| 64 | |
| 65 | # Parse UNC paths |
| 66 | def splitunc(p): |
| 67 | """Split a pathname into UNC mount point and relative path specifiers. |
| 68 | |
| 69 | Return a 2-tuple (unc, rest); either part may be empty. |
| 70 | If unc is not empty, it has the form '//host/mount' (or similar |
| 71 | using backslashes). unc+rest is always the input path. |
| 72 | Paths containing drive letters never have an UNC part. |
| 73 | """ |
| 74 | if p[1:2] == ':': |
| 75 | return '', p # Drive letter present |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 76 | firstTwo = p[0:2] |
| 77 | if firstTwo == '//' or firstTwo == '\\\\': |
| 78 | # is a UNC path: |
| 79 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter |
| 80 | # \\machine\mountpoint\directories... |
| 81 | # directory ^^^^^^^^^^^^^^^ |
| 82 | normp = normcase(p) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 83 | index = normp.find('\\', 2) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 84 | if index == -1: |
| 85 | ##raise RuntimeError, 'illegal UNC path: "' + p + '"' |
| 86 | return ("", p) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 87 | index = normp.find('\\', index + 1) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 88 | if index == -1: |
| 89 | index = len(p) |
| 90 | return p[:index], p[index:] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 91 | return '', p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | # Split a path in head (everything up to the last '/') and tail (the |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 95 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 96 | # join(head, tail) == p holds. |
| 97 | # The resulting head won't end in '/' unless it is the root. |
| 98 | |
| 99 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 100 | """Split a pathname. |
| 101 | |
| 102 | Return tuple (head, tail) where tail is everything after the final slash. |
| 103 | Either part may be empty.""" |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 105 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 106 | # set i to index beyond p's last slash |
| 107 | i = len(p) |
| 108 | while i and p[i-1] not in '/\\': |
| 109 | i = i - 1 |
| 110 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 111 | # remove trailing slashes from head, unless it's all slashes |
| 112 | head2 = head |
| 113 | while head2 and head2[-1] in '/\\': |
| 114 | head2 = head2[:-1] |
| 115 | head = head2 or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 116 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 120 | # 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] | 121 | # pathname component; the root is everything before that. |
| 122 | # It is always true that root + ext == p. |
| 123 | |
| 124 | def splitext(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 125 | """Split the extension from a pathname. |
| 126 | |
| 127 | Extension is everything from the last dot to the end. |
| 128 | Return (root, ext), either part may be empty.""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 129 | root, ext = '', '' |
| 130 | for c in p: |
| 131 | if c in ['/','\\']: |
| 132 | root, ext = root + ext + c, '' |
| 133 | elif c == '.': |
| 134 | if ext: |
| 135 | root, ext = root + ext, c |
| 136 | else: |
| 137 | ext = c |
| 138 | elif ext: |
| 139 | ext = ext + c |
| 140 | else: |
| 141 | root = root + c |
| 142 | return root, ext |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | # Return the tail (basename) part of a path. |
| 146 | |
| 147 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 148 | """Returns the final component of a pathname""" |
| 149 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 150 | |
| 151 | |
| 152 | # Return the head (dirname) part of a path. |
| 153 | |
| 154 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 155 | """Returns the directory component of a pathname""" |
| 156 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | # Return the longest prefix of all list elements. |
| 160 | |
| 161 | def commonprefix(m): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 162 | "Given a list of pathnames, returns the longest common leading component" |
| 163 | if not m: return '' |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 164 | prefix = m[0] |
| 165 | for item in m: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 166 | for i in range(len(prefix)): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 167 | if prefix[:i+1] != item[:i+1]: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 168 | prefix = prefix[:i] |
| 169 | if i == 0: return '' |
| 170 | break |
Skip Montanaro | 6235831 | 2000-08-22 13:01:53 +0000 | [diff] [blame] | 171 | return prefix |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 172 | |
| 173 | |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 174 | # Get size, mtime, atime of files. |
| 175 | |
| 176 | def getsize(filename): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 177 | """Return the size of a file, reported by os.stat()""" |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 178 | st = os.stat(filename) |
| 179 | return st[stat.ST_SIZE] |
| 180 | |
| 181 | def getmtime(filename): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 182 | """Return the last modification time of a file, reported by os.stat()""" |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 183 | st = os.stat(filename) |
| 184 | return st[stat.ST_MTIME] |
| 185 | |
| 186 | def getatime(filename): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 187 | """Return the last access time of a file, reported by os.stat()""" |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 188 | st = os.stat(filename) |
Fred Drake | 162bd85 | 2000-07-01 06:36:51 +0000 | [diff] [blame] | 189 | return st[stat.ST_ATIME] |
Guido van Rossum | 2bc1f8f | 1998-07-24 20:49:26 +0000 | [diff] [blame] | 190 | |
| 191 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 192 | # Is a path a symbolic link? |
| 193 | # This will always return false on systems where posix.lstat doesn't exist. |
| 194 | |
| 195 | def islink(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 196 | """Test for symbolic link. On WindowsNT/95 always returns false""" |
| 197 | return 0 |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | # Does a path exist? |
| 201 | # This is false for dangling symbolic links. |
| 202 | |
| 203 | def exists(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 204 | """Test whether a path exists""" |
| 205 | try: |
| 206 | st = os.stat(path) |
| 207 | except os.error: |
| 208 | return 0 |
| 209 | return 1 |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | # Is a path a dos directory? |
| 213 | # This follows symbolic links, so both islink() and isdir() can be true |
| 214 | # for the same path. |
| 215 | |
| 216 | def isdir(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 217 | """Test whether a path is a directory""" |
| 218 | try: |
| 219 | st = os.stat(path) |
| 220 | except os.error: |
| 221 | return 0 |
| 222 | return stat.S_ISDIR(st[stat.ST_MODE]) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | # Is a path a regular file? |
| 226 | # This follows symbolic links, so both islink() and isdir() can be true |
| 227 | # for the same path. |
| 228 | |
| 229 | def isfile(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 230 | """Test whether a path is a regular file""" |
| 231 | try: |
| 232 | st = os.stat(path) |
| 233 | except os.error: |
| 234 | return 0 |
| 235 | return stat.S_ISREG(st[stat.ST_MODE]) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 236 | |
| 237 | |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 238 | # Is a path a mount point? Either a root (with or without drive letter) |
| 239 | # or an UNC path with at most a / or \ after the mount point. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 240 | |
| 241 | def ismount(path): |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 242 | """Test whether a path is a mount point (defined as root of drive)""" |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 243 | unc, rest = splitunc(path) |
| 244 | if unc: |
| 245 | return rest in ("", "/", "\\") |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 246 | p = splitdrive(path)[1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 247 | return len(p) == 1 and p[0] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | # Directory tree walk. |
| 251 | # For each directory under top (including top itself, but excluding |
| 252 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 253 | # dirname is the name of the directory and filenames is the list |
| 254 | # files files (and subdirectories etc.) in the directory. |
| 255 | # The func may modify the filenames list, to implement a filter, |
| 256 | # or to impose a different order of visiting. |
| 257 | |
| 258 | def walk(top, func, arg): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 259 | """Directory tree walk whth callback function. |
| 260 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 261 | walk(top, func, arg) calls func(arg, d, files) for each directory d |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 262 | in the tree rooted at top (including top itself); files is a list |
| 263 | of all the files and subdirs in directory d.""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 264 | try: |
| 265 | names = os.listdir(top) |
| 266 | except os.error: |
| 267 | return |
| 268 | func(arg, top, names) |
| 269 | exceptions = ('.', '..') |
| 270 | for name in names: |
| 271 | if name not in exceptions: |
| 272 | name = join(top, name) |
| 273 | if isdir(name): |
| 274 | walk(name, func, arg) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 275 | |
| 276 | |
| 277 | # Expand paths beginning with '~' or '~user'. |
| 278 | # '~' means $HOME; '~user' means that user's home directory. |
| 279 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 280 | # the path is returned unchanged (leaving error reporting to whatever |
| 281 | # function is called with the expanded path as argument). |
| 282 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 283 | # (A function should also be defined to do full *sh-style environment |
| 284 | # variable expansion.) |
| 285 | |
| 286 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 287 | """Expand ~ and ~user constructs. |
| 288 | |
| 289 | If user or $HOME is unknown, do nothing.""" |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 290 | if path[:1] != '~': |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 291 | return path |
| 292 | i, n = 1, len(path) |
| 293 | while i < n and path[i] not in '/\\': |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 294 | i = i + 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 295 | if i == 1: |
| 296 | if os.environ.has_key('HOME'): |
| 297 | userhome = os.environ['HOME'] |
| 298 | elif not os.environ.has_key('HOMEPATH'): |
| 299 | return path |
| 300 | else: |
| 301 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 302 | drive = os.environ['HOMEDRIVE'] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 303 | except KeyError: |
| 304 | drive = '' |
| 305 | userhome = join(drive, os.environ['HOMEPATH']) |
| 306 | else: |
| 307 | return path |
| 308 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | # Expand paths containing shell variable substitutions. |
| 312 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 313 | # - no expansion within single quotes |
| 314 | # - no escape character, except for '$$' which is translated into '$' |
| 315 | # - ${varname} is accepted. |
| 316 | # - varnames can be made out of letters, digits and the character '_' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 317 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 318 | # XXX except '^|<>='. |
| 319 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 320 | def expandvars(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 321 | """Expand shell variables of form $var and ${var}. |
| 322 | |
| 323 | Unknown variables are left unchanged.""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 324 | if '$' not in path: |
| 325 | return path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 326 | import string |
| 327 | varchars = string.letters + string.digits + '_-' |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 328 | res = '' |
| 329 | index = 0 |
| 330 | pathlen = len(path) |
| 331 | while index < pathlen: |
| 332 | c = path[index] |
| 333 | if c == '\'': # no expansion within single quotes |
| 334 | path = path[index + 1:] |
| 335 | pathlen = len(path) |
| 336 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 337 | index = path.index('\'') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 338 | res = res + '\'' + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 339 | except ValueError: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 340 | res = res + path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 341 | index = pathlen - 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 342 | elif c == '$': # variable or '$$' |
| 343 | if path[index + 1:index + 2] == '$': |
| 344 | res = res + c |
| 345 | index = index + 1 |
| 346 | elif path[index + 1:index + 2] == '{': |
| 347 | path = path[index+2:] |
| 348 | pathlen = len(path) |
| 349 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 350 | index = path.index('}') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 351 | var = path[:index] |
| 352 | if os.environ.has_key(var): |
| 353 | res = res + os.environ[var] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 354 | except ValueError: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 355 | res = res + path |
| 356 | index = pathlen - 1 |
| 357 | else: |
| 358 | var = '' |
| 359 | index = index + 1 |
| 360 | c = path[index:index + 1] |
| 361 | while c != '' and c in varchars: |
| 362 | var = var + c |
| 363 | index = index + 1 |
| 364 | c = path[index:index + 1] |
| 365 | if os.environ.has_key(var): |
| 366 | res = res + os.environ[var] |
| 367 | if c != '': |
| 368 | res = res + c |
| 369 | else: |
| 370 | res = res + c |
| 371 | index = index + 1 |
| 372 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 373 | |
| 374 | |
| 375 | # 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] | 376 | # Previously, this function also truncated pathnames to 8+3 format, |
| 377 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 378 | |
| 379 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 380 | """Normalize path, eliminating double slashes, etc.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 381 | path = path.replace("/", "\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 382 | prefix, path = splitdrive(path) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 383 | while path[:1] == "\\": |
| 384 | prefix = prefix + "\\" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 385 | path = path[1:] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 386 | comps = path.split("\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 387 | i = 0 |
| 388 | while i < len(comps): |
| 389 | if comps[i] == '.': |
| 390 | del comps[i] |
| 391 | elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'): |
| 392 | del comps[i-1:i+1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 393 | i = i - 1 |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 394 | elif comps[i] == '' and i > 0 and comps[i-1] != '': |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 395 | del comps[i] |
| 396 | else: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 397 | i = i + 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 398 | # If the path is now empty, substitute '.' |
| 399 | if not prefix and not comps: |
| 400 | comps.append('.') |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 401 | return prefix + "\\".join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 402 | |
| 403 | |
| 404 | # Return an absolute path. |
| 405 | def abspath(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 406 | """Return the absolute version of a path""" |
Guido van Rossum | 9787bea | 1999-01-29 22:30:41 +0000 | [diff] [blame] | 407 | try: |
| 408 | import win32api |
Guido van Rossum | 9787bea | 1999-01-29 22:30:41 +0000 | [diff] [blame] | 409 | except ImportError: |
Guido van Rossum | 823e91c | 2000-02-02 16:54:39 +0000 | [diff] [blame] | 410 | global abspath |
| 411 | def _abspath(path): |
| 412 | if not isabs(path): |
| 413 | path = join(os.getcwd(), path) |
| 414 | return normpath(path) |
| 415 | abspath = _abspath |
| 416 | return _abspath(path) |
Mark Hammond | 647d2fe | 2000-08-14 06:20:32 +0000 | [diff] [blame] | 417 | if path: # Empty path must return current working directory. |
| 418 | try: |
| 419 | path = win32api.GetFullPathName(path) |
| 420 | except win32api.error: |
| 421 | pass # Bad path - return unchanged. |
| 422 | else: |
| 423 | path = os.getcwd() |
Guido van Rossum | 6dfc792 | 1999-11-30 15:00:00 +0000 | [diff] [blame] | 424 | return normpath(path) |