okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-1402-w' into f-140253623e56f

main
2.2 KiB66 linesraw
1#!/usr/bin/env python3
2"""Replace this machine's private paths in the report's data files before publishing.
3
4The report rules say to remove credentials and private paths. Every file this
5report ships is rewritten in place:
6
7    <home>/Projects/Strata-data  -> <data root>
8    <home>/Projects/Strata       -> <strata checkout>
9    <home>/.local/opt            -> <local opt>
10    <home>                       -> <home>
11
12Nothing else is touched: numbers, flags and log lines stay exactly as the
13engine wrote them. Run it once, after every measurement is in data/.
14
15Usage: sanitize.py [--data DIR] [--dry-run]
16"""
17import argparse
18import sys
19from pathlib import Path
20
21DATA = Path(__file__).resolve().parent.parent / 'data'
22HOME = Path.home()
23# This machine's layout: where the checkout, its data root and the toolchain are.
24CHECKOUT = HOME / 'Projects' / 'Strata'
25DATA_ROOT = HOME / 'Projects' / 'Strata-data'
26LOCAL_OPT = HOME / '.local' / 'opt'
27REPLACEMENTS = [
28    (str(DATA_ROOT), '<data root>'),
29    (str(CHECKOUT), '<strata checkout>'),
30    (str(LOCAL_OPT), '<local opt>'),
31    (str(HOME), '<home>'),
32]
33SUFFIXES = {'.log', '.out', '.json', '.txt', '.csv', '.sh', '.py', '.md'}
34
35
36def sanitize(text):
37    for old, new in REPLACEMENTS:
38        text = text.replace(old, new)
39    return text
40
41
42def main():
43    p = argparse.ArgumentParser(description=__doc__)
44    p.add_argument('--data', type=Path, default=DATA)
45    p.add_argument('--dry-run', action='store_true')
46    args = p.parse_args()
47    files = [f for f in sorted(args.data.rglob('*'))
48             if f.is_file() and f.suffix in SUFFIXES]
49    readme = args.data.parent / 'README.md'
50    if readme.exists():
51        files.append(readme)
52    changed = 0
53    for f in files:
54        before = f.read_text(encoding='utf-8', errors='replace')
55        after = sanitize(before)
56        if after != before:
57            changed += 1
58            hits = sum(before.count(old) for old, _ in REPLACEMENTS)
59            print(f'{f.name}: {hits} path(s)')
60            if not args.dry_run:
61                f.write_text(after, encoding='utf-8')
62    print(f'{changed} file(s) {"would be " if args.dry_run else ""}rewritten')
63
64
65if __name__ == '__main__':
66    sys.exit(main())