-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune-cache.py
96 lines (81 loc) · 2.49 KB
/
prune-cache.py
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
# SPDX-License-Identifier: MIT
import sys
import json
import datetime
import argparse
import pathlib
def parse_cache_date(datestr: str):
yr, mo, day, type = datestr.split("-")
date = datetime.date(int(yr), int(mo), int(day))
return date
def prune_cache_file(infile: str, outfile: str, days_before_today):
with open(infile, "r") as f:
json_data = json.load(f)
pruned = {}
today = datetime.date.today()
for key, value in json_data.items():
date = parse_cache_date(key)
delta = (today - date).days
# print(f"{today} - {date}: delta={delta}")
if delta > days_before_today:
pruned[key] = value
with open(outfile, "w") as dstf:
json.dump(pruned, dstf, indent=1, sort_keys=True)
def main(argv=None):
pruned_file = "./sst-data-cache-pruned.json"
class CustomFormatter(
argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter,
):
pass
try:
parser = argparse.ArgumentParser(
description="""Cache-file pruner""",
formatter_class=CustomFormatter,
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="""Process verbosely""",
)
parser.add_argument(
"--days",
"-d",
type=int,
default=180,
help="""Prune limit: days before today -- """
"""most recent N days will be pruned out""",
)
parser.add_argument(
"--inplace",
"-I",
action="store_true",
default=False,
help="""Write pruned result to input file, """
"""replacing it (out file param will be ignored)""",
)
parser.add_argument(
"--out",
"-o",
type=pathlib.Path,
default=pruned_file,
help="""Write pruned result to this file""",
)
parser.add_argument(
"--in",
"-i",
type=pathlib.Path,
dest="infile", # needed since "in" is a keyword
default="./sst-data-cache.json",
help="""Input cache file""",
)
args = parser.parse_args(argv)
if args.inplace:
args.out = args.infile
prune_cache_file(args.infile, args.out, args.days)
except RuntimeError as e:
print(e)
return 1
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))