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