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 |
| 461 | # do not do any normalization, but return the path unchanged |
| 462 | return path |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 463 | path = path.replace(altsep, sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 464 | prefix, path = splitdrive(path) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 465 | |
| 466 | # collapse initial backslashes |
| 467 | if path.startswith(sep): |
Georg Brandl | 599b65d | 2010-07-23 08:46:35 +0000 | [diff] [blame] | 468 | prefix += sep |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 469 | path = path.lstrip(sep) |
| 470 | |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 471 | comps = path.split(sep) |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 472 | i = 0 |
| 473 | while i < len(comps): |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 474 | if not comps[i] or comps[i] == curdir: |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 475 | del comps[i] |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 476 | elif comps[i] == pardir: |
| 477 | if i > 0 and comps[i-1] != pardir: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 478 | del comps[i-1:i+1] |
| 479 | i -= 1 |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 480 | elif i == 0 and prefix.endswith(sep): |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 481 | del comps[i] |
| 482 | else: |
| 483 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 484 | else: |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 485 | i += 1 |
Guido van Rossum | 15e22e1 | 1997-12-05 19:03:01 +0000 | [diff] [blame] | 486 | # If the path is now empty, substitute '.' |
| 487 | if not prefix and not comps: |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 488 | comps.append(curdir) |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 489 | return prefix + sep.join(comps) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 490 | |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 491 | def _abspath_fallback(path): |
| 492 | """Return the absolute version of a path as a fallback function in case |
| 493 | `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for |
| 494 | more. |
| 495 | |
| 496 | """ |
| 497 | |
| 498 | path = os.fspath(path) |
| 499 | if not isabs(path): |
| 500 | if isinstance(path, bytes): |
| 501 | cwd = os.getcwdb() |
| 502 | else: |
| 503 | cwd = os.getcwd() |
| 504 | path = join(cwd, path) |
| 505 | return normpath(path) |
Guido van Rossum | e294cf6 | 1999-01-29 18:05:18 +0000 | [diff] [blame] | 506 | |
| 507 | # Return an absolute path. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 508 | try: |
| 509 | from nt import _getfullpathname |
Mark Hammond | f717f05 | 2002-01-17 00:44:26 +0000 | [diff] [blame] | 510 | |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 511 | except ImportError: # not running on Windows - mock up something sensible |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 512 | abspath = _abspath_fallback |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 513 | |
| 514 | else: # use native Windows method on Windows |
| 515 | def abspath(path): |
| 516 | """Return the absolute version of a path.""" |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 517 | try: |
Tim Graham | d03b775 | 2018-10-25 11:26:38 -0400 | [diff] [blame] | 518 | return normpath(_getfullpathname(path)) |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 519 | except (OSError, ValueError): |
Franz Wöllert | d2e902e | 2018-07-29 14:47:09 +0200 | [diff] [blame] | 520 | return _abspath_fallback(path) |
Guido van Rossum | 83eeef4 | 2001-09-17 15:16:09 +0000 | [diff] [blame] | 521 | |
| 522 | # realpath is a no-op on systems without islink support |
| 523 | realpath = abspath |
Mark Hammond | 8696ebc | 2002-10-08 02:44:31 +0000 | [diff] [blame] | 524 | # Win9x family and earlier have no Unicode filename support. |
Tim Peters | 26bc25a | 2002-10-09 07:56:04 +0000 | [diff] [blame] | 525 | supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and |
| 526 | sys.getwindowsversion()[3] >= 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 527 | |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 528 | def relpath(path, start=None): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 529 | """Return a relative version of a path""" |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 530 | path = os.fspath(path) |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 531 | if isinstance(path, bytes): |
| 532 | sep = b'\\' |
| 533 | curdir = b'.' |
| 534 | pardir = b'..' |
| 535 | else: |
| 536 | sep = '\\' |
| 537 | curdir = '.' |
| 538 | pardir = '..' |
Amaury Forgeot d'Arc | c72ef8b | 2008-10-03 18:38:26 +0000 | [diff] [blame] | 539 | |
Serhiy Storchaka | 8518b79 | 2014-07-23 20:43:13 +0300 | [diff] [blame] | 540 | if start is None: |
| 541 | start = curdir |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 542 | |
| 543 | if not path: |
| 544 | raise ValueError("no path specified") |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 545 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 546 | start = os.fspath(start) |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 547 | try: |
| 548 | start_abs = abspath(normpath(start)) |
| 549 | path_abs = abspath(normpath(path)) |
| 550 | start_drive, start_rest = splitdrive(start_abs) |
| 551 | path_drive, path_rest = splitdrive(path_abs) |
| 552 | if normcase(start_drive) != normcase(path_drive): |
| 553 | raise ValueError("path is on mount %r, start on mount %r" % ( |
| 554 | path_drive, start_drive)) |
Mark Hammond | 5a607a3 | 2009-05-06 08:04:54 +0000 | [diff] [blame] | 555 | |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 556 | start_list = [x for x in start_rest.split(sep) if x] |
| 557 | path_list = [x for x in path_rest.split(sep) if x] |
| 558 | # Work out how much of the filepath is shared by start and path. |
| 559 | i = 0 |
| 560 | for e1, e2 in zip(start_list, path_list): |
| 561 | if normcase(e1) != normcase(e2): |
| 562 | break |
| 563 | i += 1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 564 | |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 565 | rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
| 566 | if not rel_list: |
| 567 | return curdir |
| 568 | return join(*rel_list) |
Serhiy Storchaka | e4f4708 | 2014-10-04 16:09:02 +0300 | [diff] [blame] | 569 | except (TypeError, ValueError, AttributeError, BytesWarning, DeprecationWarning): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 570 | genericpath._check_arg_types('relpath', path, start) |
| 571 | raise |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 572 | |
| 573 | |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 574 | # Return the longest common sub-path of the sequence of paths given as input. |
| 575 | # The function is case-insensitive and 'separator-insensitive', i.e. if the |
| 576 | # only difference between two paths is the use of '\' versus '/' as separator, |
| 577 | # they are deemed to be equal. |
| 578 | # |
| 579 | # However, the returned path will have the standard '\' separator (even if the |
| 580 | # given paths had the alternative '/' separator) and will have the case of the |
| 581 | # first path given in the sequence. Additionally, any trailing separator is |
| 582 | # stripped from the returned path. |
| 583 | |
| 584 | def commonpath(paths): |
| 585 | """Given a sequence of path names, returns the longest common sub-path.""" |
| 586 | |
| 587 | if not paths: |
| 588 | raise ValueError('commonpath() arg is an empty sequence') |
| 589 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 590 | paths = tuple(map(os.fspath, paths)) |
Serhiy Storchaka | 3822093 | 2015-03-31 15:31:53 +0300 | [diff] [blame] | 591 | if isinstance(paths[0], bytes): |
| 592 | sep = b'\\' |
| 593 | altsep = b'/' |
| 594 | curdir = b'.' |
| 595 | else: |
| 596 | sep = '\\' |
| 597 | altsep = '/' |
| 598 | curdir = '.' |
| 599 | |
| 600 | try: |
| 601 | drivesplits = [splitdrive(p.replace(altsep, sep).lower()) for p in paths] |
| 602 | split_paths = [p.split(sep) for d, p in drivesplits] |
| 603 | |
| 604 | try: |
| 605 | isabs, = set(p[:1] == sep for d, p in drivesplits) |
| 606 | except ValueError: |
| 607 | raise ValueError("Can't mix absolute and relative paths") from None |
| 608 | |
| 609 | # Check that all drive letters or UNC paths match. The check is made only |
| 610 | # now otherwise type errors for mixing strings and bytes would not be |
| 611 | # caught. |
| 612 | if len(set(d for d, p in drivesplits)) != 1: |
| 613 | raise ValueError("Paths don't have the same drive") |
| 614 | |
| 615 | drive, path = splitdrive(paths[0].replace(altsep, sep)) |
| 616 | common = path.split(sep) |
| 617 | common = [c for c in common if c and c != curdir] |
| 618 | |
| 619 | split_paths = [[c for c in s if c and c != curdir] for s in split_paths] |
| 620 | s1 = min(split_paths) |
| 621 | s2 = max(split_paths) |
| 622 | for i, c in enumerate(s1): |
| 623 | if c != s2[i]: |
| 624 | common = common[:i] |
| 625 | break |
| 626 | else: |
| 627 | common = common[:len(s1)] |
| 628 | |
| 629 | prefix = drive + sep if isabs else drive |
| 630 | return prefix + sep.join(common) |
| 631 | except (TypeError, AttributeError): |
| 632 | genericpath._check_arg_types('commonpath', *paths) |
| 633 | raise |
| 634 | |
| 635 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 636 | # determine if two files are in fact the same file |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 637 | try: |
Brian Curtin | e8e8042 | 2010-09-24 13:56:34 +0000 | [diff] [blame] | 638 | # GetFinalPathNameByHandle is available starting with Windows 6.0. |
| 639 | # Windows XP and non-Windows OS'es will mock _getfinalpathname. |
| 640 | if sys.getwindowsversion()[:2] >= (6, 0): |
| 641 | from nt import _getfinalpathname |
| 642 | else: |
| 643 | raise ImportError |
| 644 | except (AttributeError, ImportError): |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 645 | # On Windows XP and earlier, two files are the same if their absolute |
| 646 | # pathnames are the same. |
Brian Curtin | e8e8042 | 2010-09-24 13:56:34 +0000 | [diff] [blame] | 647 | # Non-Windows operating systems fake this method with an XP |
| 648 | # approximation. |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 649 | def _getfinalpathname(f): |
Ronald Oussoren | 6355c16 | 2011-05-06 17:11:07 +0200 | [diff] [blame] | 650 | return normcase(abspath(f)) |
Brian Curtin | 0dac808 | 2010-09-23 20:38:14 +0000 | [diff] [blame] | 651 | |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 652 | |
| 653 | try: |
| 654 | # The genericpath.isdir implementation uses os.stat and checks the mode |
| 655 | # attribute to tell whether or not the path is a directory. |
| 656 | # This is overkill on Windows - just pass the path to GetFileAttributes |
| 657 | # and check the attribute from there. |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 658 | from nt import _isdir as isdir |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 659 | except ImportError: |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 660 | # Use genericpath.isdir as imported above. |
| 661 | pass |