-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnapshot_updater.py
66 lines (51 loc) · 1.93 KB
/
snapshot_updater.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
import json
import re
import argparse
from datetime import datetime, timedelta
from os import listdir
class SnapshotUpdater:
def __init__(self, exp_time):
self.expiration_time = timedelta(days=int(exp_time))
def add_timedelta(self, m):
parsed_time = datetime.strptime(m.group(1), "%Y-%m-%dT%H:%M:%S.%f") \
+ self.expiration_time
return parsed_time.isoformat(timespec="milliseconds")
def update_time(self, actions, time_prefix):
return re.sub(
f"{time_prefix}_time\":\"(.*?)Z",
lambda m: f"{time_prefix}_time\":\"{self.add_timedelta(m)}Z",
actions
)
@staticmethod
def read_snapshot(snapshot_file):
with open(snapshot_file) as file:
snapshot_data = json.load(file)
return snapshot_data
@staticmethod
def write_snapshot(snapshot_file, snapshot_data):
with open(snapshot_file, mode="w") as file:
json.dump(snapshot_data, file)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--integration", required=True)
parser.add_argument("--expiration_time", required=True)
args = parser.parse_args()
snapshots_dir = [
elem for elem in listdir() if
elem == args.integration
]
snapshot_updater = SnapshotUpdater(args.expiration_time)
for snapshot in listdir(snapshots_dir[0]):
path_to_snapshot = f"{snapshots_dir[0]}/{snapshot}"
try:
data = snapshot_updater.read_snapshot(path_to_snapshot)
except json.JSONDecodeError:
print(f"Error: File '{snapshot}' is malformed.")
continue
actions = data["actions"]
actions = snapshot_updater.update_time(actions, "start")
actions = snapshot_updater.update_time(actions, "end")
data["actions"] = actions
snapshot_updater.write_snapshot(path_to_snapshot, data)
if __name__ == '__main__':
main()