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