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 |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 9 | import sys |
Christian Heimes | c5f05e4 | 2008-02-23 17:40:11 +0000 | [diff] [blame] | 10 | import stat |
Martin v. Löwis | 05c075d | 2007-03-07 11:04:33 +0000 | [diff] [blame] | 11 | import genericpath |
Benjamin Peterson | 0893a0a | 2008-05-09 00:27:01 +0000 | [diff] [blame] | 12 | import warnings |
| 13 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 14 | from genericpath import * |
Skip Montanaro | 4d5d5bf | 2000-07-13 01:01:03 +0000 | [diff] [blame] | 15 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 16 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 17 | "basename","dirname","commonprefix","getsize","getmtime", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 18 | "getatime","getctime", "islink","exists","lexists","isdir","isfile", |
| 19 | "ismount","walk","expanduser","expandvars","normpath","abspath", |
| 20 | "splitunc","curdir","pardir","sep","pathsep","defpath","altsep", |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 21 | "extsep","devnull","realpath","supports_unicode_filenames","relpath"] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 22 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 23 | # strings representing various path-related bits and pieces |
| 24 | curdir = '.' |
| 25 | pardir = '..' |
| 26 | extsep = '.' |
| 27 | sep = '\\' |
| 28 | pathsep = ';' |
Skip Montanaro | 9ddac3e | 2003-03-28 22:23:24 +0000 | [diff] [blame] | 29 | altsep = '/' |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 30 | defpath = '.;C:\\bin' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 31 | if 'ce' in sys.builtin_module_names: |
| 32 | defpath = '\\Windows' |
| 33 | elif 'os2' in sys.builtin_module_names: |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 34 | # OS/2 w/ VACPP |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 35 | altsep = '/' |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 36 | devnull = 'nul' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 38 | # Normalize the case of a pathname and map slashes to backslashes. |
| 39 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 40 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 42 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 43 | """Normalize case of pathname. |
| 44 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 45 | Makes all characters lowercase and all slashes into backslashes.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 46 | return s.replace("/", "\\").lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 48 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 49 | # Return whether a path is absolute. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 50 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 51 | # 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] | 52 | # volume), or if a pathname after the volume letter and colon / UNC resource |
| 53 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 54 | |
| 55 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 56 | """Test whether a path is absolute""" |
| 57 | s = splitdrive(s)[1] |
| 58 | return s != '' and s[:1] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 59 | |
| 60 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 61 | # Join two (or more) paths. |
| 62 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 63 | def join(a, *p): |
Georg Brandl | da5f16a | 2007-08-23 21:27:57 +0000 | [diff] [blame] | 64 | """Join two or more pathname components, inserting "\\" as needed. |
| 65 | If any component is an absolute path, all previous path components |
| 66 | will be discarded.""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 67 | path = a |
| 68 | for b in p: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 69 | b_wins = 0 # set to 1 iff b makes path irrelevant |
| 70 | if path == "": |
| 71 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 72 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 73 | elif isabs(b): |
| 74 | # This probably wipes out path so far. However, it's more |
| 75 | # complicated if path begins with a drive letter: |
| 76 | # 1. join('c:', '/a') == 'c:/a' |
| 77 | # 2. join('c:/', '/a') == 'c:/a' |
| 78 | # But |
| 79 | # 3. join('c:/a', '/b') == '/b' |
| 80 | # 4. join('c:', 'd:/') = 'd:/' |
| 81 | # 5. join('c:/', 'd:/') = 'd:/' |
| 82 | if path[1:2] != ":" or b[1:2] == ":": |
| 83 | # Path doesn't start with a drive letter, or cases 4 and 5. |
| 84 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 85 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 86 | # Else path has a drive letter, and b doesn't but is absolute. |
| 87 | elif len(path) > 3 or (len(path) == 3 and |
| 88 | path[-1] not in "/\\"): |
| 89 | # case 3 |
| 90 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 91 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 92 | if b_wins: |
| 93 | path = b |
| 94 | else: |
| 95 | # Join, and ensure there's a separator. |
| 96 | assert len(path) > 0 |
| 97 | if path[-1] in "/\\": |
| 98 | if b and b[0] in "/\\": |
| 99 | path += b[1:] |
| 100 | else: |
| 101 | path += b |
| 102 | elif path[-1] == ":": |
| 103 | path += b |
| 104 | elif b: |
| 105 | if b[0] in "/\\": |
| 106 | path += b |
| 107 | else: |
| 108 | path += "\\" + b |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 109 | else: |
| 110 | # path is not empty and does not end with a backslash, |
| 111 | # but b is empty; since, e.g., split('a/') produces |
| 112 | # ('a', ''), it's best if join() adds a backslash in |
| 113 | # this case. |
| 114 | path += '\\' |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 116 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | # 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] | 120 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 121 | # It is always true that drivespec + pathspec == p |
| 122 | def splitdrive(p): |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 123 | """Split a pathname into drive and path specifiers. Returns a 2-tuple |
| 124 | "(drive,path)"; either part may be empty""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 125 | if p[1:2] == ':': |
| 126 | return p[0:2], p[2:] |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 127 | return '', p |
| 128 | |
| 129 | |
| 130 | # Parse UNC paths |
| 131 | def splitunc(p): |
| 132 | """Split a pathname into UNC mount point and relative path specifiers. |
| 133 | |
| 134 | Return a 2-tuple (unc, rest); either part may be empty. |
| 135 | If unc is not empty, it has the form '//host/mount' (or similar |
| 136 | using backslashes). unc+rest is always the input path. |
| 137 | Paths containing drive letters never have an UNC part. |
| 138 | """ |
| 139 | if p[1:2] == ':': |
| 140 | return '', p # Drive letter present |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 141 | firstTwo = p[0:2] |
| 142 | if firstTwo == '//' or firstTwo == '\\\\': |
| 143 | # is a UNC path: |
| 144 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter |
| 145 | # \\machine\mountpoint\directories... |
| 146 | # directory ^^^^^^^^^^^^^^^ |
Serhiy Storchaka | dd5a46c | 2013-12-16 15:15:29 +0200 | [diff] [blame] | 147 | normp = p.replace('\\', '/') |
| 148 | index = normp.find('/', 2) |
| 149 | if index <= 2: |
| 150 | return '', p |
| 151 | index2 = normp.find('/', index + 1) |
| 152 | # a UNC path can't have two slashes in a row |
| 153 | # (after the initial two) |
| 154 | if index2 == index + 1: |
| 155 | return '', p |
| 156 | if index2 == -1: |
| 157 | index2 = len(p) |
| 158 | return p[:index2], p[index2:] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 159 | return '', p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | # 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] | 163 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 164 | # join(head, tail) == p holds. |
| 165 | # The resulting head won't end in '/' unless it is the root. |
| 166 | |
| 167 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 168 | """Split a pathname. |
| 169 | |
| 170 | Return tuple (head, tail) where tail is everything after the final slash. |
| 171 | Either part may be empty.""" |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 173 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 174 | # set i to index beyond p's last slash |
| 175 | i = len(p) |
| 176 | while i and p[i-1] not in '/\\': |
| 177 | i = i - 1 |
| 178 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 179 | # remove trailing slashes from head, unless it's all slashes |
| 180 | head2 = head |
| 181 | while head2 and head2[-1] in '/\\': |
| 182 | head2 = head2[:-1] |
| 183 | head = head2 or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 184 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 188 | # 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] | 189 | # pathname component; the root is everything before that. |
| 190 | # It is always true that root + ext == p. |
| 191 | |
| 192 | def splitext(p): |
Martin v. Löwis | 05c075d | 2007-03-07 11:04:33 +0000 | [diff] [blame] | 193 | return genericpath._splitext(p, sep, altsep, extsep) |
| 194 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | # Return the tail (basename) part of a path. |
| 198 | |
| 199 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 200 | """Returns the final component of a pathname""" |
| 201 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | # Return the head (dirname) part of a path. |
| 205 | |
| 206 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 207 | """Returns the directory component of a pathname""" |
| 208 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 210 | # Is a path a symbolic link? |
| 211 | # This will always return false on systems where posix.lstat doesn't exist. |
| 212 | |
| 213 | def islink(path): |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 214 | """Test for symbolic link. |
| 215 | On WindowsNT/95 and OS/2 always returns false |
| 216 | """ |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 217 | return False |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 218 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 219 | # alias exists to lexists |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 220 | lexists = exists |
| 221 | |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 222 | # Is a path a mount point? Either a root (with or without drive letter) |
| 223 | # 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] | 224 | |
| 225 | def ismount(path): |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 226 | """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] | 227 | unc, rest = splitunc(path) |
| 228 | if unc: |
| 229 | return rest in ("", "/", "\\") |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 230 | p = splitdrive(path)[1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 231 | return len(p) == 1 and p[0] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 232 | |
| 233 | |
| 234 | # Directory tree walk. |
| 235 | # For each directory under top (including top itself, but excluding |
| 236 | # '.' and '..'), func(arg, dirname, filenames) is called, where |
| 237 | # dirname is the name of the directory and filenames is the list |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 238 | # of files (and subdirectories etc.) in the directory. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 239 | # The func may modify the filenames list, to implement a filter, |
| 240 | # or to impose a different order of visiting. |
| 241 | |
| 242 | def walk(top, func, arg): |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 243 | """Directory tree walk with callback function. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 244 | |
Tim Peters | cf5e6a4 | 2001-10-10 04:16:20 +0000 | [diff] [blame] | 245 | For each directory in the directory tree rooted at top (including top |
| 246 | itself, but excluding '.' and '..'), call func(arg, dirname, fnames). |
| 247 | dirname is the name of the directory, and fnames a list of the names of |
| 248 | the files and subdirectories in dirname (excluding '.' and '..'). func |
| 249 | may modify the fnames list in-place (e.g. via del or slice assignment), |
| 250 | and walk will only recurse into the subdirectories whose names remain in |
| 251 | fnames; this can be used to implement a filter, or to impose a specific |
| 252 | order of visiting. No semantics are defined for, or required of, arg, |
| 253 | beyond that arg is always passed to func. It can be used, e.g., to pass |
| 254 | a filename pattern, or a mutable object designed to accumulate |
| 255 | statistics. Passing None for arg is common.""" |
Philip Jenvey | d846f1d | 2009-05-08 02:28:39 +0000 | [diff] [blame] | 256 | warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.", |
| 257 | stacklevel=2) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 258 | try: |
| 259 | names = os.listdir(top) |
| 260 | except os.error: |
| 261 | return |
| 262 | func(arg, top, names) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 263 | for name in names: |
Georg Brandl | e2a902c | 2008-01-06 14:17:36 +0000 | [diff] [blame] | 264 | name = join(top, name) |
| 265 | if isdir(name): |
| 266 | walk(name, func, arg) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 267 | |
| 268 | |
| 269 | # Expand paths beginning with '~' or '~user'. |
| 270 | # '~' means $HOME; '~user' means that user's home directory. |
| 271 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 272 | # the path is returned unchanged (leaving error reporting to whatever |
| 273 | # function is called with the expanded path as argument). |
| 274 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 275 | # (A function should also be defined to do full *sh-style environment |
| 276 | # variable expansion.) |
| 277 | |
| 278 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 279 | """Expand ~ and ~user constructs. |
| 280 | |
| 281 | If user or $HOME is unknown, do nothing.""" |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 282 | if path[:1] != '~': |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 283 | return path |
| 284 | i, n = 1, len(path) |
| 285 | while i < n and path[i] not in '/\\': |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 286 | i = i + 1 |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 287 | |
| 288 | if 'HOME' in os.environ: |
| 289 | userhome = os.environ['HOME'] |
| 290 | elif 'USERPROFILE' in os.environ: |
| 291 | userhome = os.environ['USERPROFILE'] |
| 292 | elif not 'HOMEPATH' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 293 | return path |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 294 | else: |
| 295 | try: |
| 296 | drive = os.environ['HOMEDRIVE'] |
| 297 | except KeyError: |
| 298 | drive = '' |
| 299 | userhome = join(drive, os.environ['HOMEPATH']) |
| 300 | |
| 301 | if i != 1: #~user |
| 302 | userhome = join(dirname(userhome), path[1:i]) |
| 303 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 304 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 305 | |
| 306 | |
| 307 | # Expand paths containing shell variable substitutions. |
| 308 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 309 | # - no expansion within single quotes |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 310 | # - '$$' is translated into '$' |
| 311 | # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 312 | # - ${varname} is accepted. |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 313 | # - $varname is accepted. |
| 314 | # - %varname% is accepted. |
| 315 | # - varnames can be made out of letters, digits and the characters '_-' |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 316 | # (though is not verified in the ${varname} and %varname% cases) |
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): |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 321 | """Expand shell variables of the forms $var, ${var} and %var%. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 322 | |
| 323 | Unknown variables are left unchanged.""" |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 324 | if '$' not in path and '%' not in path: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 325 | return path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 326 | import string |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 327 | varchars = string.ascii_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 |
Georg Brandl | 03b90d8 | 2007-03-13 22:07:36 +0000 | [diff] [blame] | 342 | elif c == '%': # variable or '%' |
| 343 | if path[index + 1:index + 2] == '%': |
| 344 | res = res + c |
| 345 | index = index + 1 |
| 346 | else: |
| 347 | path = path[index+1:] |
| 348 | pathlen = len(path) |
| 349 | try: |
| 350 | index = path.index('%') |
| 351 | except ValueError: |
| 352 | res = res + '%' + path |
| 353 | index = pathlen - 1 |
| 354 | else: |
| 355 | var = path[:index] |
| 356 | if var in os.environ: |
| 357 | res = res + os.environ[var] |
| 358 | else: |
| 359 | res = res + '%' + var + '%' |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 360 | elif c == '$': # variable or '$$' |
| 361 | if path[index + 1:index + 2] == '$': |
| 362 | res = res + c |
| 363 | index = index + 1 |
| 364 | elif path[index + 1:index + 2] == '{': |
| 365 | path = path[index+2:] |
| 366 | pathlen = len(path) |
| 367 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 368 | index = path.index('}') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 369 | var = path[:index] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 370 | if var in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 371 | res = res + os.environ[var] |
Sjoerd Mullender | 33a0a06 | 2007-01-16 16:42:38 +0000 | [diff] [blame] | 372 | else: |
| 373 | res = res + '${' + var + '}' |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 374 | except ValueError: |
Sjoerd Mullender | 33a0a06 | 2007-01-16 16:42:38 +0000 | [diff] [blame] | 375 | res = res + '${' + path |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 376 | index = pathlen - 1 |
| 377 | else: |
| 378 | var = '' |
| 379 | index = index + 1 |
| 380 | c = path[index:index + 1] |
| 381 | while c != '' and c in varchars: |
| 382 | var = var + c |
| 383 | index = index + 1 |
| 384 | c = path[index:index + 1] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 385 | if var in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 386 | res = res + os.environ[var] |
Sjoerd Mullender | 33a0a06 | 2007-01-16 16:42:38 +0000 | [diff] [blame] | 387 | else: |
| 388 | res = res + '$' + var |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 389 | if c != '': |
Sjoerd Mullender | 33a0a06 | 2007-01-16 16:42:38 +0000 | [diff] [blame] | 390 | index = index - 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 391 | else: |
| 392 | res = res + c |
| 393 | index = index + 1 |
| 394 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 395 | |
| 396 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 397 | # 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] | 398 | # Previously, this function also truncated pathnames to 8+3 format, |
| 399 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 400 | |
| 401 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 402 | """Normalize path, eliminating double slashes, etc.""" |
Ezio Melotti | b5689de | 2010-01-12 03:32:05 +0000 | [diff] [blame] | 403 | # Preserve unicode (if path is unicode) |
| 404 | backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.') |
Georg Brandl | e277325 | 2010-08-01 19:14:56 +0000 | [diff] [blame] | 405 | if path.startswith(('\\\\.\\', '\\\\?\\')): |
| 406 | # in the case of paths with these prefixes: |
| 407 | # \\.\ -> device names |
| 408 | # \\?\ -> literal paths |
| 409 | # do not do any normalization, but return the path unchanged |
| 410 | return path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 411 | path = path.replace("/", "\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 412 | prefix, path = splitdrive(path) |
Brett Cannon | bdc3627 | 2004-07-10 20:42:22 +0000 | [diff] [blame] | 413 | # We need to be careful here. If the prefix is empty, and the path starts |
| 414 | # with a backslash, it could either be an absolute path on the current |
| 415 | # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It |
| 416 | # is therefore imperative NOT to collapse multiple backslashes blindly in |
| 417 | # that case. |
| 418 | # The code below preserves multiple backslashes when there is no drive |
| 419 | # letter. This means that the invalid filename \\\a\b is preserved |
| 420 | # unchanged, where a\\\b is normalised to a\b. It's not clear that there |
| 421 | # is any better behaviour for such edge cases. |
| 422 | if prefix == '': |
| 423 | # No drive letter - preserve initial backslashes |
| 424 | while path[:1] == "\\": |
Ezio Melotti | b5689de | 2010-01-12 03:32:05 +0000 | [diff] [blame] | 425 | prefix = prefix + backslash |
Brett Cannon | bdc3627 | 2004-07-10 20:42:22 +0000 | [diff] [blame] | 426 | path = path[1:] |
| 427 | else: |
| 428 | # We have a drive letter - collapse initial backslashes |
| 429 | if path.startswith("\\"): |
Ezio Melotti | b5689de | 2010-01-12 03:32:05 +0000 | [diff] [blame] | 430 | prefix = prefix + backslash |
Brett Cannon | bdc3627 | 2004-07-10 20:42:22 +0000 | [diff] [blame] | 431 | path = path.lstrip("\\") |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 432 | comps = path.split("\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 433 | i = 0 |
| 434 | while i < len(comps): |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 435 | if comps[i] in ('.', ''): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 436 | del comps[i] |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 437 | elif comps[i] == '..': |
| 438 | if i > 0 and comps[i-1] != '..': |
| 439 | del comps[i-1:i+1] |
| 440 | i -= 1 |
| 441 | elif i == 0 and prefix.endswith("\\"): |
| 442 | del comps[i] |
| 443 | else: |
| 444 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 445 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 446 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 447 | # If the path is now empty, substitute '.' |
| 448 | if not prefix and not comps: |
Ezio Melotti | b5689de | 2010-01-12 03:32:05 +0000 | [diff] [blame] | 449 | comps.append(dot) |
| 450 | return prefix + backslash.join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 451 | |
| 452 | |
| 453 | # Return an absolute path. |
Tim Peters | 21fbd57 | 2006-04-21 21:18:10 +0000 | [diff] [blame] | 454 | try: |
| 455 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 456 | |
Tim Peters | 21fbd57 | 2006-04-21 21:18:10 +0000 | [diff] [blame] | 457 | except ImportError: # not running on Windows - mock up something sensible |
| 458 | def abspath(path): |
| 459 | """Return the absolute version of a path.""" |
| 460 | if not isabs(path): |
Ezio Melotti | 4cc80ca | 2010-02-20 08:09:39 +0000 | [diff] [blame] | 461 | if isinstance(path, unicode): |
| 462 | cwd = os.getcwdu() |
| 463 | else: |
| 464 | cwd = os.getcwd() |
| 465 | path = join(cwd, path) |
Tim Peters | 21fbd57 | 2006-04-21 21:18:10 +0000 | [diff] [blame] | 466 | return normpath(path) |
| 467 | |
| 468 | else: # use native Windows method on Windows |
| 469 | def abspath(path): |
| 470 | """Return the absolute version of a path.""" |
| 471 | |
| 472 | if path: # Empty path must return current working directory. |
| 473 | try: |
| 474 | path = _getfullpathname(path) |
| 475 | except WindowsError: |
| 476 | pass # Bad path - return unchanged. |
Ezio Melotti | 4cc80ca | 2010-02-20 08:09:39 +0000 | [diff] [blame] | 477 | elif isinstance(path, unicode): |
| 478 | path = os.getcwdu() |
Tim Peters | 21fbd57 | 2006-04-21 21:18:10 +0000 | [diff] [blame] | 479 | else: |
| 480 | path = os.getcwd() |
| 481 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 482 | |
| 483 | # realpath is a no-op on systems without islink support |
| 484 | realpath = abspath |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 485 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 486 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 487 | sys.getwindowsversion()[3] >= 2) |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 488 | |
Hirokazu Yamamoto | 8596ef7 | 2010-10-19 01:23:51 +0000 | [diff] [blame] | 489 | def _abspath_split(path): |
| 490 | abs = abspath(normpath(path)) |
| 491 | prefix, rest = splitunc(abs) |
| 492 | is_unc = bool(prefix) |
| 493 | if not is_unc: |
| 494 | prefix, rest = splitdrive(abs) |
| 495 | return is_unc, prefix, [x for x in rest.split(sep) if x] |
| 496 | |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 497 | def relpath(path, start=curdir): |
| 498 | """Return a relative version of a path""" |
| 499 | |
| 500 | if not path: |
| 501 | raise ValueError("no path specified") |
Hirokazu Yamamoto | 8596ef7 | 2010-10-19 01:23:51 +0000 | [diff] [blame] | 502 | |
| 503 | start_is_unc, start_prefix, start_list = _abspath_split(start) |
| 504 | path_is_unc, path_prefix, path_list = _abspath_split(path) |
| 505 | |
| 506 | if path_is_unc ^ start_is_unc: |
| 507 | raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" |
| 508 | % (path, start)) |
| 509 | if path_prefix.lower() != start_prefix.lower(): |
| 510 | if path_is_unc: |
| 511 | raise ValueError("path is on UNC root %s, start on UNC root %s" |
| 512 | % (path_prefix, start_prefix)) |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 513 | else: |
| 514 | raise ValueError("path is on drive %s, start on drive %s" |
Hirokazu Yamamoto | 8596ef7 | 2010-10-19 01:23:51 +0000 | [diff] [blame] | 515 | % (path_prefix, start_prefix)) |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 516 | # Work out how much of the filepath is shared by start and path. |
Hirokazu Yamamoto | 8596ef7 | 2010-10-19 01:23:51 +0000 | [diff] [blame] | 517 | i = 0 |
| 518 | for e1, e2 in zip(start_list, path_list): |
| 519 | if e1.lower() != e2.lower(): |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 520 | break |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 521 | i += 1 |
| 522 | |
| 523 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
Georg Brandl | 183a084 | 2008-01-06 14:27:15 +0000 | [diff] [blame] | 524 | if not rel_list: |
| 525 | return curdir |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 526 | return join(*rel_list) |
Brian Curtin | caea7e8 | 2011-06-08 19:29:53 -0500 | [diff] [blame] | 527 | |
| 528 | try: |
| 529 | # The genericpath.isdir implementation uses os.stat and checks the mode |
| 530 | # attribute to tell whether or not the path is a directory. |
| 531 | # This is overkill on Windows - just pass the path to GetFileAttributes |
| 532 | # and check the attribute from there. |
Brian Curtin | 5446f08 | 2011-06-09 10:00:42 -0500 | [diff] [blame] | 533 | from nt import _isdir as isdir |
Brian Curtin | caea7e8 | 2011-06-08 19:29:53 -0500 | [diff] [blame] | 534 | except ImportError: |
Brian Curtin | 5446f08 | 2011-06-09 10:00:42 -0500 | [diff] [blame] | 535 | # Use genericpath.isdir as imported above. |
| 536 | pass |