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