okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
5bf3cbb
main
1"""One scratch directory per test process, removed when the process exits. 2 3Every Python suite works inside ``<root>/<name>-<pid>``, where root is 4``$ALBEDO_TEST_TMP`` or ``/tmp/albedo-tests``. Claiming it points tempfile's 5default directory and ``TMPDIR`` there, so daemons, their kernels and any 6temporary directory a test forgets are removed together instead of piling up in 7/tmp or in each nix shell's own TMPDIR. A run that died before cleaning up is 8swept by the next one. The root is fixed rather than taken from TMPDIR, so 9locks under it hold across shells and unix socket paths below it stay short. 10""" 11 12import atexit 13import os 14from pathlib import Path 15import shutil 16import tempfile 17 18ROOT = Path (os .environ .get ("ALBEDO_TEST_TMP" ,"/tmp/albedo-tests" )) 19 20 21def claim (name :str )-> Path : 22"""This process's scratch directory, created on first use.""" 23path = ROOT / f" { name } - { os . getpid () } " 24if tempfile .tempdir == str (path ): 25return path 26ROOT .mkdir (parents = True ,exist_ok = True ) 27sweep () 28path .mkdir (exist_ok = True ) 29tempfile .tempdir = str (path ) 30os .environ ["TMPDIR" ]= str (path ) 31atexit .register (shutil .rmtree ,path ,ignore_errors = True ) 32return path 33 34 35def sweep ()-> None : 36"""Remove the directories of test processes that no longer exist.""" 37for entry in ROOT .iterdir (): 38pid = entry .name .rpartition ("-" )[2 ] 39if entry .is_dir ()and pid .isdigit ()and not alive (int (pid )): 40shutil .rmtree (entry ,ignore_errors = True ) 41 42 43def alive (pid :int )-> bool : 44try : 45os .kill (pid ,0 ) 46except ProcessLookupError : 47return False 48except PermissionError : 49return True 50return True