35 lines
2.3 KiB
Python
35 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only sweep of this worktree and focused TMPDIR, including deleted cwd."""
|
|
import datetime
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
HERE=Path(__file__).resolve().parent.parent
|
|
REPO=HERE.parents[3]
|
|
RUN=HERE/'run5'
|
|
def main():
|
|
tmp=Path((RUN/'TMPDIR').read_text().strip())
|
|
started=datetime.datetime.fromisoformat((RUN/'start.txt').read_text().strip().replace('Z','+00:00')).timestamp()
|
|
boot=int(next(s.split()[1] for s in Path('/proc/stat').read_text().splitlines() if s.startswith('btime ')))
|
|
ancestors=set();pid=os.getpid()
|
|
while pid and pid not in ancestors:
|
|
ancestors.add(pid)
|
|
try:pid=int(next(s.split(':')[1] for s in Path(f'/proc/{pid}/status').read_text().splitlines() if s.startswith('PPid:')))
|
|
except (OSError,StopIteration):break
|
|
known={1425557,1425563,1425568,1425580,1425769,1425776,1425783,1425797,1426427,2880281,2880282,2880283,2880284,2880445,2880487,2880537,2883251,2880439,2469277,2469278,2469279,2469292,2469478,2469485,2469518,2469601,2470486}
|
|
remaining=[];excluded=[]
|
|
for proc in Path('/proc').iterdir():
|
|
if not proc.name.isdigit() or int(proc.name) in ancestors:continue
|
|
try:
|
|
rawcwd=os.readlink(proc/'cwd');cwd=Path(rawcwd.removesuffix(' (deleted)'))
|
|
if not(cwd==REPO or REPO in cwd.parents or cwd==tmp or tmp in cwd.parents):continue
|
|
stat=(proc/'stat').read_text().rsplit(') ',1)[1].split()
|
|
stamp=boot+int(stat[19])/os.sysconf('SC_CLK_TCK')
|
|
item=dict(pid=int(proc.name),comm=(proc/'comm').read_text().strip(),cwd=rawcwd,started_utc=datetime.datetime.fromtimestamp(stamp,datetime.timezone.utc).isoformat())
|
|
if int(proc.name) in known and stamp<started:excluded.append(item)
|
|
else:remaining.append(item)
|
|
except (OSError,ValueError,IndexError):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, including deleted cwd under this run TMPDIR. Own ancestry excluded. Only known MCP/npm/code-mode infrastructure PIDs with pre-run start ticks are excluded. No signalling.')
|
|
(RUN/'sweep.json').write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(result,indent=2))
|
|
if __name__=='__main__':main()
|