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