#!/usr/bin/env python3 """Owned opt-in test expiry, conservative unused cache, and shared build guard.""" import pathlib,os,time,shutil,fcntl,subprocess,json,sys BASE=pathlib.Path('/opt/services/canina/.managed-disk') RESERVE=20*1024**3 def retire(root,now=None): root=pathlib.Path(root);now=time.time() if now is None else now if root.is_symlink() or root.resolve()!=root:raise RuntimeError('unsafe root') removed=[] for p in sorted(root.iterdir()): marker=p/'.disposable-test' if p.is_symlink() or not p.is_dir() or p.resolve()!=p:continue if not marker.is_file() or marker.is_symlink() or marker.read_text()!='canina-managed-test-v1\n':continue if now-p.stat().st_mtime<7*86400:continue # Unknown/source-bearing contents are never eligible; symlinks are never followed. if any(x.name in {'.git','.env','node_modules','vendor'} or x.is_symlink() or os.path.ismount(x) for x in p.rglob('*')):continue users=[] for proc in pathlib.Path('/proc').glob('[0-9]*'): for q in [proc/'cwd',proc/'root',*(proc/'fd').glob('*')]: try: v=os.readlink(q) if v==str(p) or v.startswith(str(p)+'/'):users.append(v) except OSError:pass if users:continue shutil.rmtree(p);removed.append(p.name) return removed def busy(): for p in pathlib.Path('/proc').glob('[0-9]*'): try: args=(p/'cmdline').read_bytes().replace(b'\0',b' ').decode(errors='replace') if any(x in args for x in ['docker build','compose build','next build','npm ci','npm install','composer install','rclone rcat']):return True except OSError:continue return False def main(): if BASE.is_symlink() or BASE.resolve()!=BASE:raise SystemExit('unsafe state root') with (BASE/'heavy.lock').open('a') as lock: fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) if sys.argv[1:2]==['build']: free=shutil.disk_usage(BASE).free if free