You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1341 lines
43 KiB

  1. import fnmatch
  2. import functools
  3. import io
  4. import ntpath
  5. import os
  6. import posixpath
  7. import re
  8. import sys
  9. from collections import Sequence
  10. from contextlib import contextmanager
  11. from errno import EINVAL, ENOENT, ENOTDIR
  12. from operator import attrgetter
  13. from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
  14. from urllib.parse import quote_from_bytes as urlquote_from_bytes
  15. supports_symlinks = True
  16. if os.name == 'nt':
  17. import nt
  18. if sys.getwindowsversion()[:2] >= (6, 0):
  19. from nt import _getfinalpathname
  20. else:
  21. supports_symlinks = False
  22. _getfinalpathname = None
  23. else:
  24. nt = None
  25. __all__ = [
  26. "PurePath", "PurePosixPath", "PureWindowsPath",
  27. "Path", "PosixPath", "WindowsPath",
  28. ]
  29. #
  30. # Internals
  31. #
  32. def _is_wildcard_pattern(pat):
  33. # Whether this pattern needs actual matching using fnmatch, or can
  34. # be looked up directly as a file.
  35. return "*" in pat or "?" in pat or "[" in pat
  36. class _Flavour(object):
  37. """A flavour implements a particular (platform-specific) set of path
  38. semantics."""
  39. def __init__(self):
  40. self.join = self.sep.join
  41. def parse_parts(self, parts):
  42. parsed = []
  43. sep = self.sep
  44. altsep = self.altsep
  45. drv = root = ''
  46. it = reversed(parts)
  47. for part in it:
  48. if not part:
  49. continue
  50. if altsep:
  51. part = part.replace(altsep, sep)
  52. drv, root, rel = self.splitroot(part)
  53. if sep in rel:
  54. for x in reversed(rel.split(sep)):
  55. if x and x != '.':
  56. parsed.append(sys.intern(x))
  57. else:
  58. if rel and rel != '.':
  59. parsed.append(sys.intern(rel))
  60. if drv or root:
  61. if not drv:
  62. # If no drive is present, try to find one in the previous
  63. # parts. This makes the result of parsing e.g.
  64. # ("C:", "/", "a") reasonably intuitive.
  65. for part in it:
  66. drv = self.splitroot(part)[0]
  67. if drv:
  68. break
  69. break
  70. if drv or root:
  71. parsed.append(drv + root)
  72. parsed.reverse()
  73. return drv, root, parsed
  74. def join_parsed_parts(self, drv, root, parts, drv2, root2, parts2):
  75. """
  76. Join the two paths represented by the respective
  77. (drive, root, parts) tuples. Return a new (drive, root, parts) tuple.
  78. """
  79. if root2:
  80. if not drv2 and drv:
  81. return drv, root2, [drv + root2] + parts2[1:]
  82. elif drv2:
  83. if drv2 == drv or self.casefold(drv2) == self.casefold(drv):
  84. # Same drive => second path is relative to the first
  85. return drv, root, parts + parts2[1:]
  86. else:
  87. # Second path is non-anchored (common case)
  88. return drv, root, parts + parts2
  89. return drv2, root2, parts2
  90. class _WindowsFlavour(_Flavour):
  91. # Reference for Windows paths can be found at
  92. # http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
  93. sep = '\\'
  94. altsep = '/'
  95. has_drv = True
  96. pathmod = ntpath
  97. is_supported = (os.name == 'nt')
  98. drive_letters = (
  99. set(chr(x) for x in range(ord('a'), ord('z') + 1)) |
  100. set(chr(x) for x in range(ord('A'), ord('Z') + 1))
  101. )
  102. ext_namespace_prefix = '\\\\?\\'
  103. reserved_names = (
  104. {'CON', 'PRN', 'AUX', 'NUL'} |
  105. {'COM%d' % i for i in range(1, 10)} |
  106. {'LPT%d' % i for i in range(1, 10)}
  107. )
  108. # Interesting findings about extended paths:
  109. # - '\\?\c:\a', '//?/c:\a' and '//?/c:/a' are all supported
  110. # but '\\?\c:/a' is not
  111. # - extended paths are always absolute; "relative" extended paths will
  112. # fail.
  113. def splitroot(self, part, sep=sep):
  114. first = part[0:1]
  115. second = part[1:2]
  116. if (second == sep and first == sep):
  117. # XXX extended paths should also disable the collapsing of "."
  118. # components (according to MSDN docs).
  119. prefix, part = self._split_extended_path(part)
  120. first = part[0:1]
  121. second = part[1:2]
  122. else:
  123. prefix = ''
  124. third = part[2:3]
  125. if (second == sep and first == sep and third != sep):
  126. # is a UNC path:
  127. # vvvvvvvvvvvvvvvvvvvvv root
  128. # \\machine\mountpoint\directory\etc\...
  129. # directory ^^^^^^^^^^^^^^
  130. index = part.find(sep, 2)
  131. if index != -1:
  132. index2 = part.find(sep, index + 1)
  133. # a UNC path can't have two slashes in a row
  134. # (after the initial two)
  135. if index2 != index + 1:
  136. if index2 == -1:
  137. index2 = len(part)
  138. if prefix:
  139. return prefix + part[1:index2], sep, part[index2+1:]
  140. else:
  141. return part[:index2], sep, part[index2+1:]
  142. drv = root = ''
  143. if second == ':' and first in self.drive_letters:
  144. drv = part[:2]
  145. part = part[2:]
  146. first = third
  147. if first == sep:
  148. root = first
  149. part = part.lstrip(sep)
  150. return prefix + drv, root, part
  151. def casefold(self, s):
  152. return s.lower()
  153. def casefold_parts(self, parts):
  154. return [p.lower() for p in parts]
  155. def resolve(self, path):
  156. s = str(path)
  157. if not s:
  158. return os.getcwd()
  159. if _getfinalpathname is not None:
  160. return self._ext_to_normal(_getfinalpathname(s))
  161. # Means fallback on absolute
  162. return None
  163. def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix):
  164. prefix = ''
  165. if s.startswith(ext_prefix):
  166. prefix = s[:4]
  167. s = s[4:]
  168. if s.startswith('UNC\\'):
  169. prefix += s[:3]
  170. s = '\\' + s[3:]
  171. return prefix, s
  172. def _ext_to_normal(self, s):
  173. # Turn back an extended path into a normal DOS-like path
  174. return self._split_extended_path(s)[1]
  175. def is_reserved(self, parts):
  176. # NOTE: the rules for reserved names seem somewhat complicated
  177. # (e.g. r"..\NUL" is reserved but not r"foo\NUL").
  178. # We err on the side of caution and return True for paths which are
  179. # not considered reserved by Windows.
  180. if not parts:
  181. return False
  182. if parts[0].startswith('\\\\'):
  183. # UNC paths are never reserved
  184. return False
  185. return parts[-1].partition('.')[0].upper() in self.reserved_names
  186. def make_uri(self, path):
  187. # Under Windows, file URIs use the UTF-8 encoding.
  188. drive = path.drive
  189. if len(drive) == 2 and drive[1] == ':':
  190. # It's a path on a local drive => 'file:///c:/a/b'
  191. rest = path.as_posix()[2:].lstrip('/')
  192. return 'file:///%s/%s' % (
  193. drive, urlquote_from_bytes(rest.encode('utf-8')))
  194. else:
  195. # It's a path on a network drive => 'file://host/share/a/b'
  196. return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
  197. class _PosixFlavour(_Flavour):
  198. sep = '/'
  199. altsep = ''
  200. has_drv = False
  201. pathmod = posixpath
  202. is_supported = (os.name != 'nt')
  203. def splitroot(self, part, sep=sep):
  204. if part and part[0] == sep:
  205. stripped_part = part.lstrip(sep)
  206. # According to POSIX path resolution:
  207. # http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11
  208. # "A pathname that begins with two successive slashes may be
  209. # interpreted in an implementation-defined manner, although more
  210. # than two leading slashes shall be treated as a single slash".
  211. if len(part) - len(stripped_part) == 2:
  212. return '', sep * 2, stripped_part
  213. else:
  214. return '', sep, stripped_part
  215. else:
  216. return '', '', part
  217. def casefold(self, s):
  218. return s
  219. def casefold_parts(self, parts):
  220. return parts
  221. def resolve(self, path):
  222. sep = self.sep
  223. accessor = path._accessor
  224. seen = {}
  225. def _resolve(path, rest):
  226. if rest.startswith(sep):
  227. path = ''
  228. for name in rest.split(sep):
  229. if not name or name == '.':
  230. # current dir
  231. continue
  232. if name == '..':
  233. # parent dir
  234. path, _, _ = path.rpartition(sep)
  235. continue
  236. newpath = path + sep + name
  237. if newpath in seen:
  238. # Already seen this path
  239. path = seen[newpath]
  240. if path is not None:
  241. # use cached value
  242. continue
  243. # The symlink is not resolved, so we must have a symlink loop.
  244. raise RuntimeError("Symlink loop from %r" % newpath)
  245. # Resolve the symbolic link
  246. try:
  247. target = accessor.readlink(newpath)
  248. except OSError as e:
  249. if e.errno != EINVAL:
  250. raise
  251. # Not a symlink
  252. path = newpath
  253. else:
  254. seen[newpath] = None # not resolved symlink
  255. path = _resolve(path, target)
  256. seen[newpath] = path # resolved symlink
  257. return path
  258. # NOTE: according to POSIX, getcwd() cannot contain path components
  259. # which are symlinks.
  260. base = '' if path.is_absolute() else os.getcwd()
  261. return _resolve(base, str(path)) or sep
  262. def is_reserved(self, parts):
  263. return False
  264. def make_uri(self, path):
  265. # We represent the path using the local filesystem encoding,
  266. # for portability to other applications.
  267. bpath = bytes(path)
  268. return 'file://' + urlquote_from_bytes(bpath)
  269. _windows_flavour = _WindowsFlavour()
  270. _posix_flavour = _PosixFlavour()
  271. class _Accessor:
  272. """An accessor implements a particular (system-specific or not) way of
  273. accessing paths on the filesystem."""
  274. class _NormalAccessor(_Accessor):
  275. def _wrap_strfunc(strfunc):
  276. @functools.wraps(strfunc)
  277. def wrapped(pathobj, *args):
  278. return strfunc(str(pathobj), *args)
  279. return staticmethod(wrapped)
  280. def _wrap_binary_strfunc(strfunc):
  281. @functools.wraps(strfunc)
  282. def wrapped(pathobjA, pathobjB, *args):
  283. return strfunc(str(pathobjA), str(pathobjB), *args)
  284. return staticmethod(wrapped)
  285. stat = _wrap_strfunc(os.stat)
  286. lstat = _wrap_strfunc(os.lstat)
  287. open = _wrap_strfunc(os.open)
  288. listdir = _wrap_strfunc(os.listdir)
  289. chmod = _wrap_strfunc(os.chmod)
  290. if hasattr(os, "lchmod"):
  291. lchmod = _wrap_strfunc(os.lchmod)
  292. else:
  293. def lchmod(self, pathobj, mode):
  294. raise NotImplementedError("lchmod() not available on this system")
  295. mkdir = _wrap_strfunc(os.mkdir)
  296. unlink = _wrap_strfunc(os.unlink)
  297. rmdir = _wrap_strfunc(os.rmdir)
  298. rename = _wrap_binary_strfunc(os.rename)
  299. replace = _wrap_binary_strfunc(os.replace)
  300. if nt:
  301. if supports_symlinks:
  302. symlink = _wrap_binary_strfunc(os.symlink)
  303. else:
  304. def symlink(a, b, target_is_directory):
  305. raise NotImplementedError("symlink() not available on this system")
  306. else:
  307. # Under POSIX, os.symlink() takes two args
  308. @staticmethod
  309. def symlink(a, b, target_is_directory):
  310. return os.symlink(str(a), str(b))
  311. utime = _wrap_strfunc(os.utime)
  312. # Helper for resolve()
  313. def readlink(self, path):
  314. return os.readlink(path)
  315. _normal_accessor = _NormalAccessor()
  316. #
  317. # Globbing helpers
  318. #
  319. @contextmanager
  320. def _cached(func):
  321. try:
  322. func.__cached__
  323. yield func
  324. except AttributeError:
  325. cache = {}
  326. def wrapper(*args):
  327. try:
  328. return cache[args]
  329. except KeyError:
  330. value = cache[args] = func(*args)
  331. return value
  332. wrapper.__cached__ = True
  333. try:
  334. yield wrapper
  335. finally:
  336. cache.clear()
  337. def _make_selector(pattern_parts):
  338. pat = pattern_parts[0]
  339. child_parts = pattern_parts[1:]
  340. if pat == '**':
  341. cls = _RecursiveWildcardSelector
  342. elif '**' in pat:
  343. raise ValueError("Invalid pattern: '**' can only be an entire path component")
  344. elif _is_wildcard_pattern(pat):
  345. cls = _WildcardSelector
  346. else:
  347. cls = _PreciseSelector
  348. return cls(pat, child_parts)
  349. if hasattr(functools, "lru_cache"):
  350. _make_selector = functools.lru_cache()(_make_selector)
  351. class _Selector:
  352. """A selector matches a specific glob pattern part against the children
  353. of a given path."""
  354. def __init__(self, child_parts):
  355. self.child_parts = child_parts
  356. if child_parts:
  357. self.successor = _make_selector(child_parts)
  358. else:
  359. self.successor = _TerminatingSelector()
  360. def select_from(self, parent_path):
  361. """Iterate over all child paths of `parent_path` matched by this
  362. selector. This can contain parent_path itself."""
  363. path_cls = type(parent_path)
  364. is_dir = path_cls.is_dir
  365. exists = path_cls.exists
  366. listdir = parent_path._accessor.listdir
  367. return self._select_from(parent_path, is_dir, exists, listdir)
  368. class _TerminatingSelector:
  369. def _select_from(self, parent_path, is_dir, exists, listdir):
  370. yield parent_path
  371. class _PreciseSelector(_Selector):
  372. def __init__(self, name, child_parts):
  373. self.name = name
  374. _Selector.__init__(self, child_parts)
  375. def _select_from(self, parent_path, is_dir, exists, listdir):
  376. if not is_dir(parent_path):
  377. return
  378. path = parent_path._make_child_relpath(self.name)
  379. if exists(path):
  380. for p in self.successor._select_from(path, is_dir, exists, listdir):
  381. yield p
  382. class _WildcardSelector(_Selector):
  383. def __init__(self, pat, child_parts):
  384. self.pat = re.compile(fnmatch.translate(pat))
  385. _Selector.__init__(self, child_parts)
  386. def _select_from(self, parent_path, is_dir, exists, listdir):
  387. if not is_dir(parent_path):
  388. return
  389. cf = parent_path._flavour.casefold
  390. for name in listdir(parent_path):
  391. casefolded = cf(name)
  392. if self.pat.match(casefolded):
  393. path = parent_path._make_child_relpath(name)
  394. for p in self.successor._select_from(path, is_dir, exists, listdir):
  395. yield p
  396. class _RecursiveWildcardSelector(_Selector):
  397. def __init__(self, pat, child_parts):
  398. _Selector.__init__(self, child_parts)
  399. def _iterate_directories(self, parent_path, is_dir, listdir):
  400. yield parent_path
  401. for name in listdir(parent_path):
  402. path = parent_path._make_child_relpath(name)
  403. if is_dir(path):
  404. for p in self._iterate_directories(path, is_dir, listdir):
  405. yield p
  406. def _select_from(self, parent_path, is_dir, exists, listdir):
  407. if not is_dir(parent_path):
  408. return
  409. with _cached(listdir) as listdir:
  410. yielded = set()
  411. try:
  412. successor_select = self.successor._select_from
  413. for starting_point in self._iterate_directories(parent_path, is_dir, listdir):
  414. for p in successor_select(starting_point, is_dir, exists, listdir):
  415. if p not in yielded:
  416. yield p
  417. yielded.add(p)
  418. finally:
  419. yielded.clear()
  420. #
  421. # Public API
  422. #
  423. class _PathParents(Sequence):
  424. """This object provides sequence-like access to the logical ancestors
  425. of a path. Don't try to construct it yourself."""
  426. __slots__ = ('_pathcls', '_drv', '_root', '_parts')
  427. def __init__(self, path):
  428. # We don't store the instance to avoid reference cycles
  429. self._pathcls = type(path)
  430. self._drv = path._drv
  431. self._root = path._root
  432. self._parts = path._parts
  433. def __len__(self):
  434. if self._drv or self._root:
  435. return len(self._parts) - 1
  436. else:
  437. return len(self._parts)
  438. def __getitem__(self, idx):
  439. if idx < 0 or idx >= len(self):
  440. raise IndexError(idx)
  441. return self._pathcls._from_parsed_parts(self._drv, self._root,
  442. self._parts[:-idx - 1])
  443. def __repr__(self):
  444. return "<{}.parents>".format(self._pathcls.__name__)
  445. class PurePath(object):
  446. """PurePath represents a filesystem path and offers operations which
  447. don't imply any actual filesystem I/O. Depending on your system,
  448. instantiating a PurePath will return either a PurePosixPath or a
  449. PureWindowsPath object. You can also instantiate either of these classes
  450. directly, regardless of your system.
  451. """
  452. __slots__ = (
  453. '_drv', '_root', '_parts',
  454. '_str', '_hash', '_pparts', '_cached_cparts',
  455. )
  456. def __new__(cls, *args):
  457. """Construct a PurePath from one or several strings and or existing
  458. PurePath objects. The strings and path objects are combined so as
  459. to yield a canonicalized path, which is incorporated into the
  460. new PurePath object.
  461. """
  462. if cls is PurePath:
  463. cls = PureWindowsPath if os.name == 'nt' else PurePosixPath
  464. return cls._from_parts(args)
  465. def __reduce__(self):
  466. # Using the parts tuple helps share interned path parts
  467. # when pickling related paths.
  468. return (self.__class__, tuple(self._parts))
  469. @classmethod
  470. def _parse_args(cls, args):
  471. # This is useful when you don't want to create an instance, just
  472. # canonicalize some constructor arguments.
  473. parts = []
  474. for a in args:
  475. if isinstance(a, PurePath):
  476. parts += a._parts
  477. elif isinstance(a, str):
  478. # Force-cast str subclasses to str (issue #21127)
  479. parts.append(str(a))
  480. else:
  481. raise TypeError(
  482. "argument should be a path or str object, not %r"
  483. % type(a))
  484. return cls._flavour.parse_parts(parts)
  485. @classmethod
  486. def _from_parts(cls, args, init=True):
  487. # We need to call _parse_args on the instance, so as to get the
  488. # right flavour.
  489. self = object.__new__(cls)
  490. drv, root, parts = self._parse_args(args)
  491. self._drv = drv
  492. self._root = root
  493. self._parts = parts
  494. if init:
  495. self._init()
  496. return self
  497. @classmethod
  498. def _from_parsed_parts(cls, drv, root, parts, init=True):
  499. self = object.__new__(cls)
  500. self._drv = drv
  501. self._root = root
  502. self._parts = parts
  503. if init:
  504. self._init()
  505. return self
  506. @classmethod
  507. def _format_parsed_parts(cls, drv, root, parts):
  508. if drv or root:
  509. return drv + root + cls._flavour.join(parts[1:])
  510. else:
  511. return cls._flavour.join(parts)
  512. def _init(self):
  513. # Overriden in concrete Path
  514. pass
  515. def _make_child(self, args):
  516. drv, root, parts = self._parse_args(args)
  517. drv, root, parts = self._flavour.join_parsed_parts(
  518. self._drv, self._root, self._parts, drv, root, parts)
  519. return self._from_parsed_parts(drv, root, parts)
  520. def __str__(self):
  521. """Return the string representation of the path, suitable for
  522. passing to system calls."""
  523. try:
  524. return self._str
  525. except AttributeError:
  526. self._str = self._format_parsed_parts(self._drv, self._root,
  527. self._parts) or '.'
  528. return self._str
  529. def as_posix(self):
  530. """Return the string representation of the path with forward (/)
  531. slashes."""
  532. f = self._flavour
  533. return str(self).replace(f.sep, '/')
  534. def __bytes__(self):
  535. """Return the bytes representation of the path. This is only
  536. recommended to use under Unix."""
  537. return os.fsencode(str(self))
  538. def __repr__(self):
  539. return "{}({!r})".format(self.__class__.__name__, self.as_posix())
  540. def as_uri(self):
  541. """Return the path as a 'file' URI."""
  542. if not self.is_absolute():
  543. raise ValueError("relative path can't be expressed as a file URI")
  544. return self._flavour.make_uri(self)
  545. @property
  546. def _cparts(self):
  547. # Cached casefolded parts, for hashing and comparison
  548. try:
  549. return self._cached_cparts
  550. except AttributeError:
  551. self._cached_cparts = self._flavour.casefold_parts(self._parts)
  552. return self._cached_cparts
  553. def __eq__(self, other):
  554. if not isinstance(other, PurePath):
  555. return NotImplemented
  556. return self._cparts == other._cparts and self._flavour is other._flavour
  557. def __ne__(self, other):
  558. return not self == other
  559. def __hash__(self):
  560. try:
  561. return self._hash
  562. except AttributeError:
  563. self._hash = hash(tuple(self._cparts))
  564. return self._hash
  565. def __lt__(self, other):
  566. if not isinstance(other, PurePath) or self._flavour is not other._flavour:
  567. return NotImplemented
  568. return self._cparts < other._cparts
  569. def __le__(self, other):
  570. if not isinstance(other, PurePath) or self._flavour is not other._flavour:
  571. return NotImplemented
  572. return self._cparts <= other._cparts
  573. def __gt__(self, other):
  574. if not isinstance(other, PurePath) or self._flavour is not other._flavour:
  575. return NotImplemented
  576. return self._cparts > other._cparts
  577. def __ge__(self, other):
  578. if not isinstance(other, PurePath) or self._flavour is not other._flavour:
  579. return NotImplemented
  580. return self._cparts >= other._cparts
  581. drive = property(attrgetter('_drv'),
  582. doc="""The drive prefix (letter or UNC path), if any.""")
  583. root = property(attrgetter('_root'),
  584. doc="""The root of the path, if any.""")
  585. @property
  586. def anchor(self):
  587. """The concatenation of the drive and root, or ''."""
  588. anchor = self._drv + self._root
  589. return anchor
  590. @property
  591. def name(self):
  592. """The final path component, if any."""
  593. parts = self._parts
  594. if len(parts) == (1 if (self._drv or self._root) else 0):
  595. return ''
  596. return parts[-1]
  597. @property
  598. def suffix(self):
  599. """The final component's last suffix, if any."""
  600. name = self.name
  601. i = name.rfind('.')
  602. if 0 < i < len(name) - 1:
  603. return name[i:]
  604. else:
  605. return ''
  606. @property
  607. def suffixes(self):
  608. """A list of the final component's suffixes, if any."""
  609. name = self.name
  610. if name.endswith('.'):
  611. return []
  612. name = name.lstrip('.')
  613. return ['.' + suffix for suffix in name.split('.')[1:]]
  614. @property
  615. def stem(self):
  616. """The final path component, minus its last suffix."""
  617. name = self.name
  618. i = name.rfind('.')
  619. if 0 < i < len(name) - 1:
  620. return name[:i]
  621. else:
  622. return name
  623. def with_name(self, name):
  624. """Return a new path with the file name changed."""
  625. if not self.name:
  626. raise ValueError("%r has an empty name" % (self,))
  627. drv, root, parts = self._flavour.parse_parts((name,))
  628. if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep]
  629. or drv or root or len(parts) != 1):
  630. raise ValueError("Invalid name %r" % (name))
  631. return self._from_parsed_parts(self._drv, self._root,
  632. self._parts[:-1] + [name])
  633. def with_suffix(self, suffix):
  634. """Return a new path with the file suffix changed (or added, if none)."""
  635. # XXX if suffix is None, should the current suffix be removed?
  636. f = self._flavour
  637. if f.sep in suffix or f.altsep and f.altsep in suffix:
  638. raise ValueError("Invalid suffix %r" % (suffix))
  639. if suffix and not suffix.startswith('.') or suffix == '.':
  640. raise ValueError("Invalid suffix %r" % (suffix))
  641. name = self.name
  642. if not name:
  643. raise ValueError("%r has an empty name" % (self,))
  644. old_suffix = self.suffix
  645. if not old_suffix:
  646. name = name + suffix
  647. else:
  648. name = name[:-len(old_suffix)] + suffix
  649. return self._from_parsed_parts(self._drv, self._root,
  650. self._parts[:-1] + [name])
  651. def relative_to(self, *other):
  652. """Return the relative path to another path identified by the passed
  653. arguments. If the operation is not possible (because this is not
  654. a subpath of the other path), raise ValueError.
  655. """
  656. # For the purpose of this method, drive and root are considered
  657. # separate parts, i.e.:
  658. # Path('c:/').relative_to('c:') gives Path('/')
  659. # Path('c:/').relative_to('/') raise ValueError
  660. if not other:
  661. raise TypeError("need at least one argument")
  662. parts = self._parts
  663. drv = self._drv
  664. root = self._root
  665. if root:
  666. abs_parts = [drv, root] + parts[1:]
  667. else:
  668. abs_parts = parts
  669. to_drv, to_root, to_parts = self._parse_args(other)
  670. if to_root:
  671. to_abs_parts = [to_drv, to_root] + to_parts[1:]
  672. else:
  673. to_abs_parts = to_parts
  674. n = len(to_abs_parts)
  675. cf = self._flavour.casefold_parts
  676. if (root or drv) if n == 0 else cf(abs_parts[:n]) != cf(to_abs_parts):
  677. formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
  678. raise ValueError("{!r} does not start with {!r}"
  679. .format(str(self), str(formatted)))
  680. return self._from_parsed_parts('', root if n == 1 else '',
  681. abs_parts[n:])
  682. @property
  683. def parts(self):
  684. """An object providing sequence-like access to the
  685. components in the filesystem path."""
  686. # We cache the tuple to avoid building a new one each time .parts
  687. # is accessed. XXX is this necessary?
  688. try:
  689. return self._pparts
  690. except AttributeError:
  691. self._pparts = tuple(self._parts)
  692. return self._pparts
  693. def joinpath(self, *args):
  694. """Combine this path with one or several arguments, and return a
  695. new path representing either a subpath (if all arguments are relative
  696. paths) or a totally different path (if one of the arguments is
  697. anchored).
  698. """
  699. return self._make_child(args)
  700. def __truediv__(self, key):
  701. return self._make_child((key,))
  702. def __rtruediv__(self, key):
  703. return self._from_parts([key] + self._parts)
  704. @property
  705. def parent(self):
  706. """The logical parent of the path."""
  707. drv = self._drv
  708. root = self._root
  709. parts = self._parts
  710. if len(parts) == 1 and (drv or root):
  711. return self
  712. return self._from_parsed_parts(drv, root, parts[:-1])
  713. @property
  714. def parents(self):
  715. """A sequence of this path's logical parents."""
  716. return _PathParents(self)
  717. def is_absolute(self):
  718. """True if the path is absolute (has both a root and, if applicable,
  719. a drive)."""
  720. if not self._root:
  721. return False
  722. return not self._flavour.has_drv or bool(self._drv)
  723. def is_reserved(self):
  724. """Return True if the path contains one of the special names reserved
  725. by the system, if any."""
  726. return self._flavour.is_reserved(self._parts)
  727. def match(self, path_pattern):
  728. """
  729. Return True if this path matches the given pattern.
  730. """
  731. cf = self._flavour.casefold
  732. path_pattern = cf(path_pattern)
  733. drv, root, pat_parts = self._flavour.parse_parts((path_pattern,))
  734. if not pat_parts:
  735. raise ValueError("empty pattern")
  736. if drv and drv != cf(self._drv):
  737. return False
  738. if root and root != cf(self._root):
  739. return False
  740. parts = self._cparts
  741. if drv or root:
  742. if len(pat_parts) != len(parts):
  743. return False
  744. pat_parts = pat_parts[1:]
  745. elif len(pat_parts) > len(parts):
  746. return False
  747. for part, pat in zip(reversed(parts), reversed(pat_parts)):
  748. if not fnmatch.fnmatchcase(part, pat):
  749. return False
  750. return True
  751. class PurePosixPath(PurePath):
  752. _flavour = _posix_flavour
  753. __slots__ = ()
  754. class PureWindowsPath(PurePath):
  755. _flavour = _windows_flavour
  756. __slots__ = ()
  757. # Filesystem-accessing classes
  758. class Path(PurePath):
  759. __slots__ = (
  760. '_accessor',
  761. '_closed',
  762. )
  763. def __new__(cls, *args, **kwargs):
  764. if cls is Path:
  765. cls = WindowsPath if os.name == 'nt' else PosixPath
  766. self = cls._from_parts(args, init=False)
  767. if not self._flavour.is_supported:
  768. raise NotImplementedError("cannot instantiate %r on your system"
  769. % (cls.__name__,))
  770. self._init()
  771. return self
  772. def _init(self,
  773. # Private non-constructor arguments
  774. template=None,
  775. ):
  776. self._closed = False
  777. if template is not None:
  778. self._accessor = template._accessor
  779. else:
  780. self._accessor = _normal_accessor
  781. def _make_child_relpath(self, part):
  782. # This is an optimization used for dir walking. `part` must be
  783. # a single part relative to this path.
  784. parts = self._parts + [part]
  785. return self._from_parsed_parts(self._drv, self._root, parts)
  786. def __enter__(self):
  787. if self._closed:
  788. self._raise_closed()
  789. return self
  790. def __exit__(self, t, v, tb):
  791. self._closed = True
  792. def _raise_closed(self):
  793. raise ValueError("I/O operation on closed path")
  794. def _opener(self, name, flags, mode=0o666):
  795. # A stub for the opener argument to built-in open()
  796. return self._accessor.open(self, flags, mode)
  797. def _raw_open(self, flags, mode=0o777):
  798. """
  799. Open the file pointed by this path and return a file descriptor,
  800. as os.open() does.
  801. """
  802. if self._closed:
  803. self._raise_closed()
  804. return self._accessor.open(self, flags, mode)
  805. # Public API
  806. @classmethod
  807. def cwd(cls):
  808. """Return a new path pointing to the current working directory
  809. (as returned by os.getcwd()).
  810. """
  811. return cls(os.getcwd())
  812. def samefile(self, other_path):
  813. """Return whether `other_file` is the same or not as this file.
  814. (as returned by os.path.samefile(file, other_file)).
  815. """
  816. st = self.stat()
  817. try:
  818. other_st = other_path.stat()
  819. except AttributeError:
  820. other_st = os.stat(other_path)
  821. return os.path.samestat(st, other_st)
  822. def iterdir(self):
  823. """Iterate over the files in this directory. Does not yield any
  824. result for the special paths '.' and '..'.
  825. """
  826. if self._closed:
  827. self._raise_closed()
  828. for name in self._accessor.listdir(self):
  829. if name in {'.', '..'}:
  830. # Yielding a path object for these makes little sense
  831. continue
  832. yield self._make_child_relpath(name)
  833. if self._closed:
  834. self._raise_closed()
  835. def glob(self, pattern):
  836. """Iterate over this subtree and yield all existing files (of any
  837. kind, including directories) matching the given pattern.
  838. """
  839. pattern = self._flavour.casefold(pattern)
  840. drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
  841. if drv or root:
  842. raise NotImplementedError("Non-relative patterns are unsupported")
  843. selector = _make_selector(tuple(pattern_parts))
  844. for p in selector.select_from(self):
  845. yield p
  846. def rglob(self, pattern):
  847. """Recursively yield all existing files (of any kind, including
  848. directories) matching the given pattern, anywhere in this subtree.
  849. """
  850. pattern = self._flavour.casefold(pattern)
  851. drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
  852. if drv or root:
  853. raise NotImplementedError("Non-relative patterns are unsupported")
  854. selector = _make_selector(("**",) + tuple(pattern_parts))
  855. for p in selector.select_from(self):
  856. yield p
  857. def absolute(self):
  858. """Return an absolute version of this path. This function works
  859. even if the path doesn't point to anything.
  860. No normalization is done, i.e. all '.' and '..' will be kept along.
  861. Use resolve() to get the canonical path to a file.
  862. """
  863. # XXX untested yet!
  864. if self._closed:
  865. self._raise_closed()
  866. if self.is_absolute():
  867. return self
  868. # FIXME this must defer to the specific flavour (and, under Windows,
  869. # use nt._getfullpathname())
  870. obj = self._from_parts([os.getcwd()] + self._parts, init=False)
  871. obj._init(template=self)
  872. return obj
  873. def resolve(self):
  874. """
  875. Make the path absolute, resolving all symlinks on the way and also
  876. normalizing it (for example turning slashes into backslashes under
  877. Windows).
  878. """
  879. if self._closed:
  880. self._raise_closed()
  881. s = self._flavour.resolve(self)
  882. if s is None:
  883. # No symlink resolution => for consistency, raise an error if
  884. # the path doesn't exist or is forbidden
  885. self.stat()
  886. s = str(self.absolute())
  887. # Now we have no symlinks in the path, it's safe to normalize it.
  888. normed = self._flavour.pathmod.normpath(s)
  889. obj = self._from_parts((normed,), init=False)
  890. obj._init(template=self)
  891. return obj
  892. def stat(self):
  893. """
  894. Return the result of the stat() system call on this path, like
  895. os.stat() does.
  896. """
  897. return self._accessor.stat(self)
  898. def owner(self):
  899. """
  900. Return the login name of the file owner.
  901. """
  902. import pwd
  903. return pwd.getpwuid(self.stat().st_uid).pw_name
  904. def group(self):
  905. """
  906. Return the group name of the file gid.
  907. """
  908. import grp
  909. return grp.getgrgid(self.stat().st_gid).gr_name
  910. def open(self, mode='r', buffering=-1, encoding=None,
  911. errors=None, newline=None):
  912. """
  913. Open the file pointed by this path and return a file object, as
  914. the built-in open() function does.
  915. """
  916. if self._closed:
  917. self._raise_closed()
  918. return io.open(str(self), mode, buffering, encoding, errors, newline,
  919. opener=self._opener)
  920. def read_bytes(self):
  921. """
  922. Open the file in bytes mode, read it, and close the file.
  923. """
  924. with self.open(mode='rb') as f:
  925. return f.read()
  926. def read_text(self, encoding=None, errors=None):
  927. """
  928. Open the file in text mode, read it, and close the file.
  929. """
  930. with self.open(mode='r', encoding=encoding, errors=errors) as f:
  931. return f.read()
  932. def write_bytes(self, data):
  933. """
  934. Open the file in bytes mode, write to it, and close the file.
  935. """
  936. # type-check for the buffer interface before truncating the file
  937. view = memoryview(data)
  938. with self.open(mode='wb') as f:
  939. return f.write(view)
  940. def write_text(self, data, encoding=None, errors=None):
  941. """
  942. Open the file in text mode, write to it, and close the file.
  943. """
  944. if not isinstance(data, str):
  945. raise TypeError('data must be str, not %s' %
  946. data.__class__.__name__)
  947. with self.open(mode='w', encoding=encoding, errors=errors) as f:
  948. return f.write(data)
  949. def touch(self, mode=0o666, exist_ok=True):
  950. """
  951. Create this file with the given access mode, if it doesn't exist.
  952. """
  953. if self._closed:
  954. self._raise_closed()
  955. if exist_ok:
  956. # First try to bump modification time
  957. # Implementation note: GNU touch uses the UTIME_NOW option of
  958. # the utimensat() / futimens() functions.
  959. try:
  960. self._accessor.utime(self, None)
  961. except OSError:
  962. # Avoid exception chaining
  963. pass
  964. else:
  965. return
  966. flags = os.O_CREAT | os.O_WRONLY
  967. if not exist_ok:
  968. flags |= os.O_EXCL
  969. fd = self._raw_open(flags, mode)
  970. os.close(fd)
  971. def mkdir(self, mode=0o777, parents=False, exist_ok=False):
  972. if self._closed:
  973. self._raise_closed()
  974. if not parents:
  975. try:
  976. self._accessor.mkdir(self, mode)
  977. except FileExistsError:
  978. if not exist_ok or not self.is_dir():
  979. raise
  980. else:
  981. try:
  982. self._accessor.mkdir(self, mode)
  983. except FileExistsError:
  984. if not exist_ok or not self.is_dir():
  985. raise
  986. except OSError as e:
  987. if e.errno != ENOENT:
  988. raise
  989. self.parent.mkdir(parents=True)
  990. self._accessor.mkdir(self, mode)
  991. def chmod(self, mode):
  992. """
  993. Change the permissions of the path, like os.chmod().
  994. """
  995. if self._closed:
  996. self._raise_closed()
  997. self._accessor.chmod(self, mode)
  998. def lchmod(self, mode):
  999. """
  1000. Like chmod(), except if the path points to a symlink, the symlink's
  1001. permissions are changed, rather than its target's.
  1002. """
  1003. if self._closed:
  1004. self._raise_closed()
  1005. self._accessor.lchmod(self, mode)
  1006. def unlink(self):
  1007. """
  1008. Remove this file or link.
  1009. If the path is a directory, use rmdir() instead.
  1010. """
  1011. if self._closed:
  1012. self._raise_closed()
  1013. self._accessor.unlink(self)
  1014. def rmdir(self):
  1015. """
  1016. Remove this directory. The directory must be empty.
  1017. """
  1018. if self._closed:
  1019. self._raise_closed()
  1020. self._accessor.rmdir(self)
  1021. def lstat(self):
  1022. """
  1023. Like stat(), except if the path points to a symlink, the symlink's
  1024. status information is returned, rather than its target's.
  1025. """
  1026. if self._closed:
  1027. self._raise_closed()
  1028. return self._accessor.lstat(self)
  1029. def rename(self, target):
  1030. """
  1031. Rename this path to the given path.
  1032. """
  1033. if self._closed:
  1034. self._raise_closed()
  1035. self._accessor.rename(self, target)
  1036. def replace(self, target):
  1037. """
  1038. Rename this path to the given path, clobbering the existing
  1039. destination if it exists.
  1040. """
  1041. if self._closed:
  1042. self._raise_closed()
  1043. self._accessor.replace(self, target)
  1044. def symlink_to(self, target, target_is_directory=False):
  1045. """
  1046. Make this path a symlink pointing to the given path.
  1047. Note the order of arguments (self, target) is the reverse of os.symlink's.
  1048. """
  1049. if self._closed:
  1050. self._raise_closed()
  1051. self._accessor.symlink(target, self, target_is_directory)
  1052. # Convenience functions for querying the stat results
  1053. def exists(self):
  1054. """
  1055. Whether this path exists.
  1056. """
  1057. try:
  1058. self.stat()
  1059. except OSError as e:
  1060. if e.errno not in (ENOENT, ENOTDIR):
  1061. raise
  1062. return False
  1063. return True
  1064. def is_dir(self):
  1065. """
  1066. Whether this path is a directory.
  1067. """
  1068. try:
  1069. return S_ISDIR(self.stat().st_mode)
  1070. except OSError as e:
  1071. if e.errno not in (ENOENT, ENOTDIR):
  1072. raise
  1073. # Path doesn't exist or is a broken symlink
  1074. # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
  1075. return False
  1076. def is_file(self):
  1077. """
  1078. Whether this path is a regular file (also True for symlinks pointing
  1079. to regular files).
  1080. """
  1081. try:
  1082. return S_ISREG(self.stat().st_mode)
  1083. except OSError as e:
  1084. if e.errno not in (ENOENT, ENOTDIR):
  1085. raise
  1086. # Path doesn't exist or is a broken symlink
  1087. # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
  1088. return False
  1089. def is_symlink(self):
  1090. """
  1091. Whether this path is a symbolic link.
  1092. """
  1093. try:
  1094. return S_ISLNK(self.lstat().st_mode)
  1095. except OSError as e:
  1096. if e.errno not in (ENOENT, ENOTDIR):
  1097. raise
  1098. # Path doesn't exist
  1099. return False
  1100. def is_block_device(self):
  1101. """
  1102. Whether this path is a block device.
  1103. """
  1104. try:
  1105. return S_ISBLK(self.stat().st_mode)
  1106. except OSError as e:
  1107. if e.errno not in (ENOENT, ENOTDIR):
  1108. raise
  1109. # Path doesn't exist or is a broken symlink
  1110. # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
  1111. return False
  1112. def is_char_device(self):
  1113. """
  1114. Whether this path is a character device.
  1115. """
  1116. try:
  1117. return S_ISCHR(self.stat().st_mode)
  1118. except OSError as e:
  1119. if e.errno not in (ENOENT, ENOTDIR):
  1120. raise
  1121. # Path doesn't exist or is a broken symlink
  1122. # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
  1123. return False
  1124. def is_fifo(self):
  1125. """
  1126. Whether this path is a FIFO.
  1127. """
  1128. try:
  1129. return S_ISFIFO(self.stat().st_mode)
  1130. except OSError as e:
  1131. if e.errno not in (ENOENT, ENOTDIR):
  1132. raise
  1133. # Path doesn't exist or is a broken symlink
  1134. # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
  1135. return False
  1136. def is_socket(self):
  1137. """
  1138. Whether this path is a socket.
  1139. """
  1140. try:
  1141. return S_ISSOCK(self.stat().st_mode)
  1142. except OSError as e:
  1143. if e.errno not in (ENOENT, ENOTDIR):
  1144. raise
  1145. # Path doesn't exist or is a broken symlink
  1146. # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
  1147. return False
  1148. class PosixPath(Path, PurePosixPath):
  1149. __slots__ = ()
  1150. class WindowsPath(Path, PureWindowsPath):
  1151. __slots__ = ()