forked from kuba-moo/nipa
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pw_upload.py
executable file
·216 lines (167 loc) · 6.62 KB
/
pw_upload.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
212
213
214
215
216
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2019 Netronome Systems, Inc.
# Copyright (c) 2020 Facebook
import configparser
import os
import signal
import inotify_simple as inotify
from core import NIPA_DIR
from core import log, log_open_sec, log_end_sec, log_init
from pw import Patchwork, PatchworkCheckState
# TODO: document
should_stop = False
def handler(signum, _):
global should_stop
print('Signal handler called with signal', signum)
should_stop = True
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
class PwTestResult:
def __init__(self, test_name: str, root_dir: str, url: str):
self.test = test_name
self.url = url
try:
with open(os.path.join(root_dir, test_name, "retcode"), "r") as f:
retcode = f.read()
if retcode == "0":
self.state = PatchworkCheckState.SUCCESS
elif retcode == "250":
self.state = PatchworkCheckState.WARNING
else:
self.state = PatchworkCheckState.FAIL
except FileNotFoundError:
self.state = PatchworkCheckState.FAIL
try:
with open(os.path.join(root_dir, test_name, "desc"), "r") as f:
self.desc = f.read()
except FileNotFoundError:
self.desc = "Link"
def _pw_upload_results(series_dir, pw, config):
series = os.path.basename(series_dir)
result_server = config.get('results', 'server', fallback='https://google.com')
# Collect series checks first
series_results = []
for root, dirs, _ in os.walk(series_dir):
for test in dirs:
if is_int(test):
continue
tr = PwTestResult(test, series_dir, f"{result_server}/{series}/{test}")
series_results.append(tr)
break
log(f"Found {len(series_results)} series results")
for root, dirs, _ in os.walk(series_dir):
for patch in dirs:
if not is_int(patch):
continue
for tr in series_results:
pw.post_check(patch=patch, name=tr.test, state=tr.state, url=tr.url, desc=tr.desc)
patch_dir = os.path.join(root, patch)
for _, test_dirs, _ in os.walk(patch_dir):
for test in test_dirs:
tr = PwTestResult(test, patch_dir, f"{result_server}/{series}/{patch}/{test}")
pw.post_check(patch=patch, name=tr.test, state=tr.state, url=tr.url,
desc=tr.desc)
log(f"Patch {patch} - found {len(test_dirs)} results")
break
break
def pw_upload_results(series_dir, pw, config):
log_open_sec(f'Upload results for {os.path.basename(series_dir)}')
try:
_pw_upload_results(series_dir, pw, config)
finally:
log_end_sec()
def pw_upload_results_cb(series_dir, ctx):
pw_upload_results(series_dir, ctx['pw'], ctx['config'])
class TestWatcher(object):
def __init__(self, base_path, trigger, complete, cb, cb_ctx):
self.base_path = base_path
self.trigger = trigger
self.complete = complete
self.cb = cb
self.cb_ctx = cb_ctx
self.wd2name = {}
self.inotify = inotify.INotify()
self.main_wd = None
def _complete_dir(self, wd):
log(f"Dir {self.wd2name[wd]} ({wd}) has been processed", "")
self.inotify.rm_watch(wd)
self.wd2name.pop(wd)
def _trigger_dir(self, name):
# Double check if completed, we can come here from notification
# after initial scan already processed the trigger
complete = os.path.join(self.base_path, name, self.complete)
if os.path.exists(complete):
log(f'Dir {name} already processed', '')
return
log(f"Trigger for dir {name}", "")
self.cb(os.path.join(self.base_path, name), self.cb_ctx)
os.mknod(complete)
def _handle_new_dir(self, name):
path = os.path.join(self.base_path, name)
trigger = os.path.join(path, self.trigger)
complete = os.path.join(path, self.complete)
# Fast path already processed, assume we're the only entity
# creating 'complete' markers
if os.path.exists(complete):
log(f'Dir {name} already processed', '')
return
# Install the watch, to avoid race conditions with the check
wd = self.inotify.add_watch(path, inotify.flags.CREATE)
self.wd2name[wd] = name
log(f"New watch: {wd} => {name}", '')
if os.path.exists(trigger):
self._trigger_dir(name)
def initial_scan(self):
# Install the watch first
flags = inotify.flags.CREATE | inotify.flags.ISDIR
self.main_wd = self.inotify.add_watch(self.base_path, flags)
self.wd2name[self.main_wd] = ''
# Then scan the fs tree
for root, dirs, _ in os.walk(self.base_path):
for d in dirs:
self._handle_new_dir(d)
break
def watch(self):
global should_stop
if self.main_wd is None:
raise Exception('Not initialized')
while not should_stop:
for event in self.inotify.read(timeout=2):
if event.mask & inotify.flags.IGNORED or \
event.wd < 0 or \
event.wd not in self.wd2name:
continue
if event.wd == self.main_wd:
if event.mask & inotify.flags.ISDIR:
self._handle_new_dir(event.name)
else: # subdir
print(f'File event for {self.wd2name[event.wd]} => {event.name}')
if event.name == self.trigger:
self._trigger_dir(self.wd2name[event.wd])
elif event.name == self.complete:
self._complete_dir(event.wd)
def main():
# Init state
config = configparser.ConfigParser()
config.read(['nipa.config', 'pw.config', 'upload.config'])
log_init(config.get('log', 'type', fallback='org'),
config.get('log', 'file', fallback=os.path.join(NIPA_DIR, "upload.org")),
force_single_thread=True)
results_dir = config.get('results', 'dir', fallback=os.path.join(NIPA_DIR, "results"))
pw = Patchwork(config)
signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)
tw = TestWatcher(results_dir, '.tester_done', '.pw_done', pw_upload_results_cb, {
'pw': pw,
'config': config
})
tw.initial_scan()
tw.watch()
if __name__ == "__main__":
main()