-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.py
211 lines (170 loc) · 5.61 KB
/
common.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# Copyright(c) 2023 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#
from contextlib import contextmanager
from datetime import timedelta
from filelock import FileLock
from json.decoder import JSONDecodeError
import hashlib
import json
import os
import random
import re
import sys
import time
import yaml
meta_lock = FileLock("meta/runner.lock")
class ConfigFile:
def __init__(self, path):
self.path = os.path.abspath(path)
self.last_modify = 0
def __access(self):
self.last_modify = os.path.getmtime(self.path)
def need_reload(self):
return self.last_modify != os.path.getmtime(self.path)
def load(self):
with meta_lock:
self.__access()
with open(self.path, 'r') as conf:
return yaml.safe_load(conf)
def save(self, data):
with meta_lock:
self.__access()
with open(self.path, 'w') as conf:
return yaml.dump(data, conf)
class JournalFile:
def __init__(self, path):
self.path = os.path.abspath(path)
self.last_modify = 0
def __access(self):
self.last_modify = os.path.getmtime(self.path)
def create(self):
with self.record():
pass
def need_reload(self):
if not os.path.isfile(self.path):
return False
return self.last_modify != os.path.getmtime(self.path)
def load(self):
with meta_lock:
if not os.path.isfile(self.path):
return []
self.__access()
with open(self.path, 'r') as journal_file:
return json.load(journal_file)
@contextmanager
def record(self):
with meta_lock:
with open(self.path, 'a+') as journal_file:
self.__access()
try:
journal_file.seek(0)
journal = json.load(journal_file)
except JSONDecodeError:
journal = []
new_entries = []
yield new_entries
journal.extend(new_entries)
journal_file.truncate(0)
json.dump(journal, journal_file)
class StatusFile:
def __init__(self, path):
self.path = os.path.abspath(path)
def create(self):
with self.edit():
pass
def load(self):
with meta_lock:
if not os.path.isfile(self.path):
return {}
with open(self.path, 'r') as status_file:
return json.load(status_file)
@contextmanager
def edit(self):
with meta_lock:
with open(self.path, 'a+') as status_file:
try:
status_file.seek(0)
status = json.load(status_file)
except JSONDecodeError:
status = {}
yield status
status_file.truncate(0)
json.dump(status, status_file)
class TestCase(dict):
def __init__(self, data):
super().__init__(data)
if 'sha' not in self:
signature = self.signature().encode("UTF-8")
self['sha'] = hashlib.sha1(signature).hexdigest().upper()
def signature(self):
return f"{self['dir']}|{self}|{self['seed']}"
def function(self):
if self['params']:
return f"{self['name']}[{self['params']}]"
else:
return f"{self['name']}"
def test(self):
return f"{self['path']}::{self.function()}"
def __hash__(self):
return hash(self.signature())
def __eq__(self, other):
return self.signature() == other.signature()
def __repr__(self):
return self.test()
def __str__(self):
return self.test()
@classmethod
def from_canonical_name(cls, directory, canon_name, seed, pytest_options):
m = re.fullmatch(r'(\S+)::([^\[]+)\[?([^\]]+)?\]?', canon_name)
path, name, params = m.groups()
return cls({
'dir': directory,
'path': path,
'name': name,
'params': params,
'seed': seed,
'pytest-options': pytest_options
})
class TestEvent(dict):
def __init__(self, data):
super().__init__(data)
self['test-case'] = TestCase(self['test-case'])
def signature(self):
return f"{self['test-case'].signature()}|{self['sha']}"
def duration(self):
try:
start_time = self['start-timestamp']
end_time = self.get('end-timestamp', time.time())
return timedelta(seconds=int(end_time-start_time))
except:
return timedelta(0)
def __eq__(self, other):
return self.signature() == other.signature()
def __repr__(self):
return self.__str__()
def __str__(self):
return f"<{self['sha']}>{self['test-case']}"
@classmethod
def new(cls, test_case, data={}):
signature = f"{test_case.signature()}{time.time()}".encode("UTF-8")
return cls({
**{
'test-case': test_case,
'sha': hashlib.sha1(signature).hexdigest()
},
**data
})
class JournalParser:
def __init__(self, journal_file):
self.journal_file = journal_file
def parse(self):
journal_dict = {}
for entry in self.journal_file.load():
test_event = TestEvent(entry['test-event'])
if entry['type'] == "add":
journal_dict[test_event['sha']] = test_event
elif entry['type'] == "delete":
del journal_dict[test_event['sha']]
return list(journal_dict.values())