Files

33 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Read-only post-run cwd/process sweep; never signals a process."""
import datetime, json, os
from pathlib import Path
HERE=Path(__file__).resolve().parent
repo=HERE.parents[3]
run=HERE/'run3'
tmp=Path((run/'TMPDIR').read_text().strip())
ancestors=set()
pid=os.getpid()
while pid and pid not in ancestors:
ancestors.add(pid)
try:
status=Path(f'/proc/{pid}/status').read_text()
pid=int(next(s.split(':')[1] for s in status.splitlines() if s.startswith('PPid:')))
except (OSError,StopIteration): break
remaining=[]
excluded=[]
for proc in Path('/proc').iterdir():
if not proc.name.isdigit() or int(proc.name) in ancestors: continue
try:
cwd=(proc/'cwd').resolve(strict=True)
if not (cwd==repo or repo in cwd.parents or cwd==tmp or tmp in cwd.parents): continue
comm=(proc/'comm').read_text().strip()
item=dict(pid=int(proc.name),comm=comm,cwd=str(cwd))
if item['pid']==2880439 and comm.startswith('chrome-devtools'):
excluded.append(item)
else: remaining.append(item)
except (OSError,ValueError): continue
result=dict(timestamp=datetime.datetime.now(datetime.timezone.utc).isoformat(),count=len(remaining),remaining=remaining,excluded_preexisting_infrastructure=excluded,note='Read-only /proc cwd sweep over own worktree and this run TMPDIR; own ancestry excluded; PID2880439 pre-existing receipt in run1; no signalling.')
(run/'sweep.json').write_text(json.dumps(result,indent=2)+'\n')
print(json.dumps(result,indent=2))