blob: 0ee00263cbcf7d6a0f66f688424f7542b0fe90e3 [file] [log] [blame]
Ben Reich6a666ba2021-06-23 04:23:011#!/usr/bin/env python
2
3import json
4import os
5import re
6
7
8def Clean(start_dir, attr_pattern, file_pattern):
9 cleaned = False
10
11 def _remove_attrs(json_dict, attr_pattern):
12 assert isinstance(json_dict, dict)
13
14 removed = False
15
16 for key, val in json_dict.items():
17 if isinstance(val, dict):
18 if _remove_attrs(val, attr_pattern):
19 removed = True
20 elif re.search(attr_pattern, key):
21 del json_dict[key]
22 removed = True
23
24 return removed
25
26 for root, dirs, files in os.walk(start_dir):
27 for f in files:
28 if not re.search(file_pattern, f):
29 continue
30
31 path = os.path.join(root, f)
32 json_dict = json.loads(open(path).read())
33 if not _remove_attrs(json_dict, attr_pattern):
34 continue
35
36 with open(path, 'w') as new_contents:
37 new_contents.write(json.dumps(json_dict))
38 cleaned = True
39
40 return cleaned
41
42
43if __name__ == '__main__':
44 import argparse
45 import sys
46 parser = argparse.ArgumentParser(
47 description='Recursively removes attributes from JSON files')
48 parser.add_argument('--attr_pattern', type=str, required=True,
49 help='A regex of attributes to remove')
50 parser.add_argument('--file_pattern', type=str, required=True,
51 help='A regex of files to clean')
52 parser.add_argument('start_dir', type=str,
53 help='A directory to start scanning')
54 args = parser.parse_args(sys.argv[1:])
55 Clean(start_dir=args.start_dir, attr_pattern=args.attr_pattern,
56 file_pattern=args.file_pattern)