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