---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 [redacted secret-like line] from __future__ import annotations import os import re import stat import tempfile from pathlib import Path LOG_ROOT = Path(\"/home/ben/infra/copecheck/logs\") LOG_NAMES."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-08-24T12:39:23Z"
site: "main"
source_path: "copecheck/sanitize_gnews_logs.py"
---

# !/usr/bin/env python3

Source path: `copecheck/sanitize_gnews_logs.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-08-24T12:39:23Z`

```py
#!/usr/bin/env python3
[redacted secret-like line]
from __future__ import annotations

import os
import re
import stat
import tempfile
from pathlib import Path


LOG_ROOT = Path("/home/ben/infra/copecheck/logs")
LOG_NAMES = ("batch_run2.log.1", "runner.log.1", "pipeline.log.1")
[redacted secret-like line]


def redact(path: Path) -> int:
    original = path.read_bytes()
[redacted secret-like line]
    if not count:
        return 0
    original_stat = path.stat()
    mode = stat.S_IMODE(original_stat.st_mode)
    fd, tmp_name = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
    try:
        with os.fdopen(fd, "wb") as handle:
            handle.write(cleaned)
            handle.flush()
            os.fsync(handle.fileno())
        os.chmod(tmp_name, mode)
        # Atomic replacement creates a new inode.  Preserve ownership so a
        # root-run maintenance command cannot leave the normal pipeline unable
        # to append to its own logs.
        try:
            os.chown(tmp_name, original_stat.st_uid, original_stat.st_gid)
        except PermissionError:
            # An unprivileged owner can still safely replace its own file; on
            # such systems ownership already matches the invoking user.
            pass
        os.replace(tmp_name, path)
    finally:
        if os.path.exists(tmp_name):
            os.unlink(tmp_name)
    return count


def main() -> int:
    total = 0
    for name in LOG_NAMES:
        path = LOG_ROOT / name
        count = redact(path) if path.exists() else 0
        total += count
        print(f"{name}: redacted={count}")
    print(f"total_redacted={total}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
```
