File size: 1,818 Bytes
547c405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Refuse to publish a folder that names this machine or its user.

Scans every file's bytes, model files included: an ONNX export once carried the exporting
machine's source paths in per-node metadata. Looks for this machine's home path, user name and
host name, any absolute home-style path, and any extra words given.

    python check_release.py release-telephony --also some-word another-word
"""

from __future__ import annotations

import argparse
import getpass
import re
import socket
import sys
from pathlib import Path


def findings(folder: Path, extra: list[str]) -> list[str]:
    host = socket.gethostname().split(".")[0]
    words = {str(Path.home()), getpass.getuser(), *extra} | ({host} if len(host) > 3 else set())
    pattern = re.compile(
        b"|".join([re.escape(word.encode()) for word in sorted(words)] + [rb"/Users/[A-Za-z0-9._-]+", rb"/home/[A-Za-z0-9._-]+", rb"[A-Za-z]:\\Users\\"]),
        re.IGNORECASE,
    )
    found = []
    for path in sorted(p for p in folder.rglob("*") if p.is_file()):
        matches = pattern.findall(path.read_bytes())
        if matches:
            found.append(f"{path}: {len(matches)} matches, e.g. {matches[0][:40]!r}")
    return found


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("folder", type=Path)
    parser.add_argument("--also", nargs="*", default=[], help="more words that must not appear")
    args = parser.parse_args()
    found = findings(args.folder, args.also)
    files = sum(1 for p in args.folder.rglob("*") if p.is_file())
    if found:
        print("\n".join(found))
        sys.exit(f"{len(found)} of {files} files name this machine or its user: do not publish")
    print(f"{files} files scanned, nothing found")


if __name__ == "__main__":
    main()