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