#!/usr/bin/env python ''' onFileChange.py ~/deflDev/tests/test_path.py ~/deflDev/defl/_path_.py -- ~/deflDev/bin/pythonDeflDev.py - python ~/deflDev/tests/test_path.py time find >/dev/null time python3 -c 'import pathlib;pathlib.Path("**/*")' ''' import os import pathlib import random import shlex import sys import time import defl from defl import Assert, Path, cl, log from defl.testing_ import Tester from defl._typing_ import * tryLockFileLock = defl.tempDir / 'fdsafhfauhfsa' def tryLockFile(index: int, slp: float): log.debug(index, f'sleep {slp}') time.sleep(slp) log.debug(index, f'lock file') with defl.LockFile(tryLockFileLock): time.sleep(.1) log.debug(index, f'unlock file') if len(sys.argv) > 1 and sys.argv[1] == 'tryLockFile': tryLockFileLock.remove() tryLockFile(index=int(sys.argv[2]), slp=float(sys.argv[3])) sys.exit() def msg(a, b): return f'{cl.yel}{a=}{cl.r} {cl.mag}{b=}{cl.r}' tester = Tester(name=__file__) lock = defl.tempDir.tempFile(autoRemove=F, create=F) @tester.add() def test(): Assert(lock).lam(lambda x: not x.isFile()) with defl.LockFile(lock): Assert(lock).lam(lambda x: x.isFile()) Assert(lock).lam(lambda x: not x.isFile()) @tester.add() def test(): Assert(lock).lam(lambda x: not x.isFile()) try: with defl.LockFile(lock): Assert(lock).lam(lambda x: x.isFile()) raise ValueError('test exception') except ValueError: ... # Assert(lock).lam(lambda x: not x.isFile()) Assert(lock).lam(lambda x: not x.isFile()) # @tester.add() def tryLockFile(): # TODO def run(i) -> defl.RunCom: return defl.RunCom([__file__, 'tryLockFile', i, random.random()], runNow=False, raiseOnFail=True, pipe=False) defl.ComPool([run(i).run for i in range(100)], name='tryLockFile', workers=20, progressInfo='try lock').results() def find() -> N: tmp = Path('/tmp/testFind') tmp.mkdirForce() allFile = [] for i in ['a', 'b', 'c']: (tmp / i).mkdirForce() for j in [1, 2, 3]: a = (tmp / i / j) a.touch() allFile.append(a) res = [i for i in defl.Find().setType('f').run(tmp)] for i in allFile: assert i in res @tester.add() def findTest() -> N: for i in range(30): find() @tester.add() def stat() -> N: a = Path.Temp().tempFile() d1 = a.st.st_mtime log.info('d1', lambda x: x.d1) a.setAccModTime(1, 1) d2 = a.st.st_mtime log.info('d2', lambda x: x.d2) Assert(d1) != d2 Assert(d2) == 1 log.info(tester.run()) tester.exitWithStatus() # makeTestFiles() # pathfilter1() # parts()