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