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", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 19 | "extsep","devnull","realpath","supports_unicode_filenames","relpath", |
Brian Curtin | ae57cec | 2012-12-26 08:22:00 -0600 | [diff] [blame] | 20 | "samefile", "sameopenfile", "samestat",] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 21 | |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 22 | # strings representing various path-related bits and pieces |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 23 | # These are primarily for export; internally, they are hardcoded. |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 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' |
Martin v. Löwis | bdec50f | 2004-06-08 08:29:33 +0000 | [diff] [blame] | 33 | devnull = 'nul' |
Skip Montanaro | 117910d | 2003-02-14 19:35:31 +0000 | [diff] [blame] | 34 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 35 | def _get_empty(path): |
| 36 | if isinstance(path, bytes): |
| 37 | return b'' |
| 38 | else: |
| 39 | return '' |
| 40 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 41 | def _get_sep(path): |
| 42 | if isinstance(path, bytes): |
| 43 | return b'\\' |
| 44 | else: |
| 45 | return '\\' |
| 46 | |
| 47 | def _get_altsep(path): |
| 48 | if isinstance(path, bytes): |
| 49 | return b'/' |
| 50 | else: |
| 51 | return '/' |
| 52 | |
| 53 | def _get_bothseps(path): |
| 54 | if isinstance(path, bytes): |
| 55 | return b'\\/' |
| 56 | else: |
| 57 | return '\\/' |
| 58 | |
| 59 | def _get_dot(path): |
| 60 | if isinstance(path, bytes): |
| 61 | return b'.' |
| 62 | else: |
| 63 | return '.' |
| 64 | |
| 65 | def _get_colon(path): |
| 66 | if isinstance(path, bytes): |
| 67 | return b':' |
| 68 | else: |
| 69 | return ':' |
| 70 | |
Georg Brandl | cfb6821 | 2010-07-31 21:40:15 +0000 | [diff] [blame] | 71 | def _get_special(path): |
| 72 | if isinstance(path, bytes): |
| 73 | return (b'\\\\.\\', b'\\\\?\\') |
| 74 | else: |
| 75 | return ('\\\\.\\', '\\\\?\\') |
| 76 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 77 | # Normalize the case of a pathname and map slashes to backslashes. |
| 78 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 79 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 81 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 82 | """Normalize case of pathname. |
| 83 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 84 | Makes all characters lowercase and all slashes into backslashes.""" |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 85 | if not isinstance(s, (bytes, str)): |
| 86 | raise TypeError("normcase() argument must be str or bytes, " |
| 87 | "not '{}'".format(s.__class__.__name__)) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 88 | return s.replace(_get_altsep(s), _get_sep(s)).lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 90 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 91 | # Return whether a path is absolute. |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 92 | # Trivial in Posix, harder on Windows. |
| 93 | # For Windows it is absolute if it starts with a slash or backslash (current |
| 94 | # volume), or if a pathname after the volume-letter-and-colon or UNC-resource |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 95 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 96 | |
| 97 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 98 | """Test whether a path is absolute""" |
| 99 | s = splitdrive(s)[1] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 100 | return len(s) > 0 and s[:1] in _get_bothseps(s) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 101 | |
| 102 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 103 | # Join two (or more) paths. |
| 104 | |
Barry Warsaw | 384d249 | 1997-02-18 21:53:25 +0000 | [diff] [blame] | 105 | def join(a, *p): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 106 | """Join two or more pathname components, inserting "\\" as needed. |
| 107 | If any component is an absolute path, all previous path components |
| 108 | will be discarded.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 109 | sep = _get_sep(a) |
| 110 | seps = _get_bothseps(a) |
| 111 | colon = _get_colon(a) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 112 | path = a |
| 113 | for b in p: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 114 | b_wins = 0 # set to 1 iff b makes path irrelevant |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 115 | if not path: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 116 | b_wins = 1 |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 117 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 118 | elif isabs(b): |
| 119 | # This probably wipes out path so far. However, it's more |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 120 | # complicated if path begins with a drive letter. You get a+b |
| 121 | # (minus redundant slashes) in these four cases: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 122 | # 1. join('c:', '/a') == 'c:/a' |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 123 | # 2. join('//computer/share', '/a') == '//computer/share/a' |
| 124 | # 3. join('c:/', '/a') == 'c:/a' |
| 125 | # 4. join('//computer/share/', '/a') == '//computer/share/a' |
| 126 | # But b wins in all of these cases: |
| 127 | # 5. join('c:/a', '/b') == '/b' |
| 128 | # 6. join('//computer/share/a', '/b') == '/b' |
| 129 | # 7. join('c:', 'd:/') == 'd:/' |
| 130 | # 8. join('c:', '//computer/share/') == '//computer/share/' |
| 131 | # 9. join('//computer/share', 'd:/') == 'd:/' |
| 132 | # 10. join('//computer/share', '//computer/share/') == '//computer/share/' |
| 133 | # 11. join('c:/', 'd:/') == 'd:/' |
| 134 | # 12. join('c:/', '//computer/share/') == '//computer/share/' |
| 135 | # 13. join('//computer/share/', 'd:/') == 'd:/' |
| 136 | # 14. join('//computer/share/', '//computer/share/') == '//computer/share/' |
| 137 | b_prefix, b_rest = splitdrive(b) |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 138 | |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 139 | # if b has a prefix, it always wins. |
| 140 | if b_prefix: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 141 | b_wins = 1 |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 142 | else: |
| 143 | # b doesn't have a prefix. |
| 144 | # but isabs(b) returned true. |
| 145 | # and therefore b_rest[0] must be a slash. |
| 146 | # (but let's check that.) |
| 147 | assert(b_rest and b_rest[0] in seps) |
| 148 | |
| 149 | # so, b still wins if path has a rest that's more than a sep. |
| 150 | # you get a+b if path_rest is empty or only has a sep. |
| 151 | # (see cases 1-4 for times when b loses.) |
| 152 | path_rest = splitdrive(path)[1] |
| 153 | b_wins = path_rest and path_rest not in seps |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 154 | |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 155 | if b_wins: |
| 156 | path = b |
| 157 | else: |
| 158 | # Join, and ensure there's a separator. |
| 159 | assert len(path) > 0 |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 160 | if path[-1:] in seps: |
| 161 | if b and b[:1] in seps: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 162 | path += b[1:] |
| 163 | else: |
| 164 | path += b |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 165 | elif path[-1:] == colon: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 166 | path += b |
| 167 | elif b: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 168 | if b[:1] in seps: |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 169 | path += b |
| 170 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 171 | path += sep + b |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 172 | else: |
| 173 | # path is not empty and does not end with a backslash, |
| 174 | # but b is empty; since, e.g., split('a/') produces |
| 175 | # ('a', ''), it's best if join() adds a backslash in |
| 176 | # this case. |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 177 | path += sep |
Tim Peters | 1bdd0f2 | 2001-07-19 17:18:18 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 179 | return path |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | # 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] | 183 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 184 | # It is always true that drivespec + pathspec == p |
| 185 | def splitdrive(p): |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 186 | """Split a pathname into drive/UNC sharepoint and relative path specifiers. |
| 187 | Returns a 2-tuple (drive_or_unc, path); either part may be empty. |
| 188 | |
| 189 | If you assign |
| 190 | result = splitdrive(p) |
| 191 | It is always true that: |
| 192 | result[0] + result[1] == p |
| 193 | |
| 194 | If the path contained a drive letter, drive_or_unc will contain everything |
| 195 | up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir") |
| 196 | |
| 197 | If the path contained a UNC path, the drive_or_unc will contain the host name |
| 198 | and share up to but not including the fourth directory separator character. |
| 199 | e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir") |
| 200 | |
| 201 | Paths cannot contain both a drive letter and a UNC path. |
| 202 | |
| 203 | """ |
| 204 | empty = _get_empty(p) |
| 205 | if len(p) > 1: |
| 206 | sep = _get_sep(p) |
| 207 | normp = normcase(p) |
| 208 | if (normp[0:2] == sep*2) and (normp[2:3] != sep): |
| 209 | # is a UNC path: |
| 210 | # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path |
| 211 | # \\machine\mountpoint\directory\etc\... |
| 212 | # directory ^^^^^^^^^^^^^^^ |
| 213 | index = normp.find(sep, 2) |
| 214 | if index == -1: |
| 215 | return empty, p |
| 216 | index2 = normp.find(sep, index + 1) |
| 217 | # a UNC path can't have two slashes in a row |
| 218 | # (after the initial two) |
| 219 | if index2 == index + 1: |
| 220 | return empty, p |
| 221 | if index2 == -1: |
| 222 | index2 = len(p) |
| 223 | return p[:index2], p[index2:] |
| 224 | if normp[1:2] == _get_colon(p): |
| 225 | return p[:2], p[2:] |
| 226 | return empty, p |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | # Parse UNC paths |
| 230 | def splitunc(p): |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 231 | """Deprecated since Python 3.1. Please use splitdrive() instead; |
| 232 | it now handles UNC paths. |
| 233 | |
| 234 | Split a pathname into UNC mount point and relative path specifiers. |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 235 | |
| 236 | Return a 2-tuple (unc, rest); either part may be empty. |
| 237 | If unc is not empty, it has the form '//host/mount' (or similar |
| 238 | using backslashes). unc+rest is always the input path. |
| 239 | Paths containing drive letters never have an UNC part. |
| 240 | """ |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 241 | import warnings |
| 242 | warnings.warn("ntpath.splitunc is deprecated, use ntpath.splitdrive instead", |
Gregory P. Smith | aa3b5b8 | 2009-06-30 05:33:50 +0000 | [diff] [blame] | 243 | DeprecationWarning) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 244 | sep = _get_sep(p) |
| 245 | if not p[1:2]: |
| 246 | return p[:0], p # Drive letter present |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 247 | firstTwo = p[0:2] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 248 | if normcase(firstTwo) == sep + sep: |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 249 | # is a UNC path: |
| 250 | # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter |
| 251 | # \\machine\mountpoint\directories... |
| 252 | # directory ^^^^^^^^^^^^^^^ |
| 253 | normp = normcase(p) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 254 | index = normp.find(sep, 2) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 255 | if index == -1: |
| 256 | ##raise RuntimeError, 'illegal UNC path: "' + p + '"' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 257 | return (p[:0], p) |
| 258 | index = normp.find(sep, index + 1) |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 259 | if index == -1: |
| 260 | index = len(p) |
| 261 | return p[:index], p[index:] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 262 | return p[:0], p |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 263 | |
| 264 | |
| 265 | # 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] | 266 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 267 | # join(head, tail) == p holds. |
| 268 | # The resulting head won't end in '/' unless it is the root. |
| 269 | |
| 270 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 271 | """Split a pathname. |
| 272 | |
| 273 | Return tuple (head, tail) where tail is everything after the final slash. |
| 274 | Either part may be empty.""" |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 275 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 276 | seps = _get_bothseps(p) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 277 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 278 | # set i to index beyond p's last slash |
| 279 | i = len(p) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 280 | while i and p[i-1] not in seps: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 281 | i -= 1 |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 282 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 283 | # remove trailing slashes from head, unless it's all slashes |
| 284 | head2 = head |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 285 | while head2 and head2[-1:] in seps: |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 286 | head2 = head2[:-1] |
| 287 | head = head2 or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 288 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 292 | # 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] | 293 | # pathname component; the root is everything before that. |
| 294 | # It is always true that root + ext == p. |
| 295 | |
| 296 | def splitext(p): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 297 | return genericpath._splitext(p, _get_sep(p), _get_altsep(p), |
| 298 | _get_dot(p)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 299 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 300 | |
| 301 | |
| 302 | # Return the tail (basename) part of a path. |
| 303 | |
| 304 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 305 | """Returns the final component of a pathname""" |
| 306 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 307 | |
| 308 | |
| 309 | # Return the head (dirname) part of a path. |
| 310 | |
| 311 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 312 | """Returns the directory component of a pathname""" |
| 313 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 315 | # Is a path a symbolic link? |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 316 | # This will always return false on systems where os.lstat doesn't exist. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 317 | |
| 318 | def islink(path): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 319 | """Test whether a path is a symbolic link. |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 320 | This will always return false for Windows prior to 6.0. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 321 | """ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 322 | try: |
| 323 | st = os.lstat(path) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 324 | except (OSError, AttributeError): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 325 | return False |
| 326 | return stat.S_ISLNK(st.st_mode) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 327 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 328 | # Being true for dangling symbolic links is also useful. |
| 329 | |
| 330 | def lexists(path): |
| 331 | """Test whether a path exists. Returns True for broken symbolic links""" |
| 332 | try: |
| 333 | st = os.lstat(path) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 334 | except OSError: |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 335 | return False |
| 336 | return True |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 337 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 338 | # Is a path a mount point? |
| 339 | # Any drive letter root (eg c:\) |
| 340 | # Any share UNC (eg \\server\share) |
| 341 | # Any volume mounted on a filesystem folder |
| 342 | # |
| 343 | # No one method detects all three situations. Historically we've lexically |
| 344 | # detected drive letter roots and share UNCs. The canonical approach to |
| 345 | # detecting mounted volumes (querying the reparse tag) fails for the most |
| 346 | # common case: drive letter roots. The alternative which uses GetVolumePathName |
| 347 | # fails if the drive letter is the result of a SUBST. |
| 348 | try: |
| 349 | from nt import _getvolumepathname |
| 350 | except ImportError: |
| 351 | _getvolumepathname = None |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 352 | def ismount(path): |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 353 | """Test whether a path is a mount point (a drive root, the root of a |
| 354 | share, or a mounted volume)""" |
Benjamin Peterson | 48e2478 | 2009-03-29 13:02:52 +0000 | [diff] [blame] | 355 | seps = _get_bothseps(path) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 356 | path = abspath(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 357 | root, rest = splitdrive(path) |
| 358 | if root and root[0] in seps: |
| 359 | return (not rest) or (rest in seps) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 360 | if rest in seps: |
| 361 | return True |
| 362 | |
| 363 | if _getvolumepathname: |
| 364 | return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps) |
| 365 | else: |
| 366 | return False |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 367 | |
| 368 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 369 | # Expand paths beginning with '~' or '~user'. |
| 370 | # '~' means $HOME; '~user' means that user's home directory. |
| 371 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 372 | # the path is returned unchanged (leaving error reporting to whatever |
| 373 | # function is called with the expanded path as argument). |
| 374 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 375 | # (A function should also be defined to do full *sh-style environment |
| 376 | # variable expansion.) |
| 377 | |
| 378 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 379 | """Expand ~ and ~user constructs. |
| 380 | |
| 381 | If user or $HOME is unknown, do nothing.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 382 | if isinstance(path, bytes): |
| 383 | tilde = b'~' |
| 384 | else: |
| 385 | tilde = '~' |
| 386 | if not path.startswith(tilde): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 387 | return path |
| 388 | i, n = 1, len(path) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 389 | while i < n and path[i] not in _get_bothseps(path): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 390 | i += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 391 | |
| 392 | if 'HOME' in os.environ: |
| 393 | userhome = os.environ['HOME'] |
| 394 | elif 'USERPROFILE' in os.environ: |
| 395 | userhome = os.environ['USERPROFILE'] |
| 396 | elif not 'HOMEPATH' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 397 | return path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 398 | else: |
| 399 | try: |
| 400 | drive = os.environ['HOMEDRIVE'] |
| 401 | except KeyError: |
| 402 | drive = '' |
| 403 | userhome = join(drive, os.environ['HOMEPATH']) |
| 404 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 405 | if isinstance(path, bytes): |
| 406 | userhome = userhome.encode(sys.getfilesystemencoding()) |
| 407 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 408 | if i != 1: #~user |
| 409 | userhome = join(dirname(userhome), path[1:i]) |
| 410 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 411 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 412 | |
| 413 | |
| 414 | # Expand paths containing shell variable substitutions. |
| 415 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 416 | # - no expansion within single quotes |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 417 | # - '$$' is translated into '$' |
| 418 | # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 419 | # - ${varname} is accepted. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 420 | # - $varname is accepted. |
| 421 | # - %varname% is accepted. |
| 422 | # - varnames can be made out of letters, digits and the characters '_-' |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 423 | # (though is not verified in the ${varname} and %varname% cases) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 424 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 425 | # XXX except '^|<>='. |
| 426 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 427 | def expandvars(path): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 428 | """Expand shell variables of the forms $var, ${var} and %var%. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 429 | |
| 430 | Unknown variables are left unchanged.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 431 | if isinstance(path, bytes): |
| 432 | if ord('$') not in path and ord('%') not in path: |
| 433 | return path |
| 434 | import string |
| 435 | varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii') |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 436 | quote = b'\'' |
| 437 | percent = b'%' |
| 438 | brace = b'{' |
| 439 | dollar = b'$' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 440 | else: |
| 441 | if '$' not in path and '%' not in path: |
| 442 | return path |
| 443 | import string |
| 444 | varchars = string.ascii_letters + string.digits + '_-' |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 445 | quote = '\'' |
| 446 | percent = '%' |
| 447 | brace = '{' |
| 448 | dollar = '$' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 449 | res = path[:0] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 450 | index = 0 |
| 451 | pathlen = len(path) |
| 452 | while index < pathlen: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 453 | c = path[index:index+1] |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 454 | if c == quote: # no expansion within single quotes |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 455 | path = path[index + 1:] |
| 456 | pathlen = len(path) |
| 457 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 458 | index = path.index(c) |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 459 | res += c + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 460 | except ValueError: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 461 | res += path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 462 | index = pathlen - 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 463 | elif c == percent: # variable or '%' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 464 | if path[index + 1:index + 2] == percent: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 465 | res += c |
| 466 | index += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 467 | else: |
| 468 | path = path[index+1:] |
| 469 | pathlen = len(path) |
| 470 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 471 | index = path.index(percent) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 472 | except ValueError: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 473 | res += percent + path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 474 | index = pathlen - 1 |
| 475 | else: |
| 476 | var = path[:index] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 477 | if isinstance(path, bytes): |
| 478 | var = var.decode('ascii') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 479 | if var in os.environ: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 480 | value = os.environ[var] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 481 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 482 | value = '%' + var + '%' |
| 483 | if isinstance(path, bytes): |
| 484 | value = value.encode('ascii') |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 485 | res += value |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 486 | elif c == dollar: # variable or '$$' |
| 487 | if path[index + 1:index + 2] == dollar: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 488 | res += c |
| 489 | index += 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 490 | elif path[index + 1:index + 2] == brace: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 491 | path = path[index+2:] |
| 492 | pathlen = len(path) |
| 493 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 494 | if isinstance(path, bytes): |
| 495 | index = path.index(b'}') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 496 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 497 | index = path.index('}') |
| 498 | var = path[:index] |
| 499 | if isinstance(path, bytes): |
| 500 | var = var.decode('ascii') |
| 501 | if var in os.environ: |
| 502 | value = os.environ[var] |
| 503 | else: |
| 504 | value = '${' + var + '}' |
| 505 | if isinstance(path, bytes): |
| 506 | value = value.encode('ascii') |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 507 | res += value |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 508 | except ValueError: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 509 | if isinstance(path, bytes): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 510 | res += b'${' + path |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 511 | else: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 512 | res += '${' + path |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 513 | index = pathlen - 1 |
| 514 | else: |
| 515 | var = '' |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 516 | index += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 517 | c = path[index:index + 1] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 518 | while c and c in varchars: |
| 519 | if isinstance(path, bytes): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 520 | var += c.decode('ascii') |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 521 | else: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 522 | var += c |
| 523 | index += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 524 | c = path[index:index + 1] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 525 | if var in os.environ: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 526 | value = os.environ[var] |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 527 | else: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 528 | value = '$' + var |
| 529 | if isinstance(path, bytes): |
| 530 | value = value.encode('ascii') |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 531 | res += value |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 532 | if c: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 533 | index -= 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 534 | else: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 535 | res += c |
| 536 | index += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 537 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 538 | |
| 539 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 540 | # 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] | 541 | # Previously, this function also truncated pathnames to 8+3 format, |
| 542 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 543 | |
| 544 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 545 | """Normalize path, eliminating double slashes, etc.""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 546 | sep = _get_sep(path) |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 547 | dotdot = _get_dot(path) * 2 |
Georg Brandl | cfb6821 | 2010-07-31 21:40:15 +0000 | [diff] [blame] | 548 | special_prefixes = _get_special(path) |
| 549 | if path.startswith(special_prefixes): |
| 550 | # in the case of paths with these prefixes: |
| 551 | # \\.\ -> device names |
| 552 | # \\?\ -> literal paths |
| 553 | # do not do any normalization, but return the path unchanged |
| 554 | return path |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 555 | path = path.replace(_get_altsep(path), sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 556 | prefix, path = splitdrive(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 557 | |
| 558 | # collapse initial backslashes |
| 559 | if path.startswith(sep): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 560 | prefix += sep |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 561 | path = path.lstrip(sep) |
| 562 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 563 | comps = path.split(sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 564 | i = 0 |
| 565 | while i < len(comps): |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 566 | if not comps[i] or comps[i] == _get_dot(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 567 | del comps[i] |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 568 | elif comps[i] == dotdot: |
| 569 | if i > 0 and comps[i-1] != dotdot: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 570 | del comps[i-1:i+1] |
| 571 | i -= 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 572 | elif i == 0 and prefix.endswith(_get_sep(path)): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 573 | del comps[i] |
| 574 | else: |
| 575 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 576 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 577 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 578 | # If the path is now empty, substitute '.' |
| 579 | if not prefix and not comps: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 580 | comps.append(_get_dot(path)) |
| 581 | return prefix + sep.join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 582 | |
| 583 | |
| 584 | # Return an absolute path. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 585 | try: |
| 586 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 587 | |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 588 | except ImportError: # not running on Windows - mock up something sensible |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 589 | def abspath(path): |
| 590 | """Return the absolute version of a path.""" |
| 591 | if not isabs(path): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 592 | if isinstance(path, bytes): |
| 593 | cwd = os.getcwdb() |
| 594 | else: |
| 595 | cwd = os.getcwd() |
| 596 | path = join(cwd, path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 597 | return normpath(path) |
| 598 | |
| 599 | else: # use native Windows method on Windows |
| 600 | def abspath(path): |
| 601 | """Return the absolute version of a path.""" |
| 602 | |
| 603 | if path: # Empty path must return current working directory. |
| 604 | try: |
| 605 | path = _getfullpathname(path) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 606 | except OSError: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 607 | pass # Bad path - return unchanged. |
Florent Xicluna | ad8c5ca | 2010-03-08 14:44:41 +0000 | [diff] [blame] | 608 | elif isinstance(path, bytes): |
| 609 | path = os.getcwdb() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 610 | else: |
| 611 | path = os.getcwd() |
| 612 | return normpath(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 613 | |
| 614 | # realpath is a no-op on systems without islink support |
| 615 | realpath = abspath |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 616 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 617 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 618 | sys.getwindowsversion()[3] >= 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 619 | |
| 620 | def relpath(path, start=curdir): |
| 621 | """Return a relative version of a path""" |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 622 | sep = _get_sep(path) |
| 623 | |
| 624 | if start is curdir: |
| 625 | start = _get_dot(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 626 | |
| 627 | if not path: |
| 628 | raise ValueError("no path specified") |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 629 | |
| 630 | start_abs = abspath(normpath(start)) |
| 631 | path_abs = abspath(normpath(path)) |
| 632 | start_drive, start_rest = splitdrive(start_abs) |
| 633 | path_drive, path_rest = splitdrive(path_abs) |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 634 | if normcase(start_drive) != normcase(path_drive): |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 635 | error = "path is on mount '{0}', start on mount '{1}'".format( |
| 636 | path_drive, start_drive) |
| 637 | raise ValueError(error) |
| 638 | |
| 639 | start_list = [x for x in start_rest.split(sep) if x] |
| 640 | path_list = [x for x in path_rest.split(sep) if x] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 641 | # Work out how much of the filepath is shared by start and path. |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 642 | i = 0 |
| 643 | for e1, e2 in zip(start_list, path_list): |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 644 | if normcase(e1) != normcase(e2): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 645 | break |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 646 | i += 1 |
| 647 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 648 | if isinstance(path, bytes): |
| 649 | pardir = b'..' |
| 650 | else: |
| 651 | pardir = '..' |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 652 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 653 | if not rel_list: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 654 | return _get_dot(path) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 655 | return join(*rel_list) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 656 | |
| 657 | |
| 658 | # determine if two files are in fact the same file |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 659 | try: |
Brian Curtin | e8e8042 | 2010-09-24 13:56:34 +0000 | [diff] [blame] | 660 | # GetFinalPathNameByHandle is available starting with Windows 6.0. |
| 661 | # Windows XP and non-Windows OS'es will mock _getfinalpathname. |
| 662 | if sys.getwindowsversion()[:2] >= (6, 0): |
| 663 | from nt import _getfinalpathname |
| 664 | else: |
| 665 | raise ImportError |
| 666 | except (AttributeError, ImportError): |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 667 | # On Windows XP and earlier, two files are the same if their absolute |
| 668 | # pathnames are the same. |
Brian Curtin | e8e8042 | 2010-09-24 13:56:34 +0000 | [diff] [blame] | 669 | # Non-Windows operating systems fake this method with an XP |
| 670 | # approximation. |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 671 | def _getfinalpathname(f): |
Ronald Oussoren | 6355c16 | 2011-05-06 17:11:07 +0200 | [diff] [blame] | 672 | return normcase(abspath(f)) |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 673 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 674 | |
| 675 | try: |
| 676 | # The genericpath.isdir implementation uses os.stat and checks the mode |
| 677 | # attribute to tell whether or not the path is a directory. |
| 678 | # This is overkill on Windows - just pass the path to GetFileAttributes |
| 679 | # and check the attribute from there. |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 680 | from nt import _isdir as isdir |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 681 | except ImportError: |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 682 | # Use genericpath.isdir as imported above. |
| 683 | pass |