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