defl/tests.old/test_pathUtils.py

116 lines
2.6 KiB
Python
Raw Normal View History

2024-09-11 11:14:03 -04:00
#!/usr/bin/env python
2025-03-09 09:17:53 -04:00
"""
2024-09-11 11:14:03 -04:00
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("**/*")'
2025-03-09 09:17:53 -04:00
"""
2024-09-11 11:14:03 -04:00
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'
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
def tryLockFile(index: int, slp: float):
log.debug(index, f'sleep {slp}')
time.sleep(slp)
log.debug(index, f'lock file')
2025-03-09 09:17:53 -04:00
with defl.LockFile(tryLockFileLock, wait=T):
time.sleep(0.1)
2024-09-11 11:14:03 -04:00
log.debug(index, f'unlock file')
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
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()
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
def msg(a, b):
return f'{cl.yel}{a=}{cl.r} {cl.mag}{b=}{cl.r}'
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
tester = Tester(name=__file__)
lock = defl.tempDir.tempFile(autoRemove=F, create=F)
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def test():
Assert(lock).lam(lambda x: not x.isFile())
2025-03-09 09:17:53 -04:00
with defl.LockFile(lock, wait=T):
2024-09-11 11:14:03 -04:00
Assert(lock).lam(lambda x: x.isFile())
Assert(lock).lam(lambda x: not x.isFile())
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def test():
Assert(lock).lam(lambda x: not x.isFile())
try:
2025-03-09 09:17:53 -04:00
with defl.LockFile(lock, wait=T):
2024-09-11 11:14:03 -04:00
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())
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
# @tester.add()
def tryLockFile():
# TODO
def run(i) -> defl.RunCom:
2025-03-09 09:17:53 -04:00
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()
2024-09-11 11:14:03 -04:00
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]:
2025-03-09 09:17:53 -04:00
a = tmp / i / j
2024-09-11 11:14:03 -04:00
a.touch()
allFile.append(a)
res = [i for i in defl.Find().setType('f').run(tmp)]
for i in allFile:
assert i in res
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@tester.add()
def findTest() -> N:
for i in range(30):
find()
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
@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
2025-03-09 09:17:53 -04:00
2024-09-11 11:14:03 -04:00
log.info(tester.run())
tester.exitWithStatus()
# makeTestFiles()
# pathfilter1()
# parts()