path.py 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. """local path implementation."""
  2. from __future__ import annotations
  3. import atexit
  4. import fnmatch
  5. import importlib.util
  6. import io
  7. import os
  8. import posixpath
  9. import sys
  10. import uuid
  11. import warnings
  12. from contextlib import contextmanager
  13. from os.path import abspath
  14. from os.path import dirname
  15. from os.path import exists
  16. from os.path import isabs
  17. from os.path import isdir
  18. from os.path import isfile
  19. from os.path import islink
  20. from os.path import normpath
  21. from stat import S_ISDIR
  22. from stat import S_ISLNK
  23. from stat import S_ISREG
  24. from typing import Any
  25. from typing import Callable
  26. from typing import cast
  27. from typing import overload
  28. from typing import TYPE_CHECKING
  29. from . import error
  30. if TYPE_CHECKING:
  31. from typing import Literal
  32. # Moved from local.py.
  33. iswin32 = sys.platform == "win32" or (getattr(os, "_name", False) == "nt")
  34. class Checkers:
  35. _depend_on_existence = "exists", "link", "dir", "file"
  36. def __init__(self, path):
  37. self.path = path
  38. def dotfile(self):
  39. return self.path.basename.startswith(".")
  40. def ext(self, arg):
  41. if not arg.startswith("."):
  42. arg = "." + arg
  43. return self.path.ext == arg
  44. def basename(self, arg):
  45. return self.path.basename == arg
  46. def basestarts(self, arg):
  47. return self.path.basename.startswith(arg)
  48. def relto(self, arg):
  49. return self.path.relto(arg)
  50. def fnmatch(self, arg):
  51. return self.path.fnmatch(arg)
  52. def endswith(self, arg):
  53. return str(self.path).endswith(arg)
  54. def _evaluate(self, kw):
  55. from .._code.source import getrawcode
  56. for name, value in kw.items():
  57. invert = False
  58. meth = None
  59. try:
  60. meth = getattr(self, name)
  61. except AttributeError:
  62. if name[:3] == "not":
  63. invert = True
  64. try:
  65. meth = getattr(self, name[3:])
  66. except AttributeError:
  67. pass
  68. if meth is None:
  69. raise TypeError(f"no {name!r} checker available for {self.path!r}")
  70. try:
  71. if getrawcode(meth).co_argcount > 1:
  72. if (not meth(value)) ^ invert:
  73. return False
  74. else:
  75. if bool(value) ^ bool(meth()) ^ invert:
  76. return False
  77. except (error.ENOENT, error.ENOTDIR, error.EBUSY):
  78. # EBUSY feels not entirely correct,
  79. # but its kind of necessary since ENOMEDIUM
  80. # is not accessible in python
  81. for name in self._depend_on_existence:
  82. if name in kw:
  83. if kw.get(name):
  84. return False
  85. name = "not" + name
  86. if name in kw:
  87. if not kw.get(name):
  88. return False
  89. return True
  90. _statcache: Stat
  91. def _stat(self) -> Stat:
  92. try:
  93. return self._statcache
  94. except AttributeError:
  95. try:
  96. self._statcache = self.path.stat()
  97. except error.ELOOP:
  98. self._statcache = self.path.lstat()
  99. return self._statcache
  100. def dir(self):
  101. return S_ISDIR(self._stat().mode)
  102. def file(self):
  103. return S_ISREG(self._stat().mode)
  104. def exists(self):
  105. return self._stat()
  106. def link(self):
  107. st = self.path.lstat()
  108. return S_ISLNK(st.mode)
  109. class NeverRaised(Exception):
  110. pass
  111. class Visitor:
  112. def __init__(self, fil, rec, ignore, bf, sort):
  113. if isinstance(fil, str):
  114. fil = FNMatcher(fil)
  115. if isinstance(rec, str):
  116. self.rec: Callable[[LocalPath], bool] = FNMatcher(rec)
  117. elif not hasattr(rec, "__call__") and rec:
  118. self.rec = lambda path: True
  119. else:
  120. self.rec = rec
  121. self.fil = fil
  122. self.ignore = ignore
  123. self.breadthfirst = bf
  124. self.optsort = cast(Callable[[Any], Any], sorted) if sort else (lambda x: x)
  125. def gen(self, path):
  126. try:
  127. entries = path.listdir()
  128. except self.ignore:
  129. return
  130. rec = self.rec
  131. dirs = self.optsort(
  132. [p for p in entries if p.check(dir=1) and (rec is None or rec(p))]
  133. )
  134. if not self.breadthfirst:
  135. for subdir in dirs:
  136. for p in self.gen(subdir):
  137. yield p
  138. for p in self.optsort(entries):
  139. if self.fil is None or self.fil(p):
  140. yield p
  141. if self.breadthfirst:
  142. for subdir in dirs:
  143. for p in self.gen(subdir):
  144. yield p
  145. class FNMatcher:
  146. def __init__(self, pattern):
  147. self.pattern = pattern
  148. def __call__(self, path):
  149. pattern = self.pattern
  150. if (
  151. pattern.find(path.sep) == -1
  152. and iswin32
  153. and pattern.find(posixpath.sep) != -1
  154. ):
  155. # Running on Windows, the pattern has no Windows path separators,
  156. # and the pattern has one or more Posix path separators. Replace
  157. # the Posix path separators with the Windows path separator.
  158. pattern = pattern.replace(posixpath.sep, path.sep)
  159. if pattern.find(path.sep) == -1:
  160. name = path.basename
  161. else:
  162. name = str(path) # path.strpath # XXX svn?
  163. if not os.path.isabs(pattern):
  164. pattern = "*" + path.sep + pattern
  165. return fnmatch.fnmatch(name, pattern)
  166. def map_as_list(func, iter):
  167. return list(map(func, iter))
  168. class Stat:
  169. if TYPE_CHECKING:
  170. @property
  171. def size(self) -> int:
  172. ...
  173. @property
  174. def mtime(self) -> float:
  175. ...
  176. def __getattr__(self, name: str) -> Any:
  177. return getattr(self._osstatresult, "st_" + name)
  178. def __init__(self, path, osstatresult):
  179. self.path = path
  180. self._osstatresult = osstatresult
  181. @property
  182. def owner(self):
  183. if iswin32:
  184. raise NotImplementedError("XXX win32")
  185. import pwd
  186. entry = error.checked_call(pwd.getpwuid, self.uid) # type:ignore[attr-defined]
  187. return entry[0]
  188. @property
  189. def group(self):
  190. """Return group name of file."""
  191. if iswin32:
  192. raise NotImplementedError("XXX win32")
  193. import grp
  194. entry = error.checked_call(grp.getgrgid, self.gid) # type:ignore[attr-defined]
  195. return entry[0]
  196. def isdir(self):
  197. return S_ISDIR(self._osstatresult.st_mode)
  198. def isfile(self):
  199. return S_ISREG(self._osstatresult.st_mode)
  200. def islink(self):
  201. self.path.lstat()
  202. return S_ISLNK(self._osstatresult.st_mode)
  203. def getuserid(user):
  204. import pwd
  205. if not isinstance(user, int):
  206. user = pwd.getpwnam(user)[2] # type:ignore[attr-defined]
  207. return user
  208. def getgroupid(group):
  209. import grp
  210. if not isinstance(group, int):
  211. group = grp.getgrnam(group)[2] # type:ignore[attr-defined]
  212. return group
  213. class LocalPath:
  214. """Object oriented interface to os.path and other local filesystem
  215. related information.
  216. """
  217. class ImportMismatchError(ImportError):
  218. """raised on pyimport() if there is a mismatch of __file__'s"""
  219. sep = os.sep
  220. def __init__(self, path=None, expanduser=False):
  221. """Initialize and return a local Path instance.
  222. Path can be relative to the current directory.
  223. If path is None it defaults to the current working directory.
  224. If expanduser is True, tilde-expansion is performed.
  225. Note that Path instances always carry an absolute path.
  226. Note also that passing in a local path object will simply return
  227. the exact same path object. Use new() to get a new copy.
  228. """
  229. if path is None:
  230. self.strpath = error.checked_call(os.getcwd)
  231. else:
  232. try:
  233. path = os.fspath(path)
  234. except TypeError:
  235. raise ValueError(
  236. "can only pass None, Path instances "
  237. "or non-empty strings to LocalPath"
  238. )
  239. if expanduser:
  240. path = os.path.expanduser(path)
  241. self.strpath = abspath(path)
  242. if sys.platform != "win32":
  243. def chown(self, user, group, rec=0):
  244. """Change ownership to the given user and group.
  245. user and group may be specified by a number or
  246. by a name. if rec is True change ownership
  247. recursively.
  248. """
  249. uid = getuserid(user)
  250. gid = getgroupid(group)
  251. if rec:
  252. for x in self.visit(rec=lambda x: x.check(link=0)):
  253. if x.check(link=0):
  254. error.checked_call(os.chown, str(x), uid, gid)
  255. error.checked_call(os.chown, str(self), uid, gid)
  256. def readlink(self) -> str:
  257. """Return value of a symbolic link."""
  258. # https://github.com/python/mypy/issues/12278
  259. return error.checked_call(os.readlink, self.strpath) # type: ignore[arg-type,return-value]
  260. def mklinkto(self, oldname):
  261. """Posix style hard link to another name."""
  262. error.checked_call(os.link, str(oldname), str(self))
  263. def mksymlinkto(self, value, absolute=1):
  264. """Create a symbolic link with the given value (pointing to another name)."""
  265. if absolute:
  266. error.checked_call(os.symlink, str(value), self.strpath)
  267. else:
  268. base = self.common(value)
  269. # with posix local paths '/' is always a common base
  270. relsource = self.__class__(value).relto(base)
  271. reldest = self.relto(base)
  272. n = reldest.count(self.sep)
  273. target = self.sep.join(("..",) * n + (relsource,))
  274. error.checked_call(os.symlink, target, self.strpath)
  275. def __div__(self, other):
  276. return self.join(os.fspath(other))
  277. __truediv__ = __div__ # py3k
  278. @property
  279. def basename(self):
  280. """Basename part of path."""
  281. return self._getbyspec("basename")[0]
  282. @property
  283. def dirname(self):
  284. """Dirname part of path."""
  285. return self._getbyspec("dirname")[0]
  286. @property
  287. def purebasename(self):
  288. """Pure base name of the path."""
  289. return self._getbyspec("purebasename")[0]
  290. @property
  291. def ext(self):
  292. """Extension of the path (including the '.')."""
  293. return self._getbyspec("ext")[0]
  294. def read_binary(self):
  295. """Read and return a bytestring from reading the path."""
  296. with self.open("rb") as f:
  297. return f.read()
  298. def read_text(self, encoding):
  299. """Read and return a Unicode string from reading the path."""
  300. with self.open("r", encoding=encoding) as f:
  301. return f.read()
  302. def read(self, mode="r"):
  303. """Read and return a bytestring from reading the path."""
  304. with self.open(mode) as f:
  305. return f.read()
  306. def readlines(self, cr=1):
  307. """Read and return a list of lines from the path. if cr is False, the
  308. newline will be removed from the end of each line."""
  309. mode = "r"
  310. if not cr:
  311. content = self.read(mode)
  312. return content.split("\n")
  313. else:
  314. f = self.open(mode)
  315. try:
  316. return f.readlines()
  317. finally:
  318. f.close()
  319. def load(self):
  320. """(deprecated) return object unpickled from self.read()"""
  321. f = self.open("rb")
  322. try:
  323. import pickle
  324. return error.checked_call(pickle.load, f)
  325. finally:
  326. f.close()
  327. def move(self, target):
  328. """Move this path to target."""
  329. if target.relto(self):
  330. raise error.EINVAL(target, "cannot move path into a subdirectory of itself")
  331. try:
  332. self.rename(target)
  333. except error.EXDEV: # invalid cross-device link
  334. self.copy(target)
  335. self.remove()
  336. def fnmatch(self, pattern):
  337. """Return true if the basename/fullname matches the glob-'pattern'.
  338. valid pattern characters::
  339. * matches everything
  340. ? matches any single character
  341. [seq] matches any character in seq
  342. [!seq] matches any char not in seq
  343. If the pattern contains a path-separator then the full path
  344. is used for pattern matching and a '*' is prepended to the
  345. pattern.
  346. if the pattern doesn't contain a path-separator the pattern
  347. is only matched against the basename.
  348. """
  349. return FNMatcher(pattern)(self)
  350. def relto(self, relpath):
  351. """Return a string which is the relative part of the path
  352. to the given 'relpath'.
  353. """
  354. if not isinstance(relpath, (str, LocalPath)):
  355. raise TypeError(f"{relpath!r}: not a string or path object")
  356. strrelpath = str(relpath)
  357. if strrelpath and strrelpath[-1] != self.sep:
  358. strrelpath += self.sep
  359. # assert strrelpath[-1] == self.sep
  360. # assert strrelpath[-2] != self.sep
  361. strself = self.strpath
  362. if sys.platform == "win32" or getattr(os, "_name", None) == "nt":
  363. if os.path.normcase(strself).startswith(os.path.normcase(strrelpath)):
  364. return strself[len(strrelpath) :]
  365. elif strself.startswith(strrelpath):
  366. return strself[len(strrelpath) :]
  367. return ""
  368. def ensure_dir(self, *args):
  369. """Ensure the path joined with args is a directory."""
  370. return self.ensure(*args, **{"dir": True})
  371. def bestrelpath(self, dest):
  372. """Return a string which is a relative path from self
  373. (assumed to be a directory) to dest such that
  374. self.join(bestrelpath) == dest and if not such
  375. path can be determined return dest.
  376. """
  377. try:
  378. if self == dest:
  379. return os.curdir
  380. base = self.common(dest)
  381. if not base: # can be the case on windows
  382. return str(dest)
  383. self2base = self.relto(base)
  384. reldest = dest.relto(base)
  385. if self2base:
  386. n = self2base.count(self.sep) + 1
  387. else:
  388. n = 0
  389. lst = [os.pardir] * n
  390. if reldest:
  391. lst.append(reldest)
  392. target = dest.sep.join(lst)
  393. return target
  394. except AttributeError:
  395. return str(dest)
  396. def exists(self):
  397. return self.check()
  398. def isdir(self):
  399. return self.check(dir=1)
  400. def isfile(self):
  401. return self.check(file=1)
  402. def parts(self, reverse=False):
  403. """Return a root-first list of all ancestor directories
  404. plus the path itself.
  405. """
  406. current = self
  407. lst = [self]
  408. while 1:
  409. last = current
  410. current = current.dirpath()
  411. if last == current:
  412. break
  413. lst.append(current)
  414. if not reverse:
  415. lst.reverse()
  416. return lst
  417. def common(self, other):
  418. """Return the common part shared with the other path
  419. or None if there is no common part.
  420. """
  421. last = None
  422. for x, y in zip(self.parts(), other.parts()):
  423. if x != y:
  424. return last
  425. last = x
  426. return last
  427. def __add__(self, other):
  428. """Return new path object with 'other' added to the basename"""
  429. return self.new(basename=self.basename + str(other))
  430. def visit(self, fil=None, rec=None, ignore=NeverRaised, bf=False, sort=False):
  431. """Yields all paths below the current one
  432. fil is a filter (glob pattern or callable), if not matching the
  433. path will not be yielded, defaulting to None (everything is
  434. returned)
  435. rec is a filter (glob pattern or callable) that controls whether
  436. a node is descended, defaulting to None
  437. ignore is an Exception class that is ignoredwhen calling dirlist()
  438. on any of the paths (by default, all exceptions are reported)
  439. bf if True will cause a breadthfirst search instead of the
  440. default depthfirst. Default: False
  441. sort if True will sort entries within each directory level.
  442. """
  443. yield from Visitor(fil, rec, ignore, bf, sort).gen(self)
  444. def _sortlist(self, res, sort):
  445. if sort:
  446. if hasattr(sort, "__call__"):
  447. warnings.warn(
  448. DeprecationWarning(
  449. "listdir(sort=callable) is deprecated and breaks on python3"
  450. ),
  451. stacklevel=3,
  452. )
  453. res.sort(sort)
  454. else:
  455. res.sort()
  456. def __fspath__(self):
  457. return self.strpath
  458. def __hash__(self):
  459. s = self.strpath
  460. if iswin32:
  461. s = s.lower()
  462. return hash(s)
  463. def __eq__(self, other):
  464. s1 = os.fspath(self)
  465. try:
  466. s2 = os.fspath(other)
  467. except TypeError:
  468. return False
  469. if iswin32:
  470. s1 = s1.lower()
  471. try:
  472. s2 = s2.lower()
  473. except AttributeError:
  474. return False
  475. return s1 == s2
  476. def __ne__(self, other):
  477. return not (self == other)
  478. def __lt__(self, other):
  479. return os.fspath(self) < os.fspath(other)
  480. def __gt__(self, other):
  481. return os.fspath(self) > os.fspath(other)
  482. def samefile(self, other):
  483. """Return True if 'other' references the same file as 'self'."""
  484. other = os.fspath(other)
  485. if not isabs(other):
  486. other = abspath(other)
  487. if self == other:
  488. return True
  489. if not hasattr(os.path, "samefile"):
  490. return False
  491. return error.checked_call(os.path.samefile, self.strpath, other)
  492. def remove(self, rec=1, ignore_errors=False):
  493. """Remove a file or directory (or a directory tree if rec=1).
  494. if ignore_errors is True, errors while removing directories will
  495. be ignored.
  496. """
  497. if self.check(dir=1, link=0):
  498. if rec:
  499. # force remove of readonly files on windows
  500. if iswin32:
  501. self.chmod(0o700, rec=1)
  502. import shutil
  503. error.checked_call(
  504. shutil.rmtree, self.strpath, ignore_errors=ignore_errors
  505. )
  506. else:
  507. error.checked_call(os.rmdir, self.strpath)
  508. else:
  509. if iswin32:
  510. self.chmod(0o700)
  511. error.checked_call(os.remove, self.strpath)
  512. def computehash(self, hashtype="md5", chunksize=524288):
  513. """Return hexdigest of hashvalue for this file."""
  514. try:
  515. try:
  516. import hashlib as mod
  517. except ImportError:
  518. if hashtype == "sha1":
  519. hashtype = "sha"
  520. mod = __import__(hashtype)
  521. hash = getattr(mod, hashtype)()
  522. except (AttributeError, ImportError):
  523. raise ValueError(f"Don't know how to compute {hashtype!r} hash")
  524. f = self.open("rb")
  525. try:
  526. while 1:
  527. buf = f.read(chunksize)
  528. if not buf:
  529. return hash.hexdigest()
  530. hash.update(buf)
  531. finally:
  532. f.close()
  533. def new(self, **kw):
  534. """Create a modified version of this path.
  535. the following keyword arguments modify various path parts::
  536. a:/some/path/to/a/file.ext
  537. xx drive
  538. xxxxxxxxxxxxxxxxx dirname
  539. xxxxxxxx basename
  540. xxxx purebasename
  541. xxx ext
  542. """
  543. obj = object.__new__(self.__class__)
  544. if not kw:
  545. obj.strpath = self.strpath
  546. return obj
  547. drive, dirname, basename, purebasename, ext = self._getbyspec(
  548. "drive,dirname,basename,purebasename,ext"
  549. )
  550. if "basename" in kw:
  551. if "purebasename" in kw or "ext" in kw:
  552. raise ValueError("invalid specification %r" % kw)
  553. else:
  554. pb = kw.setdefault("purebasename", purebasename)
  555. try:
  556. ext = kw["ext"]
  557. except KeyError:
  558. pass
  559. else:
  560. if ext and not ext.startswith("."):
  561. ext = "." + ext
  562. kw["basename"] = pb + ext
  563. if "dirname" in kw and not kw["dirname"]:
  564. kw["dirname"] = drive
  565. else:
  566. kw.setdefault("dirname", dirname)
  567. kw.setdefault("sep", self.sep)
  568. obj.strpath = normpath("%(dirname)s%(sep)s%(basename)s" % kw)
  569. return obj
  570. def _getbyspec(self, spec: str) -> list[str]:
  571. """See new for what 'spec' can be."""
  572. res = []
  573. parts = self.strpath.split(self.sep)
  574. args = filter(None, spec.split(","))
  575. for name in args:
  576. if name == "drive":
  577. res.append(parts[0])
  578. elif name == "dirname":
  579. res.append(self.sep.join(parts[:-1]))
  580. else:
  581. basename = parts[-1]
  582. if name == "basename":
  583. res.append(basename)
  584. else:
  585. i = basename.rfind(".")
  586. if i == -1:
  587. purebasename, ext = basename, ""
  588. else:
  589. purebasename, ext = basename[:i], basename[i:]
  590. if name == "purebasename":
  591. res.append(purebasename)
  592. elif name == "ext":
  593. res.append(ext)
  594. else:
  595. raise ValueError("invalid part specification %r" % name)
  596. return res
  597. def dirpath(self, *args, **kwargs):
  598. """Return the directory path joined with any given path arguments."""
  599. if not kwargs:
  600. path = object.__new__(self.__class__)
  601. path.strpath = dirname(self.strpath)
  602. if args:
  603. path = path.join(*args)
  604. return path
  605. return self.new(basename="").join(*args, **kwargs)
  606. def join(self, *args: os.PathLike[str], abs: bool = False) -> LocalPath:
  607. """Return a new path by appending all 'args' as path
  608. components. if abs=1 is used restart from root if any
  609. of the args is an absolute path.
  610. """
  611. sep = self.sep
  612. strargs = [os.fspath(arg) for arg in args]
  613. strpath = self.strpath
  614. if abs:
  615. newargs: list[str] = []
  616. for arg in reversed(strargs):
  617. if isabs(arg):
  618. strpath = arg
  619. strargs = newargs
  620. break
  621. newargs.insert(0, arg)
  622. # special case for when we have e.g. strpath == "/"
  623. actual_sep = "" if strpath.endswith(sep) else sep
  624. for arg in strargs:
  625. arg = arg.strip(sep)
  626. if iswin32:
  627. # allow unix style paths even on windows.
  628. arg = arg.strip("/")
  629. arg = arg.replace("/", sep)
  630. strpath = strpath + actual_sep + arg
  631. actual_sep = sep
  632. obj = object.__new__(self.__class__)
  633. obj.strpath = normpath(strpath)
  634. return obj
  635. def open(self, mode="r", ensure=False, encoding=None):
  636. """Return an opened file with the given mode.
  637. If ensure is True, create parent directories if needed.
  638. """
  639. if ensure:
  640. self.dirpath().ensure(dir=1)
  641. if encoding:
  642. return error.checked_call(io.open, self.strpath, mode, encoding=encoding)
  643. return error.checked_call(open, self.strpath, mode)
  644. def _fastjoin(self, name):
  645. child = object.__new__(self.__class__)
  646. child.strpath = self.strpath + self.sep + name
  647. return child
  648. def islink(self):
  649. return islink(self.strpath)
  650. def check(self, **kw):
  651. """Check a path for existence and properties.
  652. Without arguments, return True if the path exists, otherwise False.
  653. valid checkers::
  654. file=1 # is a file
  655. file=0 # is not a file (may not even exist)
  656. dir=1 # is a dir
  657. link=1 # is a link
  658. exists=1 # exists
  659. You can specify multiple checker definitions, for example::
  660. path.check(file=1, link=1) # a link pointing to a file
  661. """
  662. if not kw:
  663. return exists(self.strpath)
  664. if len(kw) == 1:
  665. if "dir" in kw:
  666. return not kw["dir"] ^ isdir(self.strpath)
  667. if "file" in kw:
  668. return not kw["file"] ^ isfile(self.strpath)
  669. if not kw:
  670. kw = {"exists": 1}
  671. return Checkers(self)._evaluate(kw)
  672. _patternchars = set("*?[" + os.sep)
  673. def listdir(self, fil=None, sort=None):
  674. """List directory contents, possibly filter by the given fil func
  675. and possibly sorted.
  676. """
  677. if fil is None and sort is None:
  678. names = error.checked_call(os.listdir, self.strpath)
  679. return map_as_list(self._fastjoin, names)
  680. if isinstance(fil, str):
  681. if not self._patternchars.intersection(fil):
  682. child = self._fastjoin(fil)
  683. if exists(child.strpath):
  684. return [child]
  685. return []
  686. fil = FNMatcher(fil)
  687. names = error.checked_call(os.listdir, self.strpath)
  688. res = []
  689. for name in names:
  690. child = self._fastjoin(name)
  691. if fil is None or fil(child):
  692. res.append(child)
  693. self._sortlist(res, sort)
  694. return res
  695. def size(self) -> int:
  696. """Return size of the underlying file object"""
  697. return self.stat().size
  698. def mtime(self) -> float:
  699. """Return last modification time of the path."""
  700. return self.stat().mtime
  701. def copy(self, target, mode=False, stat=False):
  702. """Copy path to target.
  703. If mode is True, will copy copy permission from path to target.
  704. If stat is True, copy permission, last modification
  705. time, last access time, and flags from path to target.
  706. """
  707. if self.check(file=1):
  708. if target.check(dir=1):
  709. target = target.join(self.basename)
  710. assert self != target
  711. copychunked(self, target)
  712. if mode:
  713. copymode(self.strpath, target.strpath)
  714. if stat:
  715. copystat(self, target)
  716. else:
  717. def rec(p):
  718. return p.check(link=0)
  719. for x in self.visit(rec=rec):
  720. relpath = x.relto(self)
  721. newx = target.join(relpath)
  722. newx.dirpath().ensure(dir=1)
  723. if x.check(link=1):
  724. newx.mksymlinkto(x.readlink())
  725. continue
  726. elif x.check(file=1):
  727. copychunked(x, newx)
  728. elif x.check(dir=1):
  729. newx.ensure(dir=1)
  730. if mode:
  731. copymode(x.strpath, newx.strpath)
  732. if stat:
  733. copystat(x, newx)
  734. def rename(self, target):
  735. """Rename this path to target."""
  736. target = os.fspath(target)
  737. return error.checked_call(os.rename, self.strpath, target)
  738. def dump(self, obj, bin=1):
  739. """Pickle object into path location"""
  740. f = self.open("wb")
  741. import pickle
  742. try:
  743. error.checked_call(pickle.dump, obj, f, bin)
  744. finally:
  745. f.close()
  746. def mkdir(self, *args):
  747. """Create & return the directory joined with args."""
  748. p = self.join(*args)
  749. error.checked_call(os.mkdir, os.fspath(p))
  750. return p
  751. def write_binary(self, data, ensure=False):
  752. """Write binary data into path. If ensure is True create
  753. missing parent directories.
  754. """
  755. if ensure:
  756. self.dirpath().ensure(dir=1)
  757. with self.open("wb") as f:
  758. f.write(data)
  759. def write_text(self, data, encoding, ensure=False):
  760. """Write text data into path using the specified encoding.
  761. If ensure is True create missing parent directories.
  762. """
  763. if ensure:
  764. self.dirpath().ensure(dir=1)
  765. with self.open("w", encoding=encoding) as f:
  766. f.write(data)
  767. def write(self, data, mode="w", ensure=False):
  768. """Write data into path. If ensure is True create
  769. missing parent directories.
  770. """
  771. if ensure:
  772. self.dirpath().ensure(dir=1)
  773. if "b" in mode:
  774. if not isinstance(data, bytes):
  775. raise ValueError("can only process bytes")
  776. else:
  777. if not isinstance(data, str):
  778. if not isinstance(data, bytes):
  779. data = str(data)
  780. else:
  781. data = data.decode(sys.getdefaultencoding())
  782. f = self.open(mode)
  783. try:
  784. f.write(data)
  785. finally:
  786. f.close()
  787. def _ensuredirs(self):
  788. parent = self.dirpath()
  789. if parent == self:
  790. return self
  791. if parent.check(dir=0):
  792. parent._ensuredirs()
  793. if self.check(dir=0):
  794. try:
  795. self.mkdir()
  796. except error.EEXIST:
  797. # race condition: file/dir created by another thread/process.
  798. # complain if it is not a dir
  799. if self.check(dir=0):
  800. raise
  801. return self
  802. def ensure(self, *args, **kwargs):
  803. """Ensure that an args-joined path exists (by default as
  804. a file). if you specify a keyword argument 'dir=True'
  805. then the path is forced to be a directory path.
  806. """
  807. p = self.join(*args)
  808. if kwargs.get("dir", 0):
  809. return p._ensuredirs()
  810. else:
  811. p.dirpath()._ensuredirs()
  812. if not p.check(file=1):
  813. p.open("wb").close()
  814. return p
  815. @overload
  816. def stat(self, raising: Literal[True] = ...) -> Stat:
  817. ...
  818. @overload
  819. def stat(self, raising: Literal[False]) -> Stat | None:
  820. ...
  821. def stat(self, raising: bool = True) -> Stat | None:
  822. """Return an os.stat() tuple."""
  823. if raising:
  824. return Stat(self, error.checked_call(os.stat, self.strpath))
  825. try:
  826. return Stat(self, os.stat(self.strpath))
  827. except KeyboardInterrupt:
  828. raise
  829. except Exception:
  830. return None
  831. def lstat(self) -> Stat:
  832. """Return an os.lstat() tuple."""
  833. return Stat(self, error.checked_call(os.lstat, self.strpath))
  834. def setmtime(self, mtime=None):
  835. """Set modification time for the given path. if 'mtime' is None
  836. (the default) then the file's mtime is set to current time.
  837. Note that the resolution for 'mtime' is platform dependent.
  838. """
  839. if mtime is None:
  840. return error.checked_call(os.utime, self.strpath, mtime)
  841. try:
  842. return error.checked_call(os.utime, self.strpath, (-1, mtime))
  843. except error.EINVAL:
  844. return error.checked_call(os.utime, self.strpath, (self.atime(), mtime))
  845. def chdir(self):
  846. """Change directory to self and return old current directory"""
  847. try:
  848. old = self.__class__()
  849. except error.ENOENT:
  850. old = None
  851. error.checked_call(os.chdir, self.strpath)
  852. return old
  853. @contextmanager
  854. def as_cwd(self):
  855. """
  856. Return a context manager, which changes to the path's dir during the
  857. managed "with" context.
  858. On __enter__ it returns the old dir, which might be ``None``.
  859. """
  860. old = self.chdir()
  861. try:
  862. yield old
  863. finally:
  864. if old is not None:
  865. old.chdir()
  866. def realpath(self):
  867. """Return a new path which contains no symbolic links."""
  868. return self.__class__(os.path.realpath(self.strpath))
  869. def atime(self):
  870. """Return last access time of the path."""
  871. return self.stat().atime
  872. def __repr__(self):
  873. return "local(%r)" % self.strpath
  874. def __str__(self):
  875. """Return string representation of the Path."""
  876. return self.strpath
  877. def chmod(self, mode, rec=0):
  878. """Change permissions to the given mode. If mode is an
  879. integer it directly encodes the os-specific modes.
  880. if rec is True perform recursively.
  881. """
  882. if not isinstance(mode, int):
  883. raise TypeError(f"mode {mode!r} must be an integer")
  884. if rec:
  885. for x in self.visit(rec=rec):
  886. error.checked_call(os.chmod, str(x), mode)
  887. error.checked_call(os.chmod, self.strpath, mode)
  888. def pypkgpath(self):
  889. """Return the Python package path by looking for the last
  890. directory upwards which still contains an __init__.py.
  891. Return None if a pkgpath can not be determined.
  892. """
  893. pkgpath = None
  894. for parent in self.parts(reverse=True):
  895. if parent.isdir():
  896. if not parent.join("__init__.py").exists():
  897. break
  898. if not isimportable(parent.basename):
  899. break
  900. pkgpath = parent
  901. return pkgpath
  902. def _ensuresyspath(self, ensuremode, path):
  903. if ensuremode:
  904. s = str(path)
  905. if ensuremode == "append":
  906. if s not in sys.path:
  907. sys.path.append(s)
  908. else:
  909. if s != sys.path[0]:
  910. sys.path.insert(0, s)
  911. def pyimport(self, modname=None, ensuresyspath=True):
  912. """Return path as an imported python module.
  913. If modname is None, look for the containing package
  914. and construct an according module name.
  915. The module will be put/looked up in sys.modules.
  916. if ensuresyspath is True then the root dir for importing
  917. the file (taking __init__.py files into account) will
  918. be prepended to sys.path if it isn't there already.
  919. If ensuresyspath=="append" the root dir will be appended
  920. if it isn't already contained in sys.path.
  921. if ensuresyspath is False no modification of syspath happens.
  922. Special value of ensuresyspath=="importlib" is intended
  923. purely for using in pytest, it is capable only of importing
  924. separate .py files outside packages, e.g. for test suite
  925. without any __init__.py file. It effectively allows having
  926. same-named test modules in different places and offers
  927. mild opt-in via this option. Note that it works only in
  928. recent versions of python.
  929. """
  930. if not self.check():
  931. raise error.ENOENT(self)
  932. if ensuresyspath == "importlib":
  933. if modname is None:
  934. modname = self.purebasename
  935. spec = importlib.util.spec_from_file_location(modname, str(self))
  936. if spec is None or spec.loader is None:
  937. raise ImportError(
  938. f"Can't find module {modname} at location {str(self)}"
  939. )
  940. mod = importlib.util.module_from_spec(spec)
  941. spec.loader.exec_module(mod)
  942. return mod
  943. pkgpath = None
  944. if modname is None:
  945. pkgpath = self.pypkgpath()
  946. if pkgpath is not None:
  947. pkgroot = pkgpath.dirpath()
  948. names = self.new(ext="").relto(pkgroot).split(self.sep)
  949. if names[-1] == "__init__":
  950. names.pop()
  951. modname = ".".join(names)
  952. else:
  953. pkgroot = self.dirpath()
  954. modname = self.purebasename
  955. self._ensuresyspath(ensuresyspath, pkgroot)
  956. __import__(modname)
  957. mod = sys.modules[modname]
  958. if self.basename == "__init__.py":
  959. return mod # we don't check anything as we might
  960. # be in a namespace package ... too icky to check
  961. modfile = mod.__file__
  962. assert modfile is not None
  963. if modfile[-4:] in (".pyc", ".pyo"):
  964. modfile = modfile[:-1]
  965. elif modfile.endswith("$py.class"):
  966. modfile = modfile[:-9] + ".py"
  967. if modfile.endswith(os.sep + "__init__.py"):
  968. if self.basename != "__init__.py":
  969. modfile = modfile[:-12]
  970. try:
  971. issame = self.samefile(modfile)
  972. except error.ENOENT:
  973. issame = False
  974. if not issame:
  975. ignore = os.getenv("PY_IGNORE_IMPORTMISMATCH")
  976. if ignore != "1":
  977. raise self.ImportMismatchError(modname, modfile, self)
  978. return mod
  979. else:
  980. try:
  981. return sys.modules[modname]
  982. except KeyError:
  983. # we have a custom modname, do a pseudo-import
  984. import types
  985. mod = types.ModuleType(modname)
  986. mod.__file__ = str(self)
  987. sys.modules[modname] = mod
  988. try:
  989. with open(str(self), "rb") as f:
  990. exec(f.read(), mod.__dict__)
  991. except BaseException:
  992. del sys.modules[modname]
  993. raise
  994. return mod
  995. def sysexec(self, *argv: os.PathLike[str], **popen_opts: Any) -> str:
  996. """Return stdout text from executing a system child process,
  997. where the 'self' path points to executable.
  998. The process is directly invoked and not through a system shell.
  999. """
  1000. from subprocess import Popen, PIPE
  1001. popen_opts.pop("stdout", None)
  1002. popen_opts.pop("stderr", None)
  1003. proc = Popen(
  1004. [str(self)] + [str(arg) for arg in argv],
  1005. **popen_opts,
  1006. stdout=PIPE,
  1007. stderr=PIPE,
  1008. )
  1009. stdout: str | bytes
  1010. stdout, stderr = proc.communicate()
  1011. ret = proc.wait()
  1012. if isinstance(stdout, bytes):
  1013. stdout = stdout.decode(sys.getdefaultencoding())
  1014. if ret != 0:
  1015. if isinstance(stderr, bytes):
  1016. stderr = stderr.decode(sys.getdefaultencoding())
  1017. raise RuntimeError(
  1018. ret,
  1019. ret,
  1020. str(self),
  1021. stdout,
  1022. stderr,
  1023. )
  1024. return stdout
  1025. @classmethod
  1026. def sysfind(cls, name, checker=None, paths=None):
  1027. """Return a path object found by looking at the systems
  1028. underlying PATH specification. If the checker is not None
  1029. it will be invoked to filter matching paths. If a binary
  1030. cannot be found, None is returned
  1031. Note: This is probably not working on plain win32 systems
  1032. but may work on cygwin.
  1033. """
  1034. if isabs(name):
  1035. p = local(name)
  1036. if p.check(file=1):
  1037. return p
  1038. else:
  1039. if paths is None:
  1040. if iswin32:
  1041. paths = os.environ["Path"].split(";")
  1042. if "" not in paths and "." not in paths:
  1043. paths.append(".")
  1044. try:
  1045. systemroot = os.environ["SYSTEMROOT"]
  1046. except KeyError:
  1047. pass
  1048. else:
  1049. paths = [
  1050. path.replace("%SystemRoot%", systemroot) for path in paths
  1051. ]
  1052. else:
  1053. paths = os.environ["PATH"].split(":")
  1054. tryadd = []
  1055. if iswin32:
  1056. tryadd += os.environ["PATHEXT"].split(os.pathsep)
  1057. tryadd.append("")
  1058. for x in paths:
  1059. for addext in tryadd:
  1060. p = local(x).join(name, abs=True) + addext
  1061. try:
  1062. if p.check(file=1):
  1063. if checker:
  1064. if not checker(p):
  1065. continue
  1066. return p
  1067. except error.EACCES:
  1068. pass
  1069. return None
  1070. @classmethod
  1071. def _gethomedir(cls):
  1072. try:
  1073. x = os.environ["HOME"]
  1074. except KeyError:
  1075. try:
  1076. x = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
  1077. except KeyError:
  1078. return None
  1079. return cls(x)
  1080. # """
  1081. # special class constructors for local filesystem paths
  1082. # """
  1083. @classmethod
  1084. def get_temproot(cls):
  1085. """Return the system's temporary directory
  1086. (where tempfiles are usually created in)
  1087. """
  1088. import tempfile
  1089. return local(tempfile.gettempdir())
  1090. @classmethod
  1091. def mkdtemp(cls, rootdir=None):
  1092. """Return a Path object pointing to a fresh new temporary directory
  1093. (which we created ourself).
  1094. """
  1095. import tempfile
  1096. if rootdir is None:
  1097. rootdir = cls.get_temproot()
  1098. return cls(error.checked_call(tempfile.mkdtemp, dir=str(rootdir)))
  1099. @classmethod
  1100. def make_numbered_dir(
  1101. cls, prefix="session-", rootdir=None, keep=3, lock_timeout=172800
  1102. ): # two days
  1103. """Return unique directory with a number greater than the current
  1104. maximum one. The number is assumed to start directly after prefix.
  1105. if keep is true directories with a number less than (maxnum-keep)
  1106. will be removed. If .lock files are used (lock_timeout non-zero),
  1107. algorithm is multi-process safe.
  1108. """
  1109. if rootdir is None:
  1110. rootdir = cls.get_temproot()
  1111. nprefix = prefix.lower()
  1112. def parse_num(path):
  1113. """Parse the number out of a path (if it matches the prefix)"""
  1114. nbasename = path.basename.lower()
  1115. if nbasename.startswith(nprefix):
  1116. try:
  1117. return int(nbasename[len(nprefix) :])
  1118. except ValueError:
  1119. pass
  1120. def create_lockfile(path):
  1121. """Exclusively create lockfile. Throws when failed"""
  1122. mypid = os.getpid()
  1123. lockfile = path.join(".lock")
  1124. if hasattr(lockfile, "mksymlinkto"):
  1125. lockfile.mksymlinkto(str(mypid))
  1126. else:
  1127. fd = error.checked_call(
  1128. os.open, str(lockfile), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644
  1129. )
  1130. with os.fdopen(fd, "w") as f:
  1131. f.write(str(mypid))
  1132. return lockfile
  1133. def atexit_remove_lockfile(lockfile):
  1134. """Ensure lockfile is removed at process exit"""
  1135. mypid = os.getpid()
  1136. def try_remove_lockfile():
  1137. # in a fork() situation, only the last process should
  1138. # remove the .lock, otherwise the other processes run the
  1139. # risk of seeing their temporary dir disappear. For now
  1140. # we remove the .lock in the parent only (i.e. we assume
  1141. # that the children finish before the parent).
  1142. if os.getpid() != mypid:
  1143. return
  1144. try:
  1145. lockfile.remove()
  1146. except error.Error:
  1147. pass
  1148. atexit.register(try_remove_lockfile)
  1149. # compute the maximum number currently in use with the prefix
  1150. lastmax = None
  1151. while True:
  1152. maxnum = -1
  1153. for path in rootdir.listdir():
  1154. num = parse_num(path)
  1155. if num is not None:
  1156. maxnum = max(maxnum, num)
  1157. # make the new directory
  1158. try:
  1159. udir = rootdir.mkdir(prefix + str(maxnum + 1))
  1160. if lock_timeout:
  1161. lockfile = create_lockfile(udir)
  1162. atexit_remove_lockfile(lockfile)
  1163. except (error.EEXIST, error.ENOENT, error.EBUSY):
  1164. # race condition (1): another thread/process created the dir
  1165. # in the meantime - try again
  1166. # race condition (2): another thread/process spuriously acquired
  1167. # lock treating empty directory as candidate
  1168. # for removal - try again
  1169. # race condition (3): another thread/process tried to create the lock at
  1170. # the same time (happened in Python 3.3 on Windows)
  1171. # https://ci.appveyor.com/project/pytestbot/py/build/1.0.21/job/ffi85j4c0lqwsfwa
  1172. if lastmax == maxnum:
  1173. raise
  1174. lastmax = maxnum
  1175. continue
  1176. break
  1177. def get_mtime(path):
  1178. """Read file modification time"""
  1179. try:
  1180. return path.lstat().mtime
  1181. except error.Error:
  1182. pass
  1183. garbage_prefix = prefix + "garbage-"
  1184. def is_garbage(path):
  1185. """Check if path denotes directory scheduled for removal"""
  1186. bn = path.basename
  1187. return bn.startswith(garbage_prefix)
  1188. # prune old directories
  1189. udir_time = get_mtime(udir)
  1190. if keep and udir_time:
  1191. for path in rootdir.listdir():
  1192. num = parse_num(path)
  1193. if num is not None and num <= (maxnum - keep):
  1194. try:
  1195. # try acquiring lock to remove directory as exclusive user
  1196. if lock_timeout:
  1197. create_lockfile(path)
  1198. except (error.EEXIST, error.ENOENT, error.EBUSY):
  1199. path_time = get_mtime(path)
  1200. if not path_time:
  1201. # assume directory doesn't exist now
  1202. continue
  1203. if abs(udir_time - path_time) < lock_timeout:
  1204. # assume directory with lockfile exists
  1205. # and lock timeout hasn't expired yet
  1206. continue
  1207. # path dir locked for exclusive use
  1208. # and scheduled for removal to avoid another thread/process
  1209. # treating it as a new directory or removal candidate
  1210. garbage_path = rootdir.join(garbage_prefix + str(uuid.uuid4()))
  1211. try:
  1212. path.rename(garbage_path)
  1213. garbage_path.remove(rec=1)
  1214. except KeyboardInterrupt:
  1215. raise
  1216. except Exception: # this might be error.Error, WindowsError ...
  1217. pass
  1218. if is_garbage(path):
  1219. try:
  1220. path.remove(rec=1)
  1221. except KeyboardInterrupt:
  1222. raise
  1223. except Exception: # this might be error.Error, WindowsError ...
  1224. pass
  1225. # make link...
  1226. try:
  1227. username = os.environ["USER"] # linux, et al
  1228. except KeyError:
  1229. try:
  1230. username = os.environ["USERNAME"] # windows
  1231. except KeyError:
  1232. username = "current"
  1233. src = str(udir)
  1234. dest = src[: src.rfind("-")] + "-" + username
  1235. try:
  1236. os.unlink(dest)
  1237. except OSError:
  1238. pass
  1239. try:
  1240. os.symlink(src, dest)
  1241. except (OSError, AttributeError, NotImplementedError):
  1242. pass
  1243. return udir
  1244. def copymode(src, dest):
  1245. """Copy permission from src to dst."""
  1246. import shutil
  1247. shutil.copymode(src, dest)
  1248. def copystat(src, dest):
  1249. """Copy permission, last modification time,
  1250. last access time, and flags from src to dst."""
  1251. import shutil
  1252. shutil.copystat(str(src), str(dest))
  1253. def copychunked(src, dest):
  1254. chunksize = 524288 # half a meg of bytes
  1255. fsrc = src.open("rb")
  1256. try:
  1257. fdest = dest.open("wb")
  1258. try:
  1259. while 1:
  1260. buf = fsrc.read(chunksize)
  1261. if not buf:
  1262. break
  1263. fdest.write(buf)
  1264. finally:
  1265. fdest.close()
  1266. finally:
  1267. fsrc.close()
  1268. def isimportable(name):
  1269. if name and (name[0].isalpha() or name[0] == "_"):
  1270. name = name.replace("_", "")
  1271. return not name or name.isalnum()
  1272. local = LocalPath