okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
53623e56f
main
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 ): 37for old ,new in REPLACEMENTS : 38text = text .replace (old ,new ) 39return text 40 41 42def main (): 43p = argparse .ArgumentParser (description = __doc__ ) 44p .add_argument ('--data' ,type = Path ,default = DATA ) 45p .add_argument ('--dry-run' ,action = 'store_true' ) 46args = p .parse_args () 47files = [f for f in sorted (args .data .rglob ('*' )) 48if f .is_file ()and f .suffix in SUFFIXES ] 49readme = args .data .parent / 'README.md' 50if readme .exists (): 51files .append (readme ) 52changed = 0 53for f in files : 54before = f .read_text (encoding = 'utf-8' ,errors = 'replace' ) 55after = sanitize (before ) 56if after != before : 57changed += 1 58hits = sum (before .count (old )for old ,_ in REPLACEMENTS ) 59f' { f . name } : { hits } path(s)' ) 60if not args .dry_run : 61f .write_text (after ,encoding = 'utf-8' ) 62f' { changed } file(s) { "would be " if args . dry_run else "" } rewritten' ) 63 64 65if __name__ == '__main__' : 66sys .exit (main ())