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