Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1 | import fnmatch |
| 2 | import functools |
| 3 | import io |
| 4 | import ntpath |
| 5 | import os |
| 6 | import posixpath |
| 7 | import re |
| 8 | import sys |
Serhiy Storchaka | 8110837 | 2017-09-26 00:55:55 +0300 | [diff] [blame] | 9 | from _collections_abc import Sequence |
Jörg Stucke | d5c120f | 2019-05-21 19:44:40 +0200 | [diff] [blame] | 10 | from errno import EINVAL, ENOENT, ENOTDIR, EBADF, ELOOP |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 11 | from operator import attrgetter |
| 12 | from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO |
Antoine Pitrou | 069a5e1 | 2013-12-03 09:41:35 +0100 | [diff] [blame] | 13 | from urllib.parse import quote_from_bytes as urlquote_from_bytes |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 14 | |
| 15 | |
| 16 | supports_symlinks = True |
Antoine Pitrou | db118f5 | 2014-11-19 00:32:08 +0100 | [diff] [blame] | 17 | if os.name == 'nt': |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 18 | import nt |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 19 | if sys.getwindowsversion()[:2] >= (6, 0): |
| 20 | from nt import _getfinalpathname |
| 21 | else: |
| 22 | supports_symlinks = False |
| 23 | _getfinalpathname = None |
Antoine Pitrou | db118f5 | 2014-11-19 00:32:08 +0100 | [diff] [blame] | 24 | else: |
| 25 | nt = None |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 26 | |
| 27 | |
| 28 | __all__ = [ |
| 29 | "PurePath", "PurePosixPath", "PureWindowsPath", |
| 30 | "Path", "PosixPath", "WindowsPath", |
| 31 | ] |
| 32 | |
| 33 | # |
| 34 | # Internals |
| 35 | # |
| 36 | |
penguindustin | 9646630 | 2019-05-06 14:57:17 -0400 | [diff] [blame] | 37 | # EBADF - guard against macOS `stat` throwing EBADF |
Jörg Stucke | d5c120f | 2019-05-21 19:44:40 +0200 | [diff] [blame] | 38 | _IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF, ELOOP) |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 39 | |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 40 | _IGNORED_WINERRORS = ( |
| 41 | 21, # ERROR_NOT_READY - drive exists but is not accessible |
Jörg Stucke | d5c120f | 2019-05-21 19:44:40 +0200 | [diff] [blame] | 42 | 1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | def _ignore_error(exception): |
| 46 | return (getattr(exception, 'errno', None) in _IGNORED_ERROS or |
| 47 | getattr(exception, 'winerror', None) in _IGNORED_WINERRORS) |
| 48 | |
| 49 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 50 | def _is_wildcard_pattern(pat): |
| 51 | # Whether this pattern needs actual matching using fnmatch, or can |
| 52 | # be looked up directly as a file. |
| 53 | return "*" in pat or "?" in pat or "[" in pat |
| 54 | |
| 55 | |
| 56 | class _Flavour(object): |
| 57 | """A flavour implements a particular (platform-specific) set of path |
| 58 | semantics.""" |
| 59 | |
| 60 | def __init__(self): |
| 61 | self.join = self.sep.join |
| 62 | |
| 63 | def parse_parts(self, parts): |
| 64 | parsed = [] |
| 65 | sep = self.sep |
| 66 | altsep = self.altsep |
| 67 | drv = root = '' |
| 68 | it = reversed(parts) |
| 69 | for part in it: |
| 70 | if not part: |
| 71 | continue |
| 72 | if altsep: |
| 73 | part = part.replace(altsep, sep) |
| 74 | drv, root, rel = self.splitroot(part) |
| 75 | if sep in rel: |
| 76 | for x in reversed(rel.split(sep)): |
| 77 | if x and x != '.': |
| 78 | parsed.append(sys.intern(x)) |
| 79 | else: |
| 80 | if rel and rel != '.': |
| 81 | parsed.append(sys.intern(rel)) |
| 82 | if drv or root: |
| 83 | if not drv: |
| 84 | # If no drive is present, try to find one in the previous |
| 85 | # parts. This makes the result of parsing e.g. |
| 86 | # ("C:", "/", "a") reasonably intuitive. |
| 87 | for part in it: |
Antoine Pitrou | 57fffd6 | 2015-02-15 18:03:59 +0100 | [diff] [blame] | 88 | if not part: |
| 89 | continue |
| 90 | if altsep: |
| 91 | part = part.replace(altsep, sep) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 92 | drv = self.splitroot(part)[0] |
| 93 | if drv: |
| 94 | break |
| 95 | break |
| 96 | if drv or root: |
| 97 | parsed.append(drv + root) |
| 98 | parsed.reverse() |
| 99 | return drv, root, parsed |
| 100 | |
| 101 | def join_parsed_parts(self, drv, root, parts, drv2, root2, parts2): |
| 102 | """ |
| 103 | Join the two paths represented by the respective |
| 104 | (drive, root, parts) tuples. Return a new (drive, root, parts) tuple. |
| 105 | """ |
| 106 | if root2: |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 107 | if not drv2 and drv: |
| 108 | return drv, root2, [drv + root2] + parts2[1:] |
| 109 | elif drv2: |
| 110 | if drv2 == drv or self.casefold(drv2) == self.casefold(drv): |
| 111 | # Same drive => second path is relative to the first |
| 112 | return drv, root, parts + parts2[1:] |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 113 | else: |
Serhiy Storchaka | a993902 | 2013-12-06 17:14:12 +0200 | [diff] [blame] | 114 | # Second path is non-anchored (common case) |
| 115 | return drv, root, parts + parts2 |
| 116 | return drv2, root2, parts2 |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 117 | |
| 118 | |
| 119 | class _WindowsFlavour(_Flavour): |
| 120 | # Reference for Windows paths can be found at |
| 121 | # http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx |
| 122 | |
| 123 | sep = '\\' |
| 124 | altsep = '/' |
| 125 | has_drv = True |
| 126 | pathmod = ntpath |
| 127 | |
Antoine Pitrou | db118f5 | 2014-11-19 00:32:08 +0100 | [diff] [blame] | 128 | is_supported = (os.name == 'nt') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 129 | |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 130 | drive_letters = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 131 | ext_namespace_prefix = '\\\\?\\' |
| 132 | |
| 133 | reserved_names = ( |
| 134 | {'CON', 'PRN', 'AUX', 'NUL'} | |
| 135 | {'COM%d' % i for i in range(1, 10)} | |
| 136 | {'LPT%d' % i for i in range(1, 10)} |
| 137 | ) |
| 138 | |
| 139 | # Interesting findings about extended paths: |
| 140 | # - '\\?\c:\a', '//?/c:\a' and '//?/c:/a' are all supported |
| 141 | # but '\\?\c:/a' is not |
| 142 | # - extended paths are always absolute; "relative" extended paths will |
| 143 | # fail. |
| 144 | |
| 145 | def splitroot(self, part, sep=sep): |
| 146 | first = part[0:1] |
| 147 | second = part[1:2] |
| 148 | if (second == sep and first == sep): |
| 149 | # XXX extended paths should also disable the collapsing of "." |
| 150 | # components (according to MSDN docs). |
| 151 | prefix, part = self._split_extended_path(part) |
| 152 | first = part[0:1] |
| 153 | second = part[1:2] |
| 154 | else: |
| 155 | prefix = '' |
| 156 | third = part[2:3] |
| 157 | if (second == sep and first == sep and third != sep): |
| 158 | # is a UNC path: |
| 159 | # vvvvvvvvvvvvvvvvvvvvv root |
| 160 | # \\machine\mountpoint\directory\etc\... |
| 161 | # directory ^^^^^^^^^^^^^^ |
| 162 | index = part.find(sep, 2) |
| 163 | if index != -1: |
| 164 | index2 = part.find(sep, index + 1) |
| 165 | # a UNC path can't have two slashes in a row |
| 166 | # (after the initial two) |
| 167 | if index2 != index + 1: |
| 168 | if index2 == -1: |
| 169 | index2 = len(part) |
| 170 | if prefix: |
| 171 | return prefix + part[1:index2], sep, part[index2+1:] |
| 172 | else: |
| 173 | return part[:index2], sep, part[index2+1:] |
| 174 | drv = root = '' |
| 175 | if second == ':' and first in self.drive_letters: |
| 176 | drv = part[:2] |
| 177 | part = part[2:] |
| 178 | first = third |
| 179 | if first == sep: |
| 180 | root = first |
| 181 | part = part.lstrip(sep) |
| 182 | return prefix + drv, root, part |
| 183 | |
| 184 | def casefold(self, s): |
| 185 | return s.lower() |
| 186 | |
| 187 | def casefold_parts(self, parts): |
| 188 | return [p.lower() for p in parts] |
| 189 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 190 | def compile_pattern(self, pattern): |
| 191 | return re.compile(fnmatch.translate(pattern), re.IGNORECASE).fullmatch |
| 192 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 193 | def resolve(self, path, strict=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 194 | s = str(path) |
| 195 | if not s: |
| 196 | return os.getcwd() |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 197 | previous_s = None |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 198 | if _getfinalpathname is not None: |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 199 | if strict: |
| 200 | return self._ext_to_normal(_getfinalpathname(s)) |
| 201 | else: |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 202 | tail_parts = [] # End of the path after the first one not found |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 203 | while True: |
| 204 | try: |
| 205 | s = self._ext_to_normal(_getfinalpathname(s)) |
| 206 | except FileNotFoundError: |
| 207 | previous_s = s |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 208 | s, tail = os.path.split(s) |
| 209 | tail_parts.append(tail) |
Steve Dower | 4b1e98b | 2016-12-28 16:02:59 -0800 | [diff] [blame] | 210 | if previous_s == s: |
| 211 | return path |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 212 | else: |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 213 | return os.path.join(s, *reversed(tail_parts)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 214 | # Means fallback on absolute |
| 215 | return None |
| 216 | |
| 217 | def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix): |
| 218 | prefix = '' |
| 219 | if s.startswith(ext_prefix): |
| 220 | prefix = s[:4] |
| 221 | s = s[4:] |
| 222 | if s.startswith('UNC\\'): |
| 223 | prefix += s[:3] |
| 224 | s = '\\' + s[3:] |
| 225 | return prefix, s |
| 226 | |
| 227 | def _ext_to_normal(self, s): |
| 228 | # Turn back an extended path into a normal DOS-like path |
| 229 | return self._split_extended_path(s)[1] |
| 230 | |
| 231 | def is_reserved(self, parts): |
| 232 | # NOTE: the rules for reserved names seem somewhat complicated |
| 233 | # (e.g. r"..\NUL" is reserved but not r"foo\NUL"). |
| 234 | # We err on the side of caution and return True for paths which are |
| 235 | # not considered reserved by Windows. |
| 236 | if not parts: |
| 237 | return False |
| 238 | if parts[0].startswith('\\\\'): |
| 239 | # UNC paths are never reserved |
| 240 | return False |
| 241 | return parts[-1].partition('.')[0].upper() in self.reserved_names |
| 242 | |
| 243 | def make_uri(self, path): |
| 244 | # Under Windows, file URIs use the UTF-8 encoding. |
| 245 | drive = path.drive |
| 246 | if len(drive) == 2 and drive[1] == ':': |
| 247 | # It's a path on a local drive => 'file:///c:/a/b' |
| 248 | rest = path.as_posix()[2:].lstrip('/') |
| 249 | return 'file:///%s/%s' % ( |
| 250 | drive, urlquote_from_bytes(rest.encode('utf-8'))) |
| 251 | else: |
| 252 | # It's a path on a network drive => 'file://host/share/a/b' |
| 253 | return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8')) |
| 254 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 255 | def gethomedir(self, username): |
Christoph Reiter | c45a2aa | 2020-01-28 10:41:50 +0100 | [diff] [blame] | 256 | if 'USERPROFILE' in os.environ: |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 257 | userhome = os.environ['USERPROFILE'] |
| 258 | elif 'HOMEPATH' in os.environ: |
Antoine Pitrou | 5d4e27e | 2014-12-30 22:09:42 +0100 | [diff] [blame] | 259 | try: |
| 260 | drv = os.environ['HOMEDRIVE'] |
| 261 | except KeyError: |
| 262 | drv = '' |
| 263 | userhome = drv + os.environ['HOMEPATH'] |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 264 | else: |
| 265 | raise RuntimeError("Can't determine home directory") |
| 266 | |
| 267 | if username: |
| 268 | # Try to guess user home directory. By default all users |
| 269 | # directories are located in the same place and are named by |
| 270 | # corresponding usernames. If current user home directory points |
| 271 | # to nonstandard place, this guess is likely wrong. |
| 272 | if os.environ['USERNAME'] != username: |
| 273 | drv, root, parts = self.parse_parts((userhome,)) |
| 274 | if parts[-1] != os.environ['USERNAME']: |
| 275 | raise RuntimeError("Can't determine home directory " |
| 276 | "for %r" % username) |
| 277 | parts[-1] = username |
| 278 | if drv or root: |
| 279 | userhome = drv + root + self.join(parts[1:]) |
| 280 | else: |
| 281 | userhome = self.join(parts) |
| 282 | return userhome |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 283 | |
| 284 | class _PosixFlavour(_Flavour): |
| 285 | sep = '/' |
| 286 | altsep = '' |
| 287 | has_drv = False |
| 288 | pathmod = posixpath |
| 289 | |
| 290 | is_supported = (os.name != 'nt') |
| 291 | |
| 292 | def splitroot(self, part, sep=sep): |
| 293 | if part and part[0] == sep: |
| 294 | stripped_part = part.lstrip(sep) |
| 295 | # According to POSIX path resolution: |
| 296 | # http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11 |
| 297 | # "A pathname that begins with two successive slashes may be |
| 298 | # interpreted in an implementation-defined manner, although more |
| 299 | # than two leading slashes shall be treated as a single slash". |
| 300 | if len(part) - len(stripped_part) == 2: |
| 301 | return '', sep * 2, stripped_part |
| 302 | else: |
| 303 | return '', sep, stripped_part |
| 304 | else: |
| 305 | return '', '', part |
| 306 | |
| 307 | def casefold(self, s): |
| 308 | return s |
| 309 | |
| 310 | def casefold_parts(self, parts): |
| 311 | return parts |
| 312 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 313 | def compile_pattern(self, pattern): |
| 314 | return re.compile(fnmatch.translate(pattern)).fullmatch |
| 315 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 316 | def resolve(self, path, strict=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 317 | sep = self.sep |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 318 | accessor = path._accessor |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 319 | seen = {} |
| 320 | def _resolve(path, rest): |
| 321 | if rest.startswith(sep): |
| 322 | path = '' |
| 323 | |
| 324 | for name in rest.split(sep): |
| 325 | if not name or name == '.': |
| 326 | # current dir |
| 327 | continue |
| 328 | if name == '..': |
| 329 | # parent dir |
| 330 | path, _, _ = path.rpartition(sep) |
| 331 | continue |
| 332 | newpath = path + sep + name |
| 333 | if newpath in seen: |
| 334 | # Already seen this path |
| 335 | path = seen[newpath] |
| 336 | if path is not None: |
| 337 | # use cached value |
| 338 | continue |
| 339 | # The symlink is not resolved, so we must have a symlink loop. |
| 340 | raise RuntimeError("Symlink loop from %r" % newpath) |
| 341 | # Resolve the symbolic link |
| 342 | try: |
| 343 | target = accessor.readlink(newpath) |
| 344 | except OSError as e: |
Antoine Pietri | add98eb | 2017-06-07 17:29:17 +0200 | [diff] [blame] | 345 | if e.errno != EINVAL and strict: |
| 346 | raise |
| 347 | # Not a symlink, or non-strict mode. We just leave the path |
| 348 | # untouched. |
Antoine Pitrou | c274fd2 | 2013-12-16 19:57:41 +0100 | [diff] [blame] | 349 | path = newpath |
| 350 | else: |
| 351 | seen[newpath] = None # not resolved symlink |
| 352 | path = _resolve(path, target) |
| 353 | seen[newpath] = path # resolved symlink |
| 354 | |
| 355 | return path |
| 356 | # NOTE: according to POSIX, getcwd() cannot contain path components |
| 357 | # which are symlinks. |
| 358 | base = '' if path.is_absolute() else os.getcwd() |
| 359 | return _resolve(base, str(path)) or sep |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 360 | |
| 361 | def is_reserved(self, parts): |
| 362 | return False |
| 363 | |
| 364 | def make_uri(self, path): |
| 365 | # We represent the path using the local filesystem encoding, |
| 366 | # for portability to other applications. |
| 367 | bpath = bytes(path) |
| 368 | return 'file://' + urlquote_from_bytes(bpath) |
| 369 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 370 | def gethomedir(self, username): |
| 371 | if not username: |
| 372 | try: |
| 373 | return os.environ['HOME'] |
| 374 | except KeyError: |
| 375 | import pwd |
| 376 | return pwd.getpwuid(os.getuid()).pw_dir |
| 377 | else: |
| 378 | import pwd |
| 379 | try: |
| 380 | return pwd.getpwnam(username).pw_dir |
| 381 | except KeyError: |
| 382 | raise RuntimeError("Can't determine home directory " |
| 383 | "for %r" % username) |
| 384 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 385 | |
| 386 | _windows_flavour = _WindowsFlavour() |
| 387 | _posix_flavour = _PosixFlavour() |
| 388 | |
| 389 | |
| 390 | class _Accessor: |
| 391 | """An accessor implements a particular (system-specific or not) way of |
| 392 | accessing paths on the filesystem.""" |
| 393 | |
| 394 | |
| 395 | class _NormalAccessor(_Accessor): |
| 396 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 397 | stat = os.stat |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 398 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 399 | lstat = os.lstat |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 400 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 401 | open = os.open |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 402 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 403 | listdir = os.listdir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 404 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 405 | scandir = os.scandir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 406 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 407 | chmod = os.chmod |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 408 | |
| 409 | if hasattr(os, "lchmod"): |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 410 | lchmod = os.lchmod |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 411 | else: |
| 412 | def lchmod(self, pathobj, mode): |
| 413 | raise NotImplementedError("lchmod() not available on this system") |
| 414 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 415 | mkdir = os.mkdir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 416 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 417 | unlink = os.unlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 418 | |
Toke Høiland-Jørgensen | 092435e | 2019-12-16 13:23:55 +0100 | [diff] [blame] | 419 | if hasattr(os, "link"): |
| 420 | link_to = os.link |
| 421 | else: |
| 422 | @staticmethod |
| 423 | def link_to(self, target): |
| 424 | raise NotImplementedError("os.link() not available on this system") |
Joannah Nanjekye | 6b5b013 | 2019-05-04 11:27:10 -0400 | [diff] [blame] | 425 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 426 | rmdir = os.rmdir |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 427 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 428 | rename = os.rename |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 429 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 430 | replace = os.replace |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 431 | |
| 432 | if nt: |
| 433 | if supports_symlinks: |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 434 | symlink = os.symlink |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 435 | else: |
| 436 | def symlink(a, b, target_is_directory): |
| 437 | raise NotImplementedError("symlink() not available on this system") |
| 438 | else: |
| 439 | # Under POSIX, os.symlink() takes two args |
| 440 | @staticmethod |
| 441 | def symlink(a, b, target_is_directory): |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 442 | return os.symlink(a, b) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 443 | |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 444 | utime = os.utime |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 445 | |
| 446 | # Helper for resolve() |
| 447 | def readlink(self, path): |
| 448 | return os.readlink(path) |
| 449 | |
| 450 | |
| 451 | _normal_accessor = _NormalAccessor() |
| 452 | |
| 453 | |
| 454 | # |
| 455 | # Globbing helpers |
| 456 | # |
| 457 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 458 | def _make_selector(pattern_parts, flavour): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 459 | pat = pattern_parts[0] |
| 460 | child_parts = pattern_parts[1:] |
| 461 | if pat == '**': |
| 462 | cls = _RecursiveWildcardSelector |
| 463 | elif '**' in pat: |
| 464 | raise ValueError("Invalid pattern: '**' can only be an entire path component") |
| 465 | elif _is_wildcard_pattern(pat): |
| 466 | cls = _WildcardSelector |
| 467 | else: |
| 468 | cls = _PreciseSelector |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 469 | return cls(pat, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 470 | |
| 471 | if hasattr(functools, "lru_cache"): |
| 472 | _make_selector = functools.lru_cache()(_make_selector) |
| 473 | |
| 474 | |
| 475 | class _Selector: |
| 476 | """A selector matches a specific glob pattern part against the children |
| 477 | of a given path.""" |
| 478 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 479 | def __init__(self, child_parts, flavour): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 480 | self.child_parts = child_parts |
| 481 | if child_parts: |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 482 | self.successor = _make_selector(child_parts, flavour) |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 483 | self.dironly = True |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 484 | else: |
| 485 | self.successor = _TerminatingSelector() |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 486 | self.dironly = False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 487 | |
| 488 | def select_from(self, parent_path): |
| 489 | """Iterate over all child paths of `parent_path` matched by this |
| 490 | selector. This can contain parent_path itself.""" |
| 491 | path_cls = type(parent_path) |
| 492 | is_dir = path_cls.is_dir |
| 493 | exists = path_cls.exists |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 494 | scandir = parent_path._accessor.scandir |
| 495 | if not is_dir(parent_path): |
| 496 | return iter([]) |
| 497 | return self._select_from(parent_path, is_dir, exists, scandir) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 498 | |
| 499 | |
| 500 | class _TerminatingSelector: |
| 501 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 502 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 503 | yield parent_path |
| 504 | |
| 505 | |
| 506 | class _PreciseSelector(_Selector): |
| 507 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 508 | def __init__(self, name, child_parts, flavour): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 509 | self.name = name |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 510 | _Selector.__init__(self, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 511 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 512 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 513 | try: |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 514 | path = parent_path._make_child_relpath(self.name) |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 515 | if (is_dir if self.dironly else exists)(path): |
| 516 | for p in self.successor._select_from(path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 517 | yield p |
| 518 | except PermissionError: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 519 | return |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 520 | |
| 521 | |
| 522 | class _WildcardSelector(_Selector): |
| 523 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 524 | def __init__(self, pat, child_parts, flavour): |
| 525 | self.match = flavour.compile_pattern(pat) |
| 526 | _Selector.__init__(self, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 527 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 528 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 529 | try: |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 530 | entries = list(scandir(parent_path)) |
| 531 | for entry in entries: |
Pablo Galindo | eb7560a | 2020-03-07 17:53:20 +0000 | [diff] [blame] | 532 | if self.dironly: |
| 533 | try: |
| 534 | # "entry.is_dir()" can raise PermissionError |
| 535 | # in some cases (see bpo-38894), which is not |
| 536 | # among the errors ignored by _ignore_error() |
| 537 | if not entry.is_dir(): |
| 538 | continue |
| 539 | except OSError as e: |
| 540 | if not _ignore_error(e): |
| 541 | raise |
| 542 | continue |
| 543 | name = entry.name |
| 544 | if self.match(name): |
| 545 | path = parent_path._make_child_relpath(name) |
| 546 | for p in self.successor._select_from(path, is_dir, exists, scandir): |
| 547 | yield p |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 548 | except PermissionError: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 549 | return |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 550 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 551 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 552 | class _RecursiveWildcardSelector(_Selector): |
| 553 | |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 554 | def __init__(self, pat, child_parts, flavour): |
| 555 | _Selector.__init__(self, child_parts, flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 556 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 557 | def _iterate_directories(self, parent_path, is_dir, scandir): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 558 | yield parent_path |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 559 | try: |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 560 | entries = list(scandir(parent_path)) |
| 561 | for entry in entries: |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 562 | entry_is_dir = False |
| 563 | try: |
| 564 | entry_is_dir = entry.is_dir() |
| 565 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 566 | if not _ignore_error(e): |
Przemysław Spodymek | 216b745 | 2018-08-27 23:33:45 +0200 | [diff] [blame] | 567 | raise |
| 568 | if entry_is_dir and not entry.is_symlink(): |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 569 | path = parent_path._make_child_relpath(entry.name) |
| 570 | for p in self._iterate_directories(path, is_dir, scandir): |
Guido van Rossum | bc9fdda | 2016-01-07 10:56:36 -0800 | [diff] [blame] | 571 | yield p |
| 572 | except PermissionError: |
| 573 | return |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 574 | |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 575 | def _select_from(self, parent_path, is_dir, exists, scandir): |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 576 | try: |
Serhiy Storchaka | 680cb15 | 2016-09-07 10:58:05 +0300 | [diff] [blame] | 577 | yielded = set() |
| 578 | try: |
| 579 | successor_select = self.successor._select_from |
| 580 | for starting_point in self._iterate_directories(parent_path, is_dir, scandir): |
| 581 | for p in successor_select(starting_point, is_dir, exists, scandir): |
| 582 | if p not in yielded: |
| 583 | yield p |
| 584 | yielded.add(p) |
| 585 | finally: |
| 586 | yielded.clear() |
Guido van Rossum | 6c2d33a | 2016-01-06 09:42:07 -0800 | [diff] [blame] | 587 | except PermissionError: |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 588 | return |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 589 | |
| 590 | |
| 591 | # |
| 592 | # Public API |
| 593 | # |
| 594 | |
| 595 | class _PathParents(Sequence): |
| 596 | """This object provides sequence-like access to the logical ancestors |
| 597 | of a path. Don't try to construct it yourself.""" |
| 598 | __slots__ = ('_pathcls', '_drv', '_root', '_parts') |
| 599 | |
| 600 | def __init__(self, path): |
| 601 | # We don't store the instance to avoid reference cycles |
| 602 | self._pathcls = type(path) |
| 603 | self._drv = path._drv |
| 604 | self._root = path._root |
| 605 | self._parts = path._parts |
| 606 | |
| 607 | def __len__(self): |
| 608 | if self._drv or self._root: |
| 609 | return len(self._parts) - 1 |
| 610 | else: |
| 611 | return len(self._parts) |
| 612 | |
| 613 | def __getitem__(self, idx): |
| 614 | if idx < 0 or idx >= len(self): |
| 615 | raise IndexError(idx) |
| 616 | return self._pathcls._from_parsed_parts(self._drv, self._root, |
| 617 | self._parts[:-idx - 1]) |
| 618 | |
| 619 | def __repr__(self): |
| 620 | return "<{}.parents>".format(self._pathcls.__name__) |
| 621 | |
| 622 | |
| 623 | class PurePath(object): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 624 | """Base class for manipulating paths without I/O. |
| 625 | |
| 626 | PurePath represents a filesystem path and offers operations which |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 627 | don't imply any actual filesystem I/O. Depending on your system, |
| 628 | instantiating a PurePath will return either a PurePosixPath or a |
| 629 | PureWindowsPath object. You can also instantiate either of these classes |
| 630 | directly, regardless of your system. |
| 631 | """ |
| 632 | __slots__ = ( |
| 633 | '_drv', '_root', '_parts', |
| 634 | '_str', '_hash', '_pparts', '_cached_cparts', |
| 635 | ) |
| 636 | |
| 637 | def __new__(cls, *args): |
| 638 | """Construct a PurePath from one or several strings and or existing |
| 639 | PurePath objects. The strings and path objects are combined so as |
| 640 | to yield a canonicalized path, which is incorporated into the |
| 641 | new PurePath object. |
| 642 | """ |
| 643 | if cls is PurePath: |
| 644 | cls = PureWindowsPath if os.name == 'nt' else PurePosixPath |
| 645 | return cls._from_parts(args) |
| 646 | |
| 647 | def __reduce__(self): |
| 648 | # Using the parts tuple helps share interned path parts |
| 649 | # when pickling related paths. |
| 650 | return (self.__class__, tuple(self._parts)) |
| 651 | |
| 652 | @classmethod |
| 653 | def _parse_args(cls, args): |
| 654 | # This is useful when you don't want to create an instance, just |
| 655 | # canonicalize some constructor arguments. |
| 656 | parts = [] |
| 657 | for a in args: |
| 658 | if isinstance(a, PurePath): |
| 659 | parts += a._parts |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 660 | else: |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 661 | a = os.fspath(a) |
| 662 | if isinstance(a, str): |
| 663 | # Force-cast str subclasses to str (issue #21127) |
| 664 | parts.append(str(a)) |
| 665 | else: |
| 666 | raise TypeError( |
| 667 | "argument should be a str object or an os.PathLike " |
| 668 | "object returning str, not %r" |
| 669 | % type(a)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 670 | return cls._flavour.parse_parts(parts) |
| 671 | |
| 672 | @classmethod |
| 673 | def _from_parts(cls, args, init=True): |
| 674 | # We need to call _parse_args on the instance, so as to get the |
| 675 | # right flavour. |
| 676 | self = object.__new__(cls) |
| 677 | drv, root, parts = self._parse_args(args) |
| 678 | self._drv = drv |
| 679 | self._root = root |
| 680 | self._parts = parts |
| 681 | if init: |
| 682 | self._init() |
| 683 | return self |
| 684 | |
| 685 | @classmethod |
| 686 | def _from_parsed_parts(cls, drv, root, parts, init=True): |
| 687 | self = object.__new__(cls) |
| 688 | self._drv = drv |
| 689 | self._root = root |
| 690 | self._parts = parts |
| 691 | if init: |
| 692 | self._init() |
| 693 | return self |
| 694 | |
| 695 | @classmethod |
| 696 | def _format_parsed_parts(cls, drv, root, parts): |
| 697 | if drv or root: |
| 698 | return drv + root + cls._flavour.join(parts[1:]) |
| 699 | else: |
| 700 | return cls._flavour.join(parts) |
| 701 | |
| 702 | def _init(self): |
Martin Panter | e26da7c | 2016-06-02 10:07:09 +0000 | [diff] [blame] | 703 | # Overridden in concrete Path |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 704 | pass |
| 705 | |
| 706 | def _make_child(self, args): |
| 707 | drv, root, parts = self._parse_args(args) |
| 708 | drv, root, parts = self._flavour.join_parsed_parts( |
| 709 | self._drv, self._root, self._parts, drv, root, parts) |
| 710 | return self._from_parsed_parts(drv, root, parts) |
| 711 | |
| 712 | def __str__(self): |
| 713 | """Return the string representation of the path, suitable for |
| 714 | passing to system calls.""" |
| 715 | try: |
| 716 | return self._str |
| 717 | except AttributeError: |
| 718 | self._str = self._format_parsed_parts(self._drv, self._root, |
| 719 | self._parts) or '.' |
| 720 | return self._str |
| 721 | |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 722 | def __fspath__(self): |
| 723 | return str(self) |
| 724 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 725 | def as_posix(self): |
| 726 | """Return the string representation of the path with forward (/) |
| 727 | slashes.""" |
| 728 | f = self._flavour |
| 729 | return str(self).replace(f.sep, '/') |
| 730 | |
| 731 | def __bytes__(self): |
| 732 | """Return the bytes representation of the path. This is only |
| 733 | recommended to use under Unix.""" |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 734 | return os.fsencode(self) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 735 | |
| 736 | def __repr__(self): |
| 737 | return "{}({!r})".format(self.__class__.__name__, self.as_posix()) |
| 738 | |
| 739 | def as_uri(self): |
| 740 | """Return the path as a 'file' URI.""" |
| 741 | if not self.is_absolute(): |
| 742 | raise ValueError("relative path can't be expressed as a file URI") |
| 743 | return self._flavour.make_uri(self) |
| 744 | |
| 745 | @property |
| 746 | def _cparts(self): |
| 747 | # Cached casefolded parts, for hashing and comparison |
| 748 | try: |
| 749 | return self._cached_cparts |
| 750 | except AttributeError: |
| 751 | self._cached_cparts = self._flavour.casefold_parts(self._parts) |
| 752 | return self._cached_cparts |
| 753 | |
| 754 | def __eq__(self, other): |
| 755 | if not isinstance(other, PurePath): |
| 756 | return NotImplemented |
| 757 | return self._cparts == other._cparts and self._flavour is other._flavour |
| 758 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 759 | def __hash__(self): |
| 760 | try: |
| 761 | return self._hash |
| 762 | except AttributeError: |
| 763 | self._hash = hash(tuple(self._cparts)) |
| 764 | return self._hash |
| 765 | |
| 766 | def __lt__(self, other): |
| 767 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 768 | return NotImplemented |
| 769 | return self._cparts < other._cparts |
| 770 | |
| 771 | def __le__(self, other): |
| 772 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 773 | return NotImplemented |
| 774 | return self._cparts <= other._cparts |
| 775 | |
| 776 | def __gt__(self, other): |
| 777 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 778 | return NotImplemented |
| 779 | return self._cparts > other._cparts |
| 780 | |
| 781 | def __ge__(self, other): |
| 782 | if not isinstance(other, PurePath) or self._flavour is not other._flavour: |
| 783 | return NotImplemented |
| 784 | return self._cparts >= other._cparts |
| 785 | |
Batuhan Taşkaya | 526606b | 2019-12-08 23:31:15 +0300 | [diff] [blame] | 786 | def __class_getitem__(cls, type): |
| 787 | return cls |
| 788 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 789 | drive = property(attrgetter('_drv'), |
| 790 | doc="""The drive prefix (letter or UNC path), if any.""") |
| 791 | |
| 792 | root = property(attrgetter('_root'), |
| 793 | doc="""The root of the path, if any.""") |
| 794 | |
| 795 | @property |
| 796 | def anchor(self): |
| 797 | """The concatenation of the drive and root, or ''.""" |
| 798 | anchor = self._drv + self._root |
| 799 | return anchor |
| 800 | |
| 801 | @property |
| 802 | def name(self): |
| 803 | """The final path component, if any.""" |
| 804 | parts = self._parts |
| 805 | if len(parts) == (1 if (self._drv or self._root) else 0): |
| 806 | return '' |
| 807 | return parts[-1] |
| 808 | |
| 809 | @property |
| 810 | def suffix(self): |
Ram Rachum | 8d4fef4 | 2019-11-02 18:46:24 +0200 | [diff] [blame] | 811 | """ |
| 812 | The final component's last suffix, if any. |
| 813 | |
| 814 | This includes the leading period. For example: '.txt' |
| 815 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 816 | name = self.name |
| 817 | i = name.rfind('.') |
| 818 | if 0 < i < len(name) - 1: |
| 819 | return name[i:] |
| 820 | else: |
| 821 | return '' |
| 822 | |
| 823 | @property |
| 824 | def suffixes(self): |
Ram Rachum | 8d4fef4 | 2019-11-02 18:46:24 +0200 | [diff] [blame] | 825 | """ |
| 826 | A list of the final component's suffixes, if any. |
| 827 | |
| 828 | These include the leading periods. For example: ['.tar', '.gz'] |
| 829 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 830 | name = self.name |
| 831 | if name.endswith('.'): |
| 832 | return [] |
| 833 | name = name.lstrip('.') |
| 834 | return ['.' + suffix for suffix in name.split('.')[1:]] |
| 835 | |
| 836 | @property |
| 837 | def stem(self): |
| 838 | """The final path component, minus its last suffix.""" |
| 839 | name = self.name |
| 840 | i = name.rfind('.') |
| 841 | if 0 < i < len(name) - 1: |
| 842 | return name[:i] |
| 843 | else: |
| 844 | return name |
| 845 | |
| 846 | def with_name(self, name): |
| 847 | """Return a new path with the file name changed.""" |
| 848 | if not self.name: |
| 849 | raise ValueError("%r has an empty name" % (self,)) |
Antoine Pitrou | 7084e73 | 2014-07-06 21:31:12 -0400 | [diff] [blame] | 850 | drv, root, parts = self._flavour.parse_parts((name,)) |
| 851 | if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep] |
| 852 | or drv or root or len(parts) != 1): |
| 853 | raise ValueError("Invalid name %r" % (name)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 854 | return self._from_parsed_parts(self._drv, self._root, |
| 855 | self._parts[:-1] + [name]) |
| 856 | |
| 857 | def with_suffix(self, suffix): |
Stefan Otte | 46dc4e3 | 2018-08-03 22:49:42 +0200 | [diff] [blame] | 858 | """Return a new path with the file suffix changed. If the path |
| 859 | has no suffix, add given suffix. If the given suffix is an empty |
| 860 | string, remove the suffix from the path. |
| 861 | """ |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 862 | f = self._flavour |
| 863 | if f.sep in suffix or f.altsep and f.altsep in suffix: |
Berker Peksag | 423d05f | 2018-08-11 08:45:06 +0300 | [diff] [blame] | 864 | raise ValueError("Invalid suffix %r" % (suffix,)) |
Antoine Pitrou | e50dafc | 2014-07-06 21:37:15 -0400 | [diff] [blame] | 865 | if suffix and not suffix.startswith('.') or suffix == '.': |
Antoine Pitrou | 1b02da9 | 2014-01-03 00:07:17 +0100 | [diff] [blame] | 866 | raise ValueError("Invalid suffix %r" % (suffix)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 867 | name = self.name |
| 868 | if not name: |
| 869 | raise ValueError("%r has an empty name" % (self,)) |
| 870 | old_suffix = self.suffix |
| 871 | if not old_suffix: |
| 872 | name = name + suffix |
| 873 | else: |
| 874 | name = name[:-len(old_suffix)] + suffix |
| 875 | return self._from_parsed_parts(self._drv, self._root, |
| 876 | self._parts[:-1] + [name]) |
| 877 | |
| 878 | def relative_to(self, *other): |
| 879 | """Return the relative path to another path identified by the passed |
| 880 | arguments. If the operation is not possible (because this is not |
| 881 | a subpath of the other path), raise ValueError. |
| 882 | """ |
| 883 | # For the purpose of this method, drive and root are considered |
| 884 | # separate parts, i.e.: |
| 885 | # Path('c:/').relative_to('c:') gives Path('/') |
| 886 | # Path('c:/').relative_to('/') raise ValueError |
| 887 | if not other: |
| 888 | raise TypeError("need at least one argument") |
| 889 | parts = self._parts |
| 890 | drv = self._drv |
| 891 | root = self._root |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 892 | if root: |
| 893 | abs_parts = [drv, root] + parts[1:] |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 894 | else: |
| 895 | abs_parts = parts |
| 896 | to_drv, to_root, to_parts = self._parse_args(other) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 897 | if to_root: |
| 898 | to_abs_parts = [to_drv, to_root] + to_parts[1:] |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 899 | else: |
| 900 | to_abs_parts = to_parts |
| 901 | n = len(to_abs_parts) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 902 | cf = self._flavour.casefold_parts |
| 903 | if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 904 | formatted = self._format_parsed_parts(to_drv, to_root, to_parts) |
| 905 | raise ValueError("{!r} does not start with {!r}" |
| 906 | .format(str(self), str(formatted))) |
Antoine Pitrou | 156b361 | 2013-12-28 19:49:04 +0100 | [diff] [blame] | 907 | return self._from_parsed_parts('', root if n == 1 else '', |
| 908 | abs_parts[n:]) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 909 | |
Hai Shi | 82642a0 | 2019-08-13 14:54:02 -0500 | [diff] [blame] | 910 | def is_relative_to(self, *other): |
| 911 | """Return True if the path is relative to another path or False. |
| 912 | """ |
| 913 | try: |
| 914 | self.relative_to(*other) |
| 915 | return True |
| 916 | except ValueError: |
| 917 | return False |
| 918 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 919 | @property |
| 920 | def parts(self): |
| 921 | """An object providing sequence-like access to the |
| 922 | components in the filesystem path.""" |
| 923 | # We cache the tuple to avoid building a new one each time .parts |
| 924 | # is accessed. XXX is this necessary? |
| 925 | try: |
| 926 | return self._pparts |
| 927 | except AttributeError: |
| 928 | self._pparts = tuple(self._parts) |
| 929 | return self._pparts |
| 930 | |
| 931 | def joinpath(self, *args): |
| 932 | """Combine this path with one or several arguments, and return a |
| 933 | new path representing either a subpath (if all arguments are relative |
| 934 | paths) or a totally different path (if one of the arguments is |
| 935 | anchored). |
| 936 | """ |
| 937 | return self._make_child(args) |
| 938 | |
| 939 | def __truediv__(self, key): |
aiudirog | 4c69be2 | 2019-08-08 01:41:10 -0400 | [diff] [blame] | 940 | try: |
| 941 | return self._make_child((key,)) |
| 942 | except TypeError: |
| 943 | return NotImplemented |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 944 | |
| 945 | def __rtruediv__(self, key): |
aiudirog | 4c69be2 | 2019-08-08 01:41:10 -0400 | [diff] [blame] | 946 | try: |
| 947 | return self._from_parts([key] + self._parts) |
| 948 | except TypeError: |
| 949 | return NotImplemented |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 950 | |
| 951 | @property |
| 952 | def parent(self): |
| 953 | """The logical parent of the path.""" |
| 954 | drv = self._drv |
| 955 | root = self._root |
| 956 | parts = self._parts |
| 957 | if len(parts) == 1 and (drv or root): |
| 958 | return self |
| 959 | return self._from_parsed_parts(drv, root, parts[:-1]) |
| 960 | |
| 961 | @property |
| 962 | def parents(self): |
| 963 | """A sequence of this path's logical parents.""" |
| 964 | return _PathParents(self) |
| 965 | |
| 966 | def is_absolute(self): |
| 967 | """True if the path is absolute (has both a root and, if applicable, |
| 968 | a drive).""" |
| 969 | if not self._root: |
| 970 | return False |
| 971 | return not self._flavour.has_drv or bool(self._drv) |
| 972 | |
| 973 | def is_reserved(self): |
| 974 | """Return True if the path contains one of the special names reserved |
| 975 | by the system, if any.""" |
| 976 | return self._flavour.is_reserved(self._parts) |
| 977 | |
| 978 | def match(self, path_pattern): |
| 979 | """ |
| 980 | Return True if this path matches the given pattern. |
| 981 | """ |
| 982 | cf = self._flavour.casefold |
| 983 | path_pattern = cf(path_pattern) |
| 984 | drv, root, pat_parts = self._flavour.parse_parts((path_pattern,)) |
| 985 | if not pat_parts: |
| 986 | raise ValueError("empty pattern") |
| 987 | if drv and drv != cf(self._drv): |
| 988 | return False |
| 989 | if root and root != cf(self._root): |
| 990 | return False |
| 991 | parts = self._cparts |
| 992 | if drv or root: |
| 993 | if len(pat_parts) != len(parts): |
| 994 | return False |
| 995 | pat_parts = pat_parts[1:] |
| 996 | elif len(pat_parts) > len(parts): |
| 997 | return False |
| 998 | for part, pat in zip(reversed(parts), reversed(pat_parts)): |
| 999 | if not fnmatch.fnmatchcase(part, pat): |
| 1000 | return False |
| 1001 | return True |
| 1002 | |
Brett Cannon | 568be63 | 2016-06-10 12:20:49 -0700 | [diff] [blame] | 1003 | # Can't subclass os.PathLike from PurePath and keep the constructor |
| 1004 | # optimizations in PurePath._parse_args(). |
| 1005 | os.PathLike.register(PurePath) |
| 1006 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1007 | |
| 1008 | class PurePosixPath(PurePath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1009 | """PurePath subclass for non-Windows systems. |
| 1010 | |
| 1011 | On a POSIX system, instantiating a PurePath should return this object. |
| 1012 | However, you can also instantiate it directly on any system. |
| 1013 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1014 | _flavour = _posix_flavour |
| 1015 | __slots__ = () |
| 1016 | |
| 1017 | |
| 1018 | class PureWindowsPath(PurePath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1019 | """PurePath subclass for Windows systems. |
| 1020 | |
| 1021 | On a Windows system, instantiating a PurePath should return this object. |
| 1022 | However, you can also instantiate it directly on any system. |
| 1023 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1024 | _flavour = _windows_flavour |
| 1025 | __slots__ = () |
| 1026 | |
| 1027 | |
| 1028 | # Filesystem-accessing classes |
| 1029 | |
| 1030 | |
| 1031 | class Path(PurePath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1032 | """PurePath subclass that can make system calls. |
| 1033 | |
| 1034 | Path represents a filesystem path but unlike PurePath, also offers |
| 1035 | methods to do system calls on path objects. Depending on your system, |
| 1036 | instantiating a Path will return either a PosixPath or a WindowsPath |
| 1037 | object. You can also instantiate a PosixPath or WindowsPath directly, |
| 1038 | but cannot instantiate a WindowsPath on a POSIX system or vice versa. |
| 1039 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1040 | __slots__ = ( |
| 1041 | '_accessor', |
| 1042 | '_closed', |
| 1043 | ) |
| 1044 | |
| 1045 | def __new__(cls, *args, **kwargs): |
| 1046 | if cls is Path: |
| 1047 | cls = WindowsPath if os.name == 'nt' else PosixPath |
| 1048 | self = cls._from_parts(args, init=False) |
| 1049 | if not self._flavour.is_supported: |
| 1050 | raise NotImplementedError("cannot instantiate %r on your system" |
| 1051 | % (cls.__name__,)) |
| 1052 | self._init() |
| 1053 | return self |
| 1054 | |
| 1055 | def _init(self, |
| 1056 | # Private non-constructor arguments |
| 1057 | template=None, |
| 1058 | ): |
| 1059 | self._closed = False |
| 1060 | if template is not None: |
| 1061 | self._accessor = template._accessor |
| 1062 | else: |
| 1063 | self._accessor = _normal_accessor |
| 1064 | |
| 1065 | def _make_child_relpath(self, part): |
| 1066 | # This is an optimization used for dir walking. `part` must be |
| 1067 | # a single part relative to this path. |
| 1068 | parts = self._parts + [part] |
| 1069 | return self._from_parsed_parts(self._drv, self._root, parts) |
| 1070 | |
| 1071 | def __enter__(self): |
| 1072 | if self._closed: |
| 1073 | self._raise_closed() |
| 1074 | return self |
| 1075 | |
| 1076 | def __exit__(self, t, v, tb): |
| 1077 | self._closed = True |
| 1078 | |
| 1079 | def _raise_closed(self): |
| 1080 | raise ValueError("I/O operation on closed path") |
| 1081 | |
| 1082 | def _opener(self, name, flags, mode=0o666): |
| 1083 | # A stub for the opener argument to built-in open() |
| 1084 | return self._accessor.open(self, flags, mode) |
| 1085 | |
Antoine Pitrou | 4a60d42 | 2013-12-02 21:25:18 +0100 | [diff] [blame] | 1086 | def _raw_open(self, flags, mode=0o777): |
| 1087 | """ |
| 1088 | Open the file pointed by this path and return a file descriptor, |
| 1089 | as os.open() does. |
| 1090 | """ |
| 1091 | if self._closed: |
| 1092 | self._raise_closed() |
| 1093 | return self._accessor.open(self, flags, mode) |
| 1094 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1095 | # Public API |
| 1096 | |
| 1097 | @classmethod |
| 1098 | def cwd(cls): |
| 1099 | """Return a new path pointing to the current working directory |
| 1100 | (as returned by os.getcwd()). |
| 1101 | """ |
| 1102 | return cls(os.getcwd()) |
| 1103 | |
Antoine Pitrou | 17cba7d | 2015-01-12 21:03:41 +0100 | [diff] [blame] | 1104 | @classmethod |
| 1105 | def home(cls): |
| 1106 | """Return a new path pointing to the user's home directory (as |
| 1107 | returned by os.path.expanduser('~')). |
| 1108 | """ |
| 1109 | return cls(cls()._flavour.gethomedir(None)) |
| 1110 | |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1111 | def samefile(self, other_path): |
Berker Peksag | 05492b8 | 2015-10-22 03:34:16 +0300 | [diff] [blame] | 1112 | """Return whether other_path is the same or not as this file |
Berker Peksag | 267597f | 2015-10-21 20:10:24 +0300 | [diff] [blame] | 1113 | (as returned by os.path.samefile()). |
Antoine Pitrou | 43e3d94 | 2014-05-13 10:50:15 +0200 | [diff] [blame] | 1114 | """ |
| 1115 | st = self.stat() |
| 1116 | try: |
| 1117 | other_st = other_path.stat() |
| 1118 | except AttributeError: |
| 1119 | other_st = os.stat(other_path) |
| 1120 | return os.path.samestat(st, other_st) |
| 1121 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1122 | def iterdir(self): |
| 1123 | """Iterate over the files in this directory. Does not yield any |
| 1124 | result for the special paths '.' and '..'. |
| 1125 | """ |
| 1126 | if self._closed: |
| 1127 | self._raise_closed() |
| 1128 | for name in self._accessor.listdir(self): |
| 1129 | if name in {'.', '..'}: |
| 1130 | # Yielding a path object for these makes little sense |
| 1131 | continue |
| 1132 | yield self._make_child_relpath(name) |
| 1133 | if self._closed: |
| 1134 | self._raise_closed() |
| 1135 | |
| 1136 | def glob(self, pattern): |
| 1137 | """Iterate over this subtree and yield all existing files (of any |
Eivind Teig | 537b6ca | 2019-02-11 11:47:09 +0100 | [diff] [blame] | 1138 | kind, including directories) matching the given relative pattern. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1139 | """ |
Serhiy Storchaka | f4f445b | 2020-02-12 12:11:34 +0200 | [diff] [blame] | 1140 | sys.audit("pathlib.Path.glob", self, pattern) |
Berker Peksag | 4a208e4 | 2016-01-30 17:50:48 +0200 | [diff] [blame] | 1141 | if not pattern: |
| 1142 | raise ValueError("Unacceptable pattern: {!r}".format(pattern)) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1143 | drv, root, pattern_parts = self._flavour.parse_parts((pattern,)) |
| 1144 | if drv or root: |
| 1145 | raise NotImplementedError("Non-relative patterns are unsupported") |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 1146 | selector = _make_selector(tuple(pattern_parts), self._flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1147 | for p in selector.select_from(self): |
| 1148 | yield p |
| 1149 | |
| 1150 | def rglob(self, pattern): |
| 1151 | """Recursively yield all existing files (of any kind, including |
Eivind Teig | 537b6ca | 2019-02-11 11:47:09 +0100 | [diff] [blame] | 1152 | directories) matching the given relative pattern, anywhere in |
| 1153 | this subtree. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1154 | """ |
Serhiy Storchaka | f4f445b | 2020-02-12 12:11:34 +0200 | [diff] [blame] | 1155 | sys.audit("pathlib.Path.rglob", self, pattern) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1156 | drv, root, pattern_parts = self._flavour.parse_parts((pattern,)) |
| 1157 | if drv or root: |
| 1158 | raise NotImplementedError("Non-relative patterns are unsupported") |
Serhiy Storchaka | 10ecbad | 2019-10-21 20:37:15 +0300 | [diff] [blame] | 1159 | selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1160 | for p in selector.select_from(self): |
| 1161 | yield p |
| 1162 | |
| 1163 | def absolute(self): |
| 1164 | """Return an absolute version of this path. This function works |
| 1165 | even if the path doesn't point to anything. |
| 1166 | |
| 1167 | No normalization is done, i.e. all '.' and '..' will be kept along. |
| 1168 | Use resolve() to get the canonical path to a file. |
| 1169 | """ |
| 1170 | # XXX untested yet! |
| 1171 | if self._closed: |
| 1172 | self._raise_closed() |
| 1173 | if self.is_absolute(): |
| 1174 | return self |
| 1175 | # FIXME this must defer to the specific flavour (and, under Windows, |
| 1176 | # use nt._getfullpathname()) |
| 1177 | obj = self._from_parts([os.getcwd()] + self._parts, init=False) |
| 1178 | obj._init(template=self) |
| 1179 | return obj |
| 1180 | |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1181 | def resolve(self, strict=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1182 | """ |
| 1183 | Make the path absolute, resolving all symlinks on the way and also |
| 1184 | normalizing it (for example turning slashes into backslashes under |
| 1185 | Windows). |
| 1186 | """ |
| 1187 | if self._closed: |
| 1188 | self._raise_closed() |
Steve Dower | 98eb360 | 2016-11-09 12:58:17 -0800 | [diff] [blame] | 1189 | s = self._flavour.resolve(self, strict=strict) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1190 | if s is None: |
| 1191 | # No symlink resolution => for consistency, raise an error if |
| 1192 | # the path doesn't exist or is forbidden |
| 1193 | self.stat() |
| 1194 | s = str(self.absolute()) |
| 1195 | # Now we have no symlinks in the path, it's safe to normalize it. |
| 1196 | normed = self._flavour.pathmod.normpath(s) |
| 1197 | obj = self._from_parts((normed,), init=False) |
| 1198 | obj._init(template=self) |
| 1199 | return obj |
| 1200 | |
| 1201 | def stat(self): |
| 1202 | """ |
| 1203 | Return the result of the stat() system call on this path, like |
| 1204 | os.stat() does. |
| 1205 | """ |
| 1206 | return self._accessor.stat(self) |
| 1207 | |
| 1208 | def owner(self): |
| 1209 | """ |
| 1210 | Return the login name of the file owner. |
| 1211 | """ |
| 1212 | import pwd |
| 1213 | return pwd.getpwuid(self.stat().st_uid).pw_name |
| 1214 | |
| 1215 | def group(self): |
| 1216 | """ |
| 1217 | Return the group name of the file gid. |
| 1218 | """ |
| 1219 | import grp |
| 1220 | return grp.getgrgid(self.stat().st_gid).gr_name |
| 1221 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1222 | def open(self, mode='r', buffering=-1, encoding=None, |
| 1223 | errors=None, newline=None): |
| 1224 | """ |
| 1225 | Open the file pointed by this path and return a file object, as |
| 1226 | the built-in open() function does. |
| 1227 | """ |
| 1228 | if self._closed: |
| 1229 | self._raise_closed() |
Serhiy Storchaka | 62a9951 | 2017-03-25 13:42:11 +0200 | [diff] [blame] | 1230 | return io.open(self, mode, buffering, encoding, errors, newline, |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1231 | opener=self._opener) |
| 1232 | |
Georg Brandl | ea68398 | 2014-10-01 19:12:33 +0200 | [diff] [blame] | 1233 | def read_bytes(self): |
| 1234 | """ |
| 1235 | Open the file in bytes mode, read it, and close the file. |
| 1236 | """ |
| 1237 | with self.open(mode='rb') as f: |
| 1238 | return f.read() |
| 1239 | |
| 1240 | def read_text(self, encoding=None, errors=None): |
| 1241 | """ |
| 1242 | Open the file in text mode, read it, and close the file. |
| 1243 | """ |
| 1244 | with self.open(mode='r', encoding=encoding, errors=errors) as f: |
| 1245 | return f.read() |
| 1246 | |
| 1247 | def write_bytes(self, data): |
| 1248 | """ |
| 1249 | Open the file in bytes mode, write to it, and close the file. |
| 1250 | """ |
| 1251 | # type-check for the buffer interface before truncating the file |
| 1252 | view = memoryview(data) |
| 1253 | with self.open(mode='wb') as f: |
| 1254 | return f.write(view) |
| 1255 | |
| 1256 | def write_text(self, data, encoding=None, errors=None): |
| 1257 | """ |
| 1258 | Open the file in text mode, write to it, and close the file. |
| 1259 | """ |
| 1260 | if not isinstance(data, str): |
| 1261 | raise TypeError('data must be str, not %s' % |
| 1262 | data.__class__.__name__) |
| 1263 | with self.open(mode='w', encoding=encoding, errors=errors) as f: |
| 1264 | return f.write(data) |
| 1265 | |
Girts | a01ba33 | 2019-10-23 14:18:40 -0700 | [diff] [blame] | 1266 | def readlink(self): |
| 1267 | """ |
| 1268 | Return the path to which the symbolic link points. |
| 1269 | """ |
| 1270 | path = self._accessor.readlink(self) |
| 1271 | obj = self._from_parts((path,), init=False) |
| 1272 | obj._init(template=self) |
| 1273 | return obj |
| 1274 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1275 | def touch(self, mode=0o666, exist_ok=True): |
| 1276 | """ |
| 1277 | Create this file with the given access mode, if it doesn't exist. |
| 1278 | """ |
| 1279 | if self._closed: |
| 1280 | self._raise_closed() |
| 1281 | if exist_ok: |
| 1282 | # First try to bump modification time |
| 1283 | # Implementation note: GNU touch uses the UTIME_NOW option of |
| 1284 | # the utimensat() / futimens() functions. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1285 | try: |
Antoine Pitrou | 2cf3917 | 2013-11-23 15:25:59 +0100 | [diff] [blame] | 1286 | self._accessor.utime(self, None) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1287 | except OSError: |
| 1288 | # Avoid exception chaining |
| 1289 | pass |
| 1290 | else: |
| 1291 | return |
| 1292 | flags = os.O_CREAT | os.O_WRONLY |
| 1293 | if not exist_ok: |
| 1294 | flags |= os.O_EXCL |
| 1295 | fd = self._raw_open(flags, mode) |
| 1296 | os.close(fd) |
| 1297 | |
Barry Warsaw | 7c549c4 | 2014-08-05 11:28:12 -0400 | [diff] [blame] | 1298 | def mkdir(self, mode=0o777, parents=False, exist_ok=False): |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1299 | """ |
| 1300 | Create a new directory at this given path. |
| 1301 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1302 | if self._closed: |
| 1303 | self._raise_closed() |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1304 | try: |
| 1305 | self._accessor.mkdir(self, mode) |
| 1306 | except FileNotFoundError: |
| 1307 | if not parents or self.parent == self: |
| 1308 | raise |
Armin Rigo | 22a594a | 2017-04-13 20:08:15 +0200 | [diff] [blame] | 1309 | self.parent.mkdir(parents=True, exist_ok=True) |
| 1310 | self.mkdir(mode, parents=False, exist_ok=exist_ok) |
Serhiy Storchaka | af7b9ec | 2017-03-24 20:51:53 +0200 | [diff] [blame] | 1311 | except OSError: |
| 1312 | # Cannot rely on checking for EEXIST, since the operating system |
| 1313 | # could give priority to other errors like EACCES or EROFS |
| 1314 | if not exist_ok or not self.is_dir(): |
| 1315 | raise |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1316 | |
| 1317 | def chmod(self, mode): |
| 1318 | """ |
| 1319 | Change the permissions of the path, like os.chmod(). |
| 1320 | """ |
| 1321 | if self._closed: |
| 1322 | self._raise_closed() |
| 1323 | self._accessor.chmod(self, mode) |
| 1324 | |
| 1325 | def lchmod(self, mode): |
| 1326 | """ |
| 1327 | Like chmod(), except if the path points to a symlink, the symlink's |
| 1328 | permissions are changed, rather than its target's. |
| 1329 | """ |
| 1330 | if self._closed: |
| 1331 | self._raise_closed() |
| 1332 | self._accessor.lchmod(self, mode) |
| 1333 | |
zlohhcuB treboR | d9e006b | 2019-05-16 00:02:11 +0200 | [diff] [blame] | 1334 | def unlink(self, missing_ok=False): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1335 | """ |
| 1336 | Remove this file or link. |
| 1337 | If the path is a directory, use rmdir() instead. |
| 1338 | """ |
| 1339 | if self._closed: |
| 1340 | self._raise_closed() |
zlohhcuB treboR | d9e006b | 2019-05-16 00:02:11 +0200 | [diff] [blame] | 1341 | try: |
| 1342 | self._accessor.unlink(self) |
| 1343 | except FileNotFoundError: |
| 1344 | if not missing_ok: |
| 1345 | raise |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1346 | |
| 1347 | def rmdir(self): |
| 1348 | """ |
| 1349 | Remove this directory. The directory must be empty. |
| 1350 | """ |
| 1351 | if self._closed: |
| 1352 | self._raise_closed() |
| 1353 | self._accessor.rmdir(self) |
| 1354 | |
| 1355 | def lstat(self): |
| 1356 | """ |
| 1357 | Like stat(), except if the path points to a symlink, the symlink's |
| 1358 | status information is returned, rather than its target's. |
| 1359 | """ |
| 1360 | if self._closed: |
| 1361 | self._raise_closed() |
| 1362 | return self._accessor.lstat(self) |
| 1363 | |
Joannah Nanjekye | 6b5b013 | 2019-05-04 11:27:10 -0400 | [diff] [blame] | 1364 | def link_to(self, target): |
| 1365 | """ |
| 1366 | Create a hard link pointing to a path named target. |
| 1367 | """ |
| 1368 | if self._closed: |
| 1369 | self._raise_closed() |
| 1370 | self._accessor.link_to(self, target) |
| 1371 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1372 | def rename(self, target): |
| 1373 | """ |
hui shang | 088a09a | 2019-09-11 21:26:49 +0800 | [diff] [blame] | 1374 | Rename this path to the given path, |
| 1375 | and return a new Path instance pointing to the given path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1376 | """ |
| 1377 | if self._closed: |
| 1378 | self._raise_closed() |
| 1379 | self._accessor.rename(self, target) |
hui shang | 088a09a | 2019-09-11 21:26:49 +0800 | [diff] [blame] | 1380 | return self.__class__(target) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1381 | |
| 1382 | def replace(self, target): |
| 1383 | """ |
| 1384 | Rename this path to the given path, clobbering the existing |
hui shang | 088a09a | 2019-09-11 21:26:49 +0800 | [diff] [blame] | 1385 | destination if it exists, and return a new Path instance |
| 1386 | pointing to the given path. |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1387 | """ |
| 1388 | if self._closed: |
| 1389 | self._raise_closed() |
| 1390 | self._accessor.replace(self, target) |
hui shang | 088a09a | 2019-09-11 21:26:49 +0800 | [diff] [blame] | 1391 | return self.__class__(target) |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1392 | |
| 1393 | def symlink_to(self, target, target_is_directory=False): |
| 1394 | """ |
| 1395 | Make this path a symlink pointing to the given path. |
| 1396 | Note the order of arguments (self, target) is the reverse of os.symlink's. |
| 1397 | """ |
| 1398 | if self._closed: |
| 1399 | self._raise_closed() |
| 1400 | self._accessor.symlink(target, self, target_is_directory) |
| 1401 | |
| 1402 | # Convenience functions for querying the stat results |
| 1403 | |
| 1404 | def exists(self): |
| 1405 | """ |
| 1406 | Whether this path exists. |
| 1407 | """ |
| 1408 | try: |
| 1409 | self.stat() |
| 1410 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1411 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1412 | raise |
| 1413 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1414 | except ValueError: |
| 1415 | # Non-encodable path |
| 1416 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1417 | return True |
| 1418 | |
| 1419 | def is_dir(self): |
| 1420 | """ |
| 1421 | Whether this path is a directory. |
| 1422 | """ |
| 1423 | try: |
| 1424 | return S_ISDIR(self.stat().st_mode) |
| 1425 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1426 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1427 | raise |
| 1428 | # Path doesn't exist or is a broken symlink |
| 1429 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1430 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1431 | except ValueError: |
| 1432 | # Non-encodable path |
| 1433 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1434 | |
| 1435 | def is_file(self): |
| 1436 | """ |
| 1437 | Whether this path is a regular file (also True for symlinks pointing |
| 1438 | to regular files). |
| 1439 | """ |
| 1440 | try: |
| 1441 | return S_ISREG(self.stat().st_mode) |
| 1442 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1443 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1444 | raise |
| 1445 | # Path doesn't exist or is a broken symlink |
| 1446 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1447 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1448 | except ValueError: |
| 1449 | # Non-encodable path |
| 1450 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1451 | |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1452 | def is_mount(self): |
| 1453 | """ |
| 1454 | Check if this path is a POSIX mount point |
| 1455 | """ |
| 1456 | # Need to exist and be a dir |
| 1457 | if not self.exists() or not self.is_dir(): |
| 1458 | return False |
| 1459 | |
| 1460 | parent = Path(self.parent) |
| 1461 | try: |
| 1462 | parent_dev = parent.stat().st_dev |
| 1463 | except OSError: |
| 1464 | return False |
| 1465 | |
| 1466 | dev = self.stat().st_dev |
| 1467 | if dev != parent_dev: |
| 1468 | return True |
| 1469 | ino = self.stat().st_ino |
| 1470 | parent_ino = parent.stat().st_ino |
| 1471 | return ino == parent_ino |
| 1472 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1473 | def is_symlink(self): |
| 1474 | """ |
| 1475 | Whether this path is a symbolic link. |
| 1476 | """ |
| 1477 | try: |
| 1478 | return S_ISLNK(self.lstat().st_mode) |
| 1479 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1480 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1481 | raise |
| 1482 | # Path doesn't exist |
| 1483 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1484 | except ValueError: |
| 1485 | # Non-encodable path |
| 1486 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1487 | |
| 1488 | def is_block_device(self): |
| 1489 | """ |
| 1490 | Whether this path is a block device. |
| 1491 | """ |
| 1492 | try: |
| 1493 | return S_ISBLK(self.stat().st_mode) |
| 1494 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1495 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1496 | raise |
| 1497 | # Path doesn't exist or is a broken symlink |
| 1498 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1499 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1500 | except ValueError: |
| 1501 | # Non-encodable path |
| 1502 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1503 | |
| 1504 | def is_char_device(self): |
| 1505 | """ |
| 1506 | Whether this path is a character device. |
| 1507 | """ |
| 1508 | try: |
| 1509 | return S_ISCHR(self.stat().st_mode) |
| 1510 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1511 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1512 | raise |
| 1513 | # Path doesn't exist or is a broken symlink |
| 1514 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1515 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1516 | except ValueError: |
| 1517 | # Non-encodable path |
| 1518 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1519 | |
| 1520 | def is_fifo(self): |
| 1521 | """ |
| 1522 | Whether this path is a FIFO. |
| 1523 | """ |
| 1524 | try: |
| 1525 | return S_ISFIFO(self.stat().st_mode) |
| 1526 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1527 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1528 | raise |
| 1529 | # Path doesn't exist or is a broken symlink |
| 1530 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1531 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1532 | except ValueError: |
| 1533 | # Non-encodable path |
| 1534 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1535 | |
| 1536 | def is_socket(self): |
| 1537 | """ |
| 1538 | Whether this path is a socket. |
| 1539 | """ |
| 1540 | try: |
| 1541 | return S_ISSOCK(self.stat().st_mode) |
| 1542 | except OSError as e: |
Steve Dower | 2f6fae6 | 2019-02-03 23:08:18 -0800 | [diff] [blame] | 1543 | if not _ignore_error(e): |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1544 | raise |
| 1545 | # Path doesn't exist or is a broken symlink |
| 1546 | # (see https://bitbucket.org/pitrou/pathlib/issue/12/) |
| 1547 | return False |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 1548 | except ValueError: |
| 1549 | # Non-encodable path |
| 1550 | return False |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1551 | |
Antoine Pitrou | 8477ed6 | 2014-12-30 20:54:45 +0100 | [diff] [blame] | 1552 | def expanduser(self): |
| 1553 | """ Return a new path with expanded ~ and ~user constructs |
| 1554 | (as returned by os.path.expanduser) |
| 1555 | """ |
| 1556 | if (not (self._drv or self._root) and |
| 1557 | self._parts and self._parts[0][:1] == '~'): |
| 1558 | homedir = self._flavour.gethomedir(self._parts[0][1:]) |
| 1559 | return self._from_parts([homedir] + self._parts[1:]) |
| 1560 | |
| 1561 | return self |
| 1562 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1563 | |
| 1564 | class PosixPath(Path, PurePosixPath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1565 | """Path subclass for non-Windows systems. |
| 1566 | |
| 1567 | On a POSIX system, instantiating a Path should return this object. |
| 1568 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1569 | __slots__ = () |
| 1570 | |
| 1571 | class WindowsPath(Path, PureWindowsPath): |
chason | dfa015c | 2018-02-19 08:36:32 +0900 | [diff] [blame] | 1572 | """Path subclass for Windows systems. |
| 1573 | |
| 1574 | On a Windows system, instantiating a Path should return this object. |
| 1575 | """ |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 1576 | __slots__ = () |
Berker Peksag | 04d4229 | 2016-03-11 23:07:27 +0200 | [diff] [blame] | 1577 | |
| 1578 | def owner(self): |
| 1579 | raise NotImplementedError("Path.owner() is unsupported on this system") |
| 1580 | |
| 1581 | def group(self): |
| 1582 | raise NotImplementedError("Path.group() is unsupported on this system") |
Cooper Lees | 173ff4a | 2017-08-01 15:35:45 -0700 | [diff] [blame] | 1583 | |
| 1584 | def is_mount(self): |
| 1585 | raise NotImplementedError("Path.is_mount() is unsupported on this system") |