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 | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 10 | import stat |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | import genericpath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | from genericpath import * |
Skip Montanaro | 4d5d5bf | 2000-07-13 01:01:03 +0000 | [diff] [blame] | 13 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 14 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 15 | "basename","dirname","commonprefix","getsize","getmtime", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 16 | "getatime","getctime", "islink","exists","lexists","isdir","isfile", |
Benjamin Peterson | d71ca41 | 2008-05-08 23:44:58 +0000 | [diff] [blame] | 17 | "ismount", "expanduser","expandvars","normpath","abspath", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 18 | "splitunc","curdir","pardir","sep","pathsep","defpath","altsep", |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | "extsep","devnull","realpath","supports_unicode_filenames","relpath"] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 20 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 21 | # strings representing various path-related bits and pieces |
| 22 | curdir = '.' |
| 23 | pardir = '..' |
| 24 | extsep = '.' |
| 25 | sep = '\\' |
| 26 | pathsep = ';' |
Skip Montanaro | 9ddac3e | 2003-03-28 22:23:24 +0000 | [diff] [blame] | 27 | altsep = '/' |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 28 | defpath = '.;C:\\bin' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 29 | if 'ce' in sys.builtin_module_names: |
| 30 | defpath = '\\Windows' |
| 31 | elif 'os2' in sys.builtin_module_names: |
Andrew MacIntyre | 437966c | 2003-02-17 09:17:50 +0000 | [diff] [blame] | 32 | # OS/2 w/ VACPP |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 33 | altsep = '/' |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 34 | devnull = 'nul' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 36 | # Normalize the case of a pathname and map slashes to backslashes. |
| 37 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 38 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 40 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 41 | """Normalize case of pathname. |
| 42 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 43 | Makes all characters lowercase and all slashes into backslashes.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 44 | return s.replace("/", "\\").lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 46 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 47 | # Return whether a path is absolute. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 48 | # Trivial in Posix, harder on the Mac or MS-DOS. |
| 49 | # 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] | 50 | # volume), or if a pathname after the volume letter and colon / UNC resource |
| 51 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 52 | |
| 53 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 54 | """Test whether a path is absolute""" |
| 55 | s = splitdrive(s)[1] |
| 56 | return s != '' and s[:1] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 57 | |
| 58 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 59 | # Join two (or more) paths. |
| 60 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 61 | def join(a, *p): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 62 | """Join two or more pathname components, inserting "\\" as needed. |
| 63 | If any component is an absolute path, all previous path components |
| 64 | will be discarded.""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 65 | path = a |
| 66 | for b in p: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 67 | b_wins = 0 # set to 1 iff b makes path irrelevant |
| 68 | if path == "": |
| 69 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 70 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 71 | elif isabs(b): |
| 72 | # This probably wipes out path so far. However, it's more |
| 73 | # complicated if path begins with a drive letter: |
| 74 | # 1. join('c:', '/a') == 'c:/a' |
| 75 | # 2. join('c:/', '/a') == 'c:/a' |
| 76 | # But |
| 77 | # 3. join('c:/a', '/b') == '/b' |
| 78 | # 4. join('c:', 'd:/') = 'd:/' |
| 79 | # 5. join('c:/', 'd:/') = 'd:/' |
| 80 | if path[1:2] != ":" or b[1:2] == ":": |
| 81 | # Path doesn't start with a drive letter, or cases 4 and 5. |
| 82 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 83 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 84 | # Else path has a drive letter, and b doesn't but is absolute. |
| 85 | elif len(path) > 3 or (len(path) == 3 and |
| 86 | path[-1] not in "/\\"): |
| 87 | # case 3 |
| 88 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 89 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 90 | if b_wins: |
| 91 | path = b |
| 92 | else: |
| 93 | # Join, and ensure there's a separator. |
| 94 | assert len(path) > 0 |
| 95 | if path[-1] in "/\\": |
| 96 | if b and b[0] in "/\\": |
| 97 | path += b[1:] |
| 98 | else: |
| 99 | path += b |
| 100 | elif path[-1] == ":": |
| 101 | path += b |
| 102 | elif b: |
| 103 | if b[0] in "/\\": |
| 104 | path += b |
| 105 | else: |
| 106 | path += "\\" + b |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 107 | else: |
| 108 | # path is not empty and does not end with a backslash, |
| 109 | # but b is empty; since, e.g., split('a/') produces |
| 110 | # ('a', ''), it's best if join() adds a backslash in |
| 111 | # this case. |
| 112 | path += '\\' |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 114 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 115 | |
| 116 | |
| 117 | # 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] | 118 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 119 | # It is always true that drivespec + pathspec == p |
| 120 | def splitdrive(p): |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 121 | """Split a pathname into drive and path specifiers. Returns a 2-tuple |
| 122 | "(drive,path)"; either part may be empty""" |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 123 | if p[1:2] == ':': |
| 124 | return p[0:2], p[2:] |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 125 | return '', p |
| 126 | |
| 127 | |
| 128 | # Parse UNC paths |
| 129 | def splitunc(p): |
| 130 | """Split a pathname into UNC mount point and relative path specifiers. |
| 131 | |
| 132 | Return a 2-tuple (unc, rest); either part may be empty. |
| 133 | If unc is not empty, it has the form '//host/mount' (or similar |
| 134 | using backslashes). unc+rest is always the input path. |
| 135 | Paths containing drive letters never have an UNC part. |
| 136 | """ |
| 137 | if p[1:2] == ':': |
| 138 | return '', p # Drive letter present |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 139 | firstTwo = p[0:2] |
| 140 | if firstTwo == '//' or firstTwo == '\\\\': |
| 141 | # is a UNC path: |
| 142 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter |
| 143 | # \\machine\mountpoint\directories... |
| 144 | # directory ^^^^^^^^^^^^^^^ |
| 145 | normp = normcase(p) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 146 | index = normp.find('\\', 2) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 147 | if index == -1: |
| 148 | ##raise RuntimeError, 'illegal UNC path: "' + p + '"' |
| 149 | return ("", p) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 150 | index = normp.find('\\', index + 1) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 151 | if index == -1: |
| 152 | index = len(p) |
| 153 | return p[:index], p[index:] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 154 | return '', p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | # 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] | 158 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 159 | # join(head, tail) == p holds. |
| 160 | # The resulting head won't end in '/' unless it is the root. |
| 161 | |
| 162 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 163 | """Split a pathname. |
| 164 | |
| 165 | Return tuple (head, tail) where tail is everything after the final slash. |
| 166 | Either part may be empty.""" |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 168 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 169 | # set i to index beyond p's last slash |
| 170 | i = len(p) |
| 171 | while i and p[i-1] not in '/\\': |
| 172 | i = i - 1 |
| 173 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 174 | # remove trailing slashes from head, unless it's all slashes |
| 175 | head2 = head |
| 176 | while head2 and head2[-1] in '/\\': |
| 177 | head2 = head2[:-1] |
| 178 | head = head2 or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 179 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 183 | # 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] | 184 | # pathname component; the root is everything before that. |
| 185 | # It is always true that root + ext == p. |
| 186 | |
| 187 | def splitext(p): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 188 | return genericpath._splitext(p, sep, altsep, extsep) |
| 189 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | # Return the tail (basename) part of a path. |
| 193 | |
| 194 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 195 | """Returns the final component of a pathname""" |
| 196 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 197 | |
| 198 | |
| 199 | # Return the head (dirname) part of a path. |
| 200 | |
| 201 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 202 | """Returns the directory component of a pathname""" |
| 203 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 205 | # Is a path a symbolic link? |
| 206 | # This will always return false on systems where posix.lstat doesn't exist. |
| 207 | |
| 208 | def islink(path): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 209 | """Test for symbolic link. |
| 210 | On WindowsNT/95 and OS/2 always returns false |
| 211 | """ |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 212 | return False |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 213 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 214 | # alias exists to lexists |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 215 | lexists = exists |
| 216 | |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 217 | # Is a path a mount point? Either a root (with or without drive letter) |
| 218 | # 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] | 219 | |
| 220 | def ismount(path): |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 221 | """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] | 222 | unc, rest = splitunc(path) |
| 223 | if unc: |
| 224 | return rest in ("", "/", "\\") |
Guido van Rossum | ca99c2c | 1998-01-19 22:25:59 +0000 | [diff] [blame] | 225 | p = splitdrive(path)[1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 226 | return len(p) == 1 and p[0] in '/\\' |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 227 | |
| 228 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 229 | # Expand paths beginning with '~' or '~user'. |
| 230 | # '~' means $HOME; '~user' means that user's home directory. |
| 231 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 232 | # the path is returned unchanged (leaving error reporting to whatever |
| 233 | # function is called with the expanded path as argument). |
| 234 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 235 | # (A function should also be defined to do full *sh-style environment |
| 236 | # variable expansion.) |
| 237 | |
| 238 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 239 | """Expand ~ and ~user constructs. |
| 240 | |
| 241 | If user or $HOME is unknown, do nothing.""" |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 242 | if path[:1] != '~': |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 243 | return path |
| 244 | i, n = 1, len(path) |
| 245 | while i < n and path[i] not in '/\\': |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 246 | i = i + 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 247 | |
| 248 | if 'HOME' in os.environ: |
| 249 | userhome = os.environ['HOME'] |
| 250 | elif 'USERPROFILE' in os.environ: |
| 251 | userhome = os.environ['USERPROFILE'] |
| 252 | elif not 'HOMEPATH' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 253 | return path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 254 | else: |
| 255 | try: |
| 256 | drive = os.environ['HOMEDRIVE'] |
| 257 | except KeyError: |
| 258 | drive = '' |
| 259 | userhome = join(drive, os.environ['HOMEPATH']) |
| 260 | |
| 261 | if i != 1: #~user |
| 262 | userhome = join(dirname(userhome), path[1:i]) |
| 263 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 264 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 265 | |
| 266 | |
| 267 | # Expand paths containing shell variable substitutions. |
| 268 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 269 | # - no expansion within single quotes |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 270 | # - '$$' is translated into '$' |
| 271 | # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 272 | # - ${varname} is accepted. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 273 | # - $varname is accepted. |
| 274 | # - %varname% is accepted. |
| 275 | # - varnames can be made out of letters, digits and the characters '_-' |
| 276 | # (though is not verifed in the ${varname} and %varname% cases) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 277 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 278 | # XXX except '^|<>='. |
| 279 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 280 | def expandvars(path): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 281 | """Expand shell variables of the forms $var, ${var} and %var%. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 282 | |
| 283 | Unknown variables are left unchanged.""" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 284 | if '$' not in path and '%' not in path: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 285 | return path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 286 | import string |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 287 | varchars = string.ascii_letters + string.digits + '_-' |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 288 | res = '' |
| 289 | index = 0 |
| 290 | pathlen = len(path) |
| 291 | while index < pathlen: |
| 292 | c = path[index] |
| 293 | if c == '\'': # no expansion within single quotes |
| 294 | path = path[index + 1:] |
| 295 | pathlen = len(path) |
| 296 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 297 | index = path.index('\'') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 298 | res = res + '\'' + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 299 | except ValueError: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 300 | res = res + path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 301 | index = pathlen - 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 302 | elif c == '%': # variable or '%' |
| 303 | if path[index + 1:index + 2] == '%': |
| 304 | res = res + c |
| 305 | index = index + 1 |
| 306 | else: |
| 307 | path = path[index+1:] |
| 308 | pathlen = len(path) |
| 309 | try: |
| 310 | index = path.index('%') |
| 311 | except ValueError: |
| 312 | res = res + '%' + path |
| 313 | index = pathlen - 1 |
| 314 | else: |
| 315 | var = path[:index] |
| 316 | if var in os.environ: |
| 317 | res = res + os.environ[var] |
| 318 | else: |
| 319 | res = res + '%' + var + '%' |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 320 | elif c == '$': # variable or '$$' |
| 321 | if path[index + 1:index + 2] == '$': |
| 322 | res = res + c |
| 323 | index = index + 1 |
| 324 | elif path[index + 1:index + 2] == '{': |
| 325 | path = path[index+2:] |
| 326 | pathlen = len(path) |
| 327 | try: |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 328 | index = path.index('}') |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 329 | var = path[:index] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 330 | if var in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 331 | res = res + os.environ[var] |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 332 | else: |
| 333 | res = res + '${' + var + '}' |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 334 | except ValueError: |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 335 | res = res + '${' + path |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 336 | index = pathlen - 1 |
| 337 | else: |
| 338 | var = '' |
| 339 | index = index + 1 |
| 340 | c = path[index:index + 1] |
| 341 | while c != '' and c in varchars: |
| 342 | var = var + c |
| 343 | index = index + 1 |
| 344 | c = path[index:index + 1] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 345 | if var in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 346 | res = res + os.environ[var] |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 347 | else: |
| 348 | res = res + '$' + var |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 349 | if c != '': |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 350 | index = index - 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 351 | else: |
| 352 | res = res + c |
| 353 | index = index + 1 |
| 354 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 355 | |
| 356 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 357 | # 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] | 358 | # Previously, this function also truncated pathnames to 8+3 format, |
| 359 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 360 | |
| 361 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 362 | """Normalize path, eliminating double slashes, etc.""" |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 363 | path = path.replace("/", "\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 364 | prefix, path = splitdrive(path) |
Brett Cannon | bdc3627 | 2004-07-10 20:42:22 +0000 | [diff] [blame] | 365 | # We need to be careful here. If the prefix is empty, and the path starts |
| 366 | # with a backslash, it could either be an absolute path on the current |
| 367 | # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It |
| 368 | # is therefore imperative NOT to collapse multiple backslashes blindly in |
| 369 | # that case. |
| 370 | # The code below preserves multiple backslashes when there is no drive |
| 371 | # letter. This means that the invalid filename \\\a\b is preserved |
| 372 | # unchanged, where a\\\b is normalised to a\b. It's not clear that there |
| 373 | # is any better behaviour for such edge cases. |
| 374 | if prefix == '': |
| 375 | # No drive letter - preserve initial backslashes |
| 376 | while path[:1] == "\\": |
| 377 | prefix = prefix + "\\" |
| 378 | path = path[1:] |
| 379 | else: |
| 380 | # We have a drive letter - collapse initial backslashes |
| 381 | if path.startswith("\\"): |
| 382 | prefix = prefix + "\\" |
| 383 | path = path.lstrip("\\") |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 384 | comps = path.split("\\") |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 385 | i = 0 |
| 386 | while i < len(comps): |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 387 | if comps[i] in ('.', ''): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 388 | del comps[i] |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 389 | elif comps[i] == '..': |
| 390 | if i > 0 and comps[i-1] != '..': |
| 391 | del comps[i-1:i+1] |
| 392 | i -= 1 |
| 393 | elif i == 0 and prefix.endswith("\\"): |
| 394 | del comps[i] |
| 395 | else: |
| 396 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 397 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 398 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 399 | # If the path is now empty, substitute '.' |
| 400 | if not prefix and not comps: |
| 401 | comps.append('.') |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 402 | return prefix + "\\".join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 403 | |
| 404 | |
| 405 | # Return an absolute path. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 406 | try: |
| 407 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 408 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 409 | except ImportError: # not running on Windows - mock up something sensible |
| 410 | def abspath(path): |
| 411 | """Return the absolute version of a path.""" |
| 412 | if not isabs(path): |
| 413 | path = join(os.getcwd(), path) |
| 414 | return normpath(path) |
| 415 | |
| 416 | else: # use native Windows method on Windows |
| 417 | def abspath(path): |
| 418 | """Return the absolute version of a path.""" |
| 419 | |
| 420 | if path: # Empty path must return current working directory. |
| 421 | try: |
| 422 | path = _getfullpathname(path) |
| 423 | except WindowsError: |
| 424 | pass # Bad path - return unchanged. |
| 425 | else: |
| 426 | path = os.getcwd() |
| 427 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 428 | |
| 429 | # realpath is a no-op on systems without islink support |
| 430 | realpath = abspath |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 431 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 432 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 433 | sys.getwindowsversion()[3] >= 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 434 | |
| 435 | def relpath(path, start=curdir): |
| 436 | """Return a relative version of a path""" |
| 437 | |
| 438 | if not path: |
| 439 | raise ValueError("no path specified") |
| 440 | start_list = abspath(start).split(sep) |
| 441 | path_list = abspath(path).split(sep) |
| 442 | if start_list[0].lower() != path_list[0].lower(): |
| 443 | unc_path, rest = splitunc(path) |
| 444 | unc_start, rest = splitunc(start) |
| 445 | if bool(unc_path) ^ bool(unc_start): |
| 446 | raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" |
| 447 | % (path, start)) |
| 448 | else: |
| 449 | raise ValueError("path is on drive %s, start on drive %s" |
| 450 | % (path_list[0], start_list[0])) |
| 451 | # Work out how much of the filepath is shared by start and path. |
| 452 | for i in range(min(len(start_list), len(path_list))): |
| 453 | if start_list[i].lower() != path_list[i].lower(): |
| 454 | break |
| 455 | else: |
| 456 | i += 1 |
| 457 | |
| 458 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 459 | if not rel_list: |
| 460 | return curdir |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 461 | return join(*rel_list) |