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 | |
Serhiy Storchaka | 3460198 | 2018-01-07 17:54:31 +0200 | [diff] [blame] | 8 | # strings representing various path-related bits and pieces |
| 9 | # These are primarily for export; internally, they are hardcoded. |
| 10 | # Should be set before imports for resolving cyclic dependency. |
| 11 | curdir = '.' |
| 12 | pardir = '..' |
| 13 | extsep = '.' |
| 14 | sep = '\\' |
| 15 | pathsep = ';' |
| 16 | altsep = '/' |
| 17 | defpath = '.;C:\\bin' |
| 18 | devnull = 'nul' |
| 19 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 20 | import os |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 21 | import sys |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 22 | import stat |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | import genericpath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 24 | from genericpath import * |
Skip Montanaro | 4d5d5bf | 2000-07-13 01:01:03 +0000 | [diff] [blame] | 25 | |
Skip Montanaro | 269b83b | 2001-02-06 01:07:02 +0000 | [diff] [blame] | 26 | __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
| 27 | "basename","dirname","commonprefix","getsize","getmtime", |
Georg Brandl | f0de6a1 | 2005-08-22 18:02:59 +0000 | [diff] [blame] | 28 | "getatime","getctime", "islink","exists","lexists","isdir","isfile", |
Benjamin Peterson | d71ca41 | 2008-05-08 23:44:58 +0000 | [diff] [blame] | 29 | "ismount", "expanduser","expandvars","normpath","abspath", |
Serhiy Storchaka | 9ed707e | 2017-01-13 20:55:05 +0200 | [diff] [blame] | 30 | "curdir","pardir","sep","pathsep","defpath","altsep", |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 31 | "extsep","devnull","realpath","supports_unicode_filenames","relpath", |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 32 | "samefile", "sameopenfile", "samestat", "commonpath"] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 33 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 34 | def _get_bothseps(path): |
| 35 | if isinstance(path, bytes): |
| 36 | return b'\\/' |
| 37 | else: |
| 38 | return '\\/' |
| 39 | |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 40 | # Normalize the case of a pathname and map slashes to backslashes. |
| 41 | # Other normalizations (such as optimizing '../' away) are not done |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 42 | # (this is done by normpath). |
Guido van Rossum | e2ad88c | 1997-08-12 14:46:58 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 44 | def normcase(s): |
Guido van Rossum | 16a0bc2 | 1998-02-18 13:48:31 +0000 | [diff] [blame] | 45 | """Normalize case of pathname. |
| 46 | |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 47 | Makes all characters lowercase and all slashes into backslashes.""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 48 | s = os.fspath(s) |
Wolfgang Maier | 74510e2 | 2019-03-28 22:47:18 +0100 | [diff] [blame] | 49 | if isinstance(s, bytes): |
| 50 | return s.replace(b'/', b'\\').lower() |
| 51 | else: |
| 52 | return s.replace('/', '\\').lower() |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 54 | |
Fred Drake | ef0b5dd | 2000-02-17 17:30:40 +0000 | [diff] [blame] | 55 | # Return whether a path is absolute. |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 56 | # Trivial in Posix, harder on Windows. |
| 57 | # For Windows it is absolute if it starts with a slash or backslash (current |
| 58 | # 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] | 59 | # starts with a slash or backslash. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 60 | |
| 61 | def isabs(s): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 62 | """Test whether a path is absolute""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 63 | s = os.fspath(s) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 64 | s = splitdrive(s)[1] |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 65 | return len(s) > 0 and s[0] in _get_bothseps(s) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 66 | |
| 67 | |
Guido van Rossum | 77e1db3 | 1997-06-02 23:11:57 +0000 | [diff] [blame] | 68 | # Join two (or more) paths. |
Serhiy Storchaka | c369c2c | 2014-01-27 23:15:14 +0200 | [diff] [blame] | 69 | def join(path, *paths): |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 70 | path = os.fspath(path) |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 71 | if isinstance(path, bytes): |
| 72 | sep = b'\\' |
| 73 | seps = b'\\/' |
| 74 | colon = b':' |
| 75 | else: |
| 76 | sep = '\\' |
| 77 | seps = '\\/' |
| 78 | colon = ':' |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 79 | try: |
Serhiy Storchaka | 5bfc03f | 2015-05-19 11:00:07 +0300 | [diff] [blame] | 80 | if not paths: |
| 81 | path[:0] + sep #23780: Ensure compatible data type even if p is null. |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 82 | result_drive, result_path = splitdrive(path) |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 83 | for p in map(os.fspath, paths): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 84 | p_drive, p_path = splitdrive(p) |
| 85 | if p_path and p_path[0] in seps: |
| 86 | # Second path is absolute |
| 87 | if p_drive or not result_drive: |
| 88 | result_drive = p_drive |
Serhiy Storchaka | c369c2c | 2014-01-27 23:15:14 +0200 | [diff] [blame] | 89 | result_path = p_path |
| 90 | continue |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 91 | elif p_drive and p_drive != result_drive: |
| 92 | if p_drive.lower() != result_drive.lower(): |
| 93 | # Different drives => ignore the first path entirely |
| 94 | result_drive = p_drive |
| 95 | result_path = p_path |
| 96 | continue |
| 97 | # Same drive in different case |
| 98 | result_drive = p_drive |
| 99 | # Second path is relative to the first |
| 100 | if result_path and result_path[-1] not in seps: |
| 101 | result_path = result_path + sep |
| 102 | result_path = result_path + p_path |
| 103 | ## add separator between UNC and non-absolute path |
| 104 | if (result_path and result_path[0] not in seps and |
| 105 | result_drive and result_drive[-1:] != colon): |
| 106 | return result_drive + sep + result_path |
| 107 | return result_drive + result_path |
| 108 | except (TypeError, AttributeError, BytesWarning): |
| 109 | genericpath._check_arg_types('join', path, *paths) |
| 110 | raise |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | # 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] | 114 | # colon) and the path specification. |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 115 | # It is always true that drivespec + pathspec == p |
| 116 | def splitdrive(p): |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 117 | """Split a pathname into drive/UNC sharepoint and relative path specifiers. |
| 118 | Returns a 2-tuple (drive_or_unc, path); either part may be empty. |
| 119 | |
| 120 | If you assign |
| 121 | result = splitdrive(p) |
| 122 | It is always true that: |
| 123 | result[0] + result[1] == p |
| 124 | |
| 125 | If the path contained a drive letter, drive_or_unc will contain everything |
| 126 | up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir") |
| 127 | |
| 128 | If the path contained a UNC path, the drive_or_unc will contain the host name |
| 129 | and share up to but not including the fourth directory separator character. |
| 130 | e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir") |
| 131 | |
| 132 | Paths cannot contain both a drive letter and a UNC path. |
| 133 | |
| 134 | """ |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 135 | p = os.fspath(p) |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 136 | if len(p) >= 2: |
| 137 | if isinstance(p, bytes): |
| 138 | sep = b'\\' |
| 139 | altsep = b'/' |
| 140 | colon = b':' |
| 141 | else: |
| 142 | sep = '\\' |
| 143 | altsep = '/' |
| 144 | colon = ':' |
| 145 | normp = p.replace(altsep, sep) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 146 | if (normp[0:2] == sep*2) and (normp[2:3] != sep): |
| 147 | # is a UNC path: |
| 148 | # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path |
| 149 | # \\machine\mountpoint\directory\etc\... |
| 150 | # directory ^^^^^^^^^^^^^^^ |
| 151 | index = normp.find(sep, 2) |
| 152 | if index == -1: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 153 | return p[:0], p |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 154 | index2 = normp.find(sep, index + 1) |
| 155 | # a UNC path can't have two slashes in a row |
| 156 | # (after the initial two) |
| 157 | if index2 == index + 1: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 158 | return p[:0], p |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 159 | if index2 == -1: |
| 160 | index2 = len(p) |
| 161 | return p[:index2], p[index2:] |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 162 | if normp[1:2] == colon: |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 163 | return p[:2], p[2:] |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 164 | return p[:0], p |
Guido van Rossum | f3c695c | 1999-04-06 19:32:19 +0000 | [diff] [blame] | 165 | |
| 166 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 167 | # 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] | 168 | # rest). After the trailing '/' is stripped, the invariant |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 169 | # join(head, tail) == p holds. |
| 170 | # The resulting head won't end in '/' unless it is the root. |
| 171 | |
| 172 | def split(p): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 173 | """Split a pathname. |
| 174 | |
| 175 | Return tuple (head, tail) where tail is everything after the final slash. |
| 176 | Either part may be empty.""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 177 | p = os.fspath(p) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 178 | seps = _get_bothseps(p) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 179 | d, p = splitdrive(p) |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 180 | # set i to index beyond p's last slash |
| 181 | i = len(p) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 182 | while i and p[i-1] not in seps: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 183 | i -= 1 |
Guido van Rossum | 8f0fa9e | 1999-03-19 21:05:12 +0000 | [diff] [blame] | 184 | head, tail = p[:i], p[i:] # now tail has no slashes |
| 185 | # remove trailing slashes from head, unless it's all slashes |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 186 | head = head.rstrip(seps) or head |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 187 | return d + head, tail |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | # Split a path in root and extension. |
Guido van Rossum | 73e122f | 1997-01-22 00:17:26 +0000 | [diff] [blame] | 191 | # 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] | 192 | # pathname component; the root is everything before that. |
| 193 | # It is always true that root + ext == p. |
| 194 | |
| 195 | def splitext(p): |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 196 | p = os.fspath(p) |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 197 | if isinstance(p, bytes): |
| 198 | return genericpath._splitext(p, b'\\', b'/', b'.') |
| 199 | else: |
| 200 | return genericpath._splitext(p, '\\', '/', '.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 201 | splitext.__doc__ = genericpath._splitext.__doc__ |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | # Return the tail (basename) part of a path. |
| 205 | |
| 206 | def basename(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 207 | """Returns the final component of a pathname""" |
| 208 | return split(p)[1] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 209 | |
| 210 | |
| 211 | # Return the head (dirname) part of a path. |
| 212 | |
| 213 | def dirname(p): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 214 | """Returns the directory component of a pathname""" |
| 215 | return split(p)[0] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 216 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 217 | # Is a path a symbolic link? |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 218 | # 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] | 219 | |
| 220 | def islink(path): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 221 | """Test whether a path is a symbolic link. |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 222 | This will always return false for Windows prior to 6.0. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 223 | """ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 224 | try: |
| 225 | st = os.lstat(path) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 226 | except (OSError, ValueError, AttributeError): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 227 | return False |
| 228 | return stat.S_ISLNK(st.st_mode) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 229 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 230 | # Being true for dangling symbolic links is also useful. |
| 231 | |
| 232 | def lexists(path): |
| 233 | """Test whether a path exists. Returns True for broken symbolic links""" |
| 234 | try: |
| 235 | st = os.lstat(path) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 236 | except (OSError, ValueError): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 237 | return False |
| 238 | return True |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 239 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 240 | # Is a path a mount point? |
| 241 | # Any drive letter root (eg c:\) |
| 242 | # Any share UNC (eg \\server\share) |
| 243 | # Any volume mounted on a filesystem folder |
| 244 | # |
| 245 | # No one method detects all three situations. Historically we've lexically |
| 246 | # detected drive letter roots and share UNCs. The canonical approach to |
| 247 | # detecting mounted volumes (querying the reparse tag) fails for the most |
| 248 | # common case: drive letter roots. The alternative which uses GetVolumePathName |
| 249 | # fails if the drive letter is the result of a SUBST. |
| 250 | try: |
| 251 | from nt import _getvolumepathname |
| 252 | except ImportError: |
| 253 | _getvolumepathname = None |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 254 | def ismount(path): |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 255 | """Test whether a path is a mount point (a drive root, the root of a |
| 256 | share, or a mounted volume)""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 257 | path = os.fspath(path) |
Benjamin Peterson | 48e2478 | 2009-03-29 13:02:52 +0000 | [diff] [blame] | 258 | seps = _get_bothseps(path) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 259 | path = abspath(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 260 | root, rest = splitdrive(path) |
| 261 | if root and root[0] in seps: |
| 262 | return (not rest) or (rest in seps) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 263 | if rest in seps: |
| 264 | return True |
| 265 | |
| 266 | if _getvolumepathname: |
| 267 | return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps) |
| 268 | else: |
| 269 | return False |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 270 | |
| 271 | |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 272 | # Expand paths beginning with '~' or '~user'. |
| 273 | # '~' means $HOME; '~user' means that user's home directory. |
| 274 | # If the path doesn't begin with '~', or if the user or $HOME is unknown, |
| 275 | # the path is returned unchanged (leaving error reporting to whatever |
| 276 | # function is called with the expanded path as argument). |
| 277 | # See also module 'glob' for expansion of *, ? and [...] in pathnames. |
| 278 | # (A function should also be defined to do full *sh-style environment |
| 279 | # variable expansion.) |
| 280 | |
| 281 | def expanduser(path): |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 282 | """Expand ~ and ~user constructs. |
| 283 | |
| 284 | If user or $HOME is unknown, do nothing.""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 285 | path = os.fspath(path) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 286 | if isinstance(path, bytes): |
| 287 | tilde = b'~' |
| 288 | else: |
| 289 | tilde = '~' |
| 290 | if not path.startswith(tilde): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 291 | return path |
| 292 | i, n = 1, len(path) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 293 | while i < n and path[i] not in _get_bothseps(path): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 294 | i += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 295 | |
Anthony Sottile | 25ec4a4 | 2019-03-12 08:39:57 -0700 | [diff] [blame] | 296 | if 'USERPROFILE' in os.environ: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 297 | userhome = os.environ['USERPROFILE'] |
| 298 | elif not 'HOMEPATH' in os.environ: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 299 | return path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 300 | else: |
| 301 | try: |
| 302 | drive = os.environ['HOMEDRIVE'] |
| 303 | except KeyError: |
| 304 | drive = '' |
| 305 | userhome = join(drive, os.environ['HOMEPATH']) |
| 306 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 307 | if isinstance(path, bytes): |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 308 | userhome = os.fsencode(userhome) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 309 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 310 | if i != 1: #~user |
| 311 | userhome = join(dirname(userhome), path[1:i]) |
| 312 | |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 313 | return userhome + path[i:] |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 314 | |
| 315 | |
| 316 | # Expand paths containing shell variable substitutions. |
| 317 | # The following rules apply: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 318 | # - no expansion within single quotes |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 319 | # - '$$' is translated into '$' |
| 320 | # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2% |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 321 | # - ${varname} is accepted. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 322 | # - $varname is accepted. |
| 323 | # - %varname% is accepted. |
| 324 | # - varnames can be made out of letters, digits and the characters '_-' |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 325 | # (though is not verified in the ${varname} and %varname% cases) |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 326 | # XXX With COMMAND.COM you can use any characters in a variable name, |
| 327 | # XXX except '^|<>='. |
| 328 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 329 | def expandvars(path): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 330 | """Expand shell variables of the forms $var, ${var} and %var%. |
Guido van Rossum | 534972b | 1999-02-03 17:20:50 +0000 | [diff] [blame] | 331 | |
| 332 | Unknown variables are left unchanged.""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 333 | path = os.fspath(path) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 334 | if isinstance(path, bytes): |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 335 | if b'$' not in path and b'%' not in path: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 336 | return path |
| 337 | import string |
| 338 | varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii') |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 339 | quote = b'\'' |
| 340 | percent = b'%' |
| 341 | brace = b'{' |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 342 | rbrace = b'}' |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 343 | dollar = b'$' |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 344 | environ = getattr(os, 'environb', None) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 345 | else: |
| 346 | if '$' not in path and '%' not in path: |
| 347 | return path |
| 348 | import string |
| 349 | varchars = string.ascii_letters + string.digits + '_-' |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 350 | quote = '\'' |
| 351 | percent = '%' |
| 352 | brace = '{' |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 353 | rbrace = '}' |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 354 | dollar = '$' |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 355 | environ = os.environ |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 356 | res = path[:0] |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 357 | index = 0 |
| 358 | pathlen = len(path) |
| 359 | while index < pathlen: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 360 | c = path[index:index+1] |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 361 | if c == quote: # no expansion within single quotes |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 362 | path = path[index + 1:] |
| 363 | pathlen = len(path) |
| 364 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 365 | index = path.index(c) |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 366 | res += c + path[:index + 1] |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 367 | except ValueError: |
Serhiy Storchaka | 1b87ae0 | 2015-03-25 16:40:15 +0200 | [diff] [blame] | 368 | res += c + path |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 369 | index = pathlen - 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 370 | elif c == percent: # variable or '%' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 371 | if path[index + 1:index + 2] == percent: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 372 | res += c |
| 373 | index += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 374 | else: |
| 375 | path = path[index+1:] |
| 376 | pathlen = len(path) |
| 377 | try: |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 378 | index = path.index(percent) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 379 | except ValueError: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 380 | res += percent + path |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 381 | index = pathlen - 1 |
| 382 | else: |
| 383 | var = path[:index] |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 384 | try: |
| 385 | if environ is None: |
| 386 | value = os.fsencode(os.environ[os.fsdecode(var)]) |
| 387 | else: |
| 388 | value = environ[var] |
| 389 | except KeyError: |
| 390 | value = percent + var + percent |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 391 | res += value |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 392 | elif c == dollar: # variable or '$$' |
| 393 | if path[index + 1:index + 2] == dollar: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 394 | res += c |
| 395 | index += 1 |
Amaury Forgeot d'Arc | 3b44e61 | 2008-10-03 20:32:33 +0000 | [diff] [blame] | 396 | elif path[index + 1:index + 2] == brace: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 397 | path = path[index+2:] |
| 398 | pathlen = len(path) |
| 399 | try: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 400 | index = path.index(rbrace) |
Fred Drake | b4e460a | 2000-09-28 16:25:20 +0000 | [diff] [blame] | 401 | except ValueError: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 402 | res += dollar + brace + path |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 403 | index = pathlen - 1 |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 404 | else: |
| 405 | var = path[:index] |
| 406 | try: |
| 407 | if environ is None: |
| 408 | value = os.fsencode(os.environ[os.fsdecode(var)]) |
| 409 | else: |
| 410 | value = environ[var] |
| 411 | except KeyError: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 412 | value = dollar + brace + var + rbrace |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 413 | res += value |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 414 | else: |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 415 | var = path[:0] |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 416 | index += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 417 | c = path[index:index + 1] |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 418 | while c and c in varchars: |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 419 | var += c |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 420 | index += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 421 | c = path[index:index + 1] |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 422 | try: |
| 423 | if environ is None: |
| 424 | value = os.fsencode(os.environ[os.fsdecode(var)]) |
| 425 | else: |
| 426 | value = environ[var] |
| 427 | except KeyError: |
| 428 | value = dollar + var |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 429 | res += value |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 430 | if c: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 431 | index -= 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 432 | else: |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 433 | res += c |
| 434 | index += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 435 | return res |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 436 | |
| 437 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 438 | # 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] | 439 | # Previously, this function also truncated pathnames to 8+3 format, |
| 440 | # but as this module is called "ntpath", that's obviously wrong! |
Guido van Rossum | 555915a | 1994-02-24 11:32:59 +0000 | [diff] [blame] | 441 | |
| 442 | def normpath(path): |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 443 | """Normalize path, eliminating double slashes, etc.""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 444 | path = os.fspath(path) |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 445 | if isinstance(path, bytes): |
| 446 | sep = b'\\' |
| 447 | altsep = b'/' |
| 448 | curdir = b'.' |
| 449 | pardir = b'..' |
| 450 | special_prefixes = (b'\\\\.\\', b'\\\\?\\') |
| 451 | else: |
| 452 | sep = '\\' |
| 453 | altsep = '/' |
| 454 | curdir = '.' |
| 455 | pardir = '..' |
| 456 | special_prefixes = ('\\\\.\\', '\\\\?\\') |
Georg Brandl | cfb6821 | 2010-07-31 21:40:15 +0000 | [diff] [blame] | 457 | if path.startswith(special_prefixes): |
| 458 | # in the case of paths with these prefixes: |
| 459 | # \\.\ -> device names |
| 460 | # \\?\ -> literal paths |
Steve Dower | a50d2f7 | 2019-08-21 17:23:38 -0700 | [diff] [blame^] | 461 | # do not do any normalization, but return the path |
| 462 | # unchanged apart from the call to os.fspath() |
Georg Brandl | cfb6821 | 2010-07-31 21:40:15 +0000 | [diff] [blame] | 463 | return path |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 464 | path = path.replace(altsep, sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 465 | prefix, path = splitdrive(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 466 | |
| 467 | # collapse initial backslashes |
| 468 | if path.startswith(sep): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 469 | prefix += sep |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 470 | path = path.lstrip(sep) |
| 471 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 472 | comps = path.split(sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 473 | i = 0 |
| 474 | while i < len(comps): |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 475 | if not comps[i] or comps[i] == curdir: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 476 | del comps[i] |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 477 | elif comps[i] == pardir: |
| 478 | if i > 0 and comps[i-1] != pardir: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 479 | del comps[i-1:i+1] |
| 480 | i -= 1 |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 481 | elif i == 0 and prefix.endswith(sep): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 482 | del comps[i] |
| 483 | else: |
| 484 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 485 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 486 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 487 | # If the path is now empty, substitute '.' |
| 488 | if not prefix and not comps: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 489 | comps.append(curdir) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 490 | return prefix + sep.join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 491 | |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 492 | def _abspath_fallback(path): |
| 493 | """Return the absolute version of a path as a fallback function in case |
| 494 | `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for |
| 495 | more. |
| 496 | |
| 497 | """ |
| 498 | |
| 499 | path = os.fspath(path) |
| 500 | if not isabs(path): |
| 501 | if isinstance(path, bytes): |
| 502 | cwd = os.getcwdb() |
| 503 | else: |
| 504 | cwd = os.getcwd() |
| 505 | path = join(cwd, path) |
| 506 | return normpath(path) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 507 | |
| 508 | # Return an absolute path. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 509 | try: |
| 510 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 511 | |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 512 | except ImportError: # not running on Windows - mock up something sensible |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 513 | abspath = _abspath_fallback |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 514 | |
| 515 | else: # use native Windows method on Windows |
| 516 | def abspath(path): |
| 517 | """Return the absolute version of a path.""" |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 518 | try: |
Tim Graham | d03b775 | 2018-10-25 11:26:38 -0400 | [diff] [blame] | 519 | return normpath(_getfullpathname(path)) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 520 | except (OSError, ValueError): |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 521 | return _abspath_fallback(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 522 | |
Miss Islington (bot) | c30c869 | 2019-08-21 14:09:33 -0700 | [diff] [blame] | 523 | try: |
| 524 | from nt import _getfinalpathname, readlink as _nt_readlink |
| 525 | except ImportError: |
| 526 | # realpath is a no-op on systems without _getfinalpathname support. |
| 527 | realpath = abspath |
| 528 | else: |
| 529 | def _readlink_deep(path, seen=None): |
| 530 | if seen is None: |
| 531 | seen = set() |
| 532 | |
| 533 | while normcase(path) not in seen: |
| 534 | seen.add(normcase(path)) |
| 535 | try: |
| 536 | path = _nt_readlink(path) |
| 537 | except OSError as ex: |
| 538 | # Stop on file (2) or directory (3) not found, or |
| 539 | # paths that are not reparse points (4390) |
| 540 | if ex.winerror in (2, 3, 4390): |
| 541 | break |
| 542 | raise |
| 543 | except ValueError: |
| 544 | # Stop on reparse points that are not symlinks |
| 545 | break |
| 546 | return path |
| 547 | |
| 548 | def _getfinalpathname_nonstrict(path): |
| 549 | # Fast path to get the final path name. If this succeeds, there |
| 550 | # is no need to go any further. |
| 551 | try: |
| 552 | return _getfinalpathname(path) |
| 553 | except OSError: |
| 554 | pass |
| 555 | |
| 556 | # Allow file (2) or directory (3) not found, invalid syntax (123), |
| 557 | # and symlinks that cannot be followed (1921) |
| 558 | allowed_winerror = 2, 3, 123, 1921 |
| 559 | |
| 560 | # Non-strict algorithm is to find as much of the target directory |
| 561 | # as we can and join the rest. |
| 562 | tail = '' |
| 563 | seen = set() |
| 564 | while path: |
| 565 | try: |
| 566 | path = _readlink_deep(path, seen) |
| 567 | path = _getfinalpathname(path) |
| 568 | return join(path, tail) if tail else path |
| 569 | except OSError as ex: |
| 570 | if ex.winerror not in allowed_winerror: |
| 571 | raise |
| 572 | path, name = split(path) |
| 573 | if path and not name: |
| 574 | return abspath(path + tail) |
| 575 | tail = join(name, tail) if tail else name |
| 576 | return abspath(tail) |
| 577 | |
| 578 | def realpath(path): |
Steve Dower | a50d2f7 | 2019-08-21 17:23:38 -0700 | [diff] [blame^] | 579 | path = normpath(path) |
Miss Islington (bot) | c30c869 | 2019-08-21 14:09:33 -0700 | [diff] [blame] | 580 | if isinstance(path, bytes): |
| 581 | prefix = b'\\\\?\\' |
| 582 | unc_prefix = b'\\\\?\\UNC\\' |
| 583 | new_unc_prefix = b'\\\\' |
| 584 | cwd = os.getcwdb() |
| 585 | else: |
| 586 | prefix = '\\\\?\\' |
| 587 | unc_prefix = '\\\\?\\UNC\\' |
| 588 | new_unc_prefix = '\\\\' |
| 589 | cwd = os.getcwd() |
Steve Dower | a50d2f7 | 2019-08-21 17:23:38 -0700 | [diff] [blame^] | 590 | did_not_exist = not exists(path) |
Miss Islington (bot) | c30c869 | 2019-08-21 14:09:33 -0700 | [diff] [blame] | 591 | had_prefix = path.startswith(prefix) |
| 592 | path = _getfinalpathname_nonstrict(path) |
| 593 | # The path returned by _getfinalpathname will always start with \\?\ - |
| 594 | # strip off that prefix unless it was already provided on the original |
| 595 | # path. |
| 596 | if not had_prefix and path.startswith(prefix): |
| 597 | # For UNC paths, the prefix will actually be \\?\UNC\ |
| 598 | # Handle that case as well. |
| 599 | if path.startswith(unc_prefix): |
| 600 | spath = new_unc_prefix + path[len(unc_prefix):] |
| 601 | else: |
| 602 | spath = path[len(prefix):] |
| 603 | # Ensure that the non-prefixed path resolves to the same path |
| 604 | try: |
| 605 | if _getfinalpathname(spath) == path: |
| 606 | path = spath |
| 607 | except OSError as ex: |
Steve Dower | a50d2f7 | 2019-08-21 17:23:38 -0700 | [diff] [blame^] | 608 | # If the path does not exist and originally did not exist, then |
| 609 | # strip the prefix anyway. |
| 610 | if ex.winerror in {2, 3} and did_not_exist: |
| 611 | path = spath |
Miss Islington (bot) | c30c869 | 2019-08-21 14:09:33 -0700 | [diff] [blame] | 612 | return path |
| 613 | |
| 614 | |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 615 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 616 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 617 | sys.getwindowsversion()[3] >= 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 618 | |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 619 | def relpath(path, start=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 620 | """Return a relative version of a path""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 621 | path = os.fspath(path) |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 622 | if isinstance(path, bytes): |
| 623 | sep = b'\\' |
| 624 | curdir = b'.' |
| 625 | pardir = b'..' |
| 626 | else: |
| 627 | sep = '\\' |
| 628 | curdir = '.' |
| 629 | pardir = '..' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 630 | |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 631 | if start is None: |
| 632 | start = curdir |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 633 | |
| 634 | if not path: |
| 635 | raise ValueError("no path specified") |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 636 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 637 | start = os.fspath(start) |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 638 | try: |
| 639 | start_abs = abspath(normpath(start)) |
| 640 | path_abs = abspath(normpath(path)) |
| 641 | start_drive, start_rest = splitdrive(start_abs) |
| 642 | path_drive, path_rest = splitdrive(path_abs) |
| 643 | if normcase(start_drive) != normcase(path_drive): |
| 644 | raise ValueError("path is on mount %r, start on mount %r" % ( |
| 645 | path_drive, start_drive)) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 646 | |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 647 | start_list = [x for x in start_rest.split(sep) if x] |
| 648 | path_list = [x for x in path_rest.split(sep) if x] |
| 649 | # Work out how much of the filepath is shared by start and path. |
| 650 | i = 0 |
| 651 | for e1, e2 in zip(start_list, path_list): |
| 652 | if normcase(e1) != normcase(e2): |
| 653 | break |
| 654 | i += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 655 | |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 656 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
| 657 | if not rel_list: |
| 658 | return curdir |
| 659 | return join(*rel_list) |
Serhiy Storchaka | e4f4708 | 2014-10-04 16:09:02 +0300 | [diff] [blame] | 660 | except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 661 | genericpath._check_arg_types('relpath', path, start) |
| 662 | raise |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 663 | |
| 664 | |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 665 | # Return the longest common sub-path of the sequence of paths given as input. |
| 666 | # The function is case-insensitive and 'separator-insensitive', i.e. if the |
| 667 | # only difference between two paths is the use of '\' versus '/' as separator, |
| 668 | # they are deemed to be equal. |
| 669 | # |
| 670 | # However, the returned path will have the standard '\' separator (even if the |
| 671 | # given paths had the alternative '/' separator) and will have the case of the |
| 672 | # first path given in the sequence. Additionally, any trailing separator is |
| 673 | # stripped from the returned path. |
| 674 | |
| 675 | def commonpath(paths): |
| 676 | """Given a sequence of path names, returns the longest common sub-path.""" |
| 677 | |
| 678 | if not paths: |
| 679 | raise ValueError('commonpath() arg is an empty sequence') |
| 680 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 681 | paths = tuple(map(os.fspath, paths)) |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 682 | if isinstance(paths[0], bytes): |
| 683 | sep = b'\\' |
| 684 | altsep = b'/' |
| 685 | curdir = b'.' |
| 686 | else: |
| 687 | sep = '\\' |
| 688 | altsep = '/' |
| 689 | curdir = '.' |
| 690 | |
| 691 | try: |
| 692 | drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths] |
| 693 | split_paths = [p.split(sep) for d, p in drivesplits] |
| 694 | |
| 695 | try: |
| 696 | isabs, = set(p[:1] == sep for d, p in drivesplits) |
| 697 | except ValueError: |
| 698 | raise ValueError("Can't mix absolute and relative paths") from None |
| 699 | |
| 700 | # Check that all drive letters or UNC paths match. The check is made only |
| 701 | # now otherwise type errors for mixing strings and bytes would not be |
| 702 | # caught. |
| 703 | if len(set(d for d, p in drivesplits)) != 1: |
| 704 | raise ValueError("Paths don't have the same drive") |
| 705 | |
| 706 | drive, path = splitdrive(paths[0].replace(altsep, sep)) |
| 707 | common = path.split(sep) |
| 708 | common = [c for c in common if c and c != curdir] |
| 709 | |
| 710 | split_paths = [[c for c in s if c and c != curdir] for s in split_paths] |
| 711 | s1 = min(split_paths) |
| 712 | s2 = max(split_paths) |
| 713 | for i, c in enumerate(s1): |
| 714 | if c != s2[i]: |
| 715 | common = common[:i] |
| 716 | break |
| 717 | else: |
| 718 | common = common[:len(s1)] |
| 719 | |
| 720 | prefix = drive + sep if isabs else drive |
| 721 | return prefix + sep.join(common) |
| 722 | except (TypeError, AttributeError): |
| 723 | genericpath._check_arg_types('commonpath', *paths) |
| 724 | raise |
| 725 | |
| 726 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 727 | try: |
| 728 | # The genericpath.isdir implementation uses os.stat and checks the mode |
| 729 | # attribute to tell whether or not the path is a directory. |
| 730 | # This is overkill on Windows - just pass the path to GetFileAttributes |
| 731 | # and check the attribute from there. |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 732 | from nt import _isdir as isdir |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 733 | except ImportError: |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 734 | # Use genericpath.isdir as imported above. |
| 735 | pass |