File size: 2,529 Bytes
2f382c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""Remove workstation identity and absolute prefixes from text metadata."""

from __future__ import annotations

import argparse
import os
import sys
import tempfile
from pathlib import Path


PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from accesspath3r.privacy import sanitize_text  # noqa: E402


TEXT_SUFFIXES = {".err", ".json", ".jsonl", ".log", ".md", ".out", ".txt"}


def sanitize_file(
    path: Path,
    *,
    project_root: Path,
    output_root: Path,
    sensitive_paths: list[str],
) -> bool:
    try:
        original = path.read_text(encoding="utf-8")
    except (OSError, UnicodeDecodeError):
        return False
    sanitized = sanitize_text(
        original,
        project_root=project_root,
        output_root=output_root,
        sensitive_paths=sensitive_paths,
    )
    if sanitized == original:
        return False

    original_mode = path.stat().st_mode & 0o7777
    file_descriptor, temporary_name = tempfile.mkstemp(
        prefix=f".{path.name}.",
        suffix=".tmp",
        dir=path.parent,
    )
    try:
        os.fchmod(file_descriptor, original_mode)
        with os.fdopen(file_descriptor, "w", encoding="utf-8") as handle:
            handle.write(sanitized)
            handle.flush()
            os.fsync(handle.fileno())
        os.replace(temporary_name, path)
    except BaseException:
        try:
            os.unlink(temporary_name)
        except OSError:
            pass
        raise
    return True


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser()
    parser.add_argument("--root", type=Path, required=True)
    parser.add_argument("--project-root", type=Path, required=True)
    parser.add_argument("--sensitive-path", action="append", default=[])
    return parser


def main() -> int:
    args = build_parser().parse_args()
    if args.root.is_file():
        paths = [args.root]
        output_root = args.root.parent
    elif args.root.is_dir():
        paths = args.root.rglob("*")
        output_root = args.root
    else:
        return 0
    for path in paths:
        if path.is_file() and path.suffix.lower() in TEXT_SUFFIXES:
            sanitize_file(
                path,
                project_root=args.project_root,
                output_root=output_root,
                sensitive_paths=args.sensitive_path,
            )
    return 0


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