-
Notifications
You must be signed in to change notification settings - Fork 0
/
repod.py
executable file
·201 lines (183 loc) · 6.85 KB
/
repod.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# repod.py: Automatic management tool for an arch repo.
# This file is part of Buildbot by JerryXiao
import logging
from multiprocessing.connection import Listener
from time import time, sleep
from pathlib import Path
from subprocess import CalledProcessError
import os
from config import REPOD_BIND_ADDRESS, REPOD_BIND_PASSWD, REPO_PUSH_BANDWIDTH, \
GPG_VERIFY_CMD
from shared_vars import PKG_SUFFIX, PKG_SIG_SUFFIX
from repo import _clean_archive as clean, \
_regenerate as regenerate, \
_remove as remove, \
_update as update
from utils import bash, configure_logger, print_exc_plus
abspath=os.path.abspath(__file__)
abspath=os.path.dirname(abspath)
os.chdir(abspath)
logger = logging.getLogger('buildbot')
configure_logger(logger, logfile='repod.log', rotate_size=1024*1024*10, enable_notify=True)
class pushFm:
def __init__(self):
self.fnames = list()
self.size = None
self.sizes = None
self.start_time = None
self.end_time = None
def start(self, fnames, sizes):
'''
sizes is list in MB
returns -1 when busy
'''
if self.is_busy():
return -1
self.fnames = fnames
self.start_time = time()
self.sizes = sizes
size = 0
for s in sizes:
size += s
self.size = size
def get_timeout(size):
if size <= 7.5:
timeout = 120
else:
timeout = size / (REPO_PUSH_BANDWIDTH / 8) * 2
return timeout
timeouts = [get_timeout(s) for s in sizes]
self.end_time = self.start_time + get_timeout(self.size)
return timeouts
def tick(self):
'''
return None means success
else returns an error string
'''
if self.is_busy():
if time() > self.end_time:
ret = f'files {self.fnames} are supposed to finish at {self.end_time}'
self.__init__()
logger.error(f'tick: {ret}')
return ret
else:
return None
else:
return None
def fail(self, tfname):
update_path = Path('updates')
if tfname in self.fnames:
for fname in self.fnames:
pkg = update_path / fname
sig = update_path / f'{fname}.sig'
for f in (pkg, sig):
if f.exists():
try:
f.unlink()
except Exception:
logger.warning(f'unable to remove {f.name}')
self.__init__()
return None
else:
return "Wrong file"
def add_time(self, tfname, atime):
if tfname in self.fnames:
assert type(atime) in (int, float)
self.end_time += atime
return None
else:
return "Wrong file"
def done(self, fnames, overwrite=False):
'''
return None means success
else returns an error string
'''
if [f for f in fnames if not (f.endswith(PKG_SUFFIX) or f.endswith(PKG_SIG_SUFFIX))]:
return "files to upload are garbage"
filter_sig = lambda fnames:[fname for fname in fnames if not fname.endswith(PKG_SIG_SUFFIX)]
if sorted(filter_sig(fnames)) == sorted(filter_sig(self.fnames)):
try:
update_path = Path('updates')
for pkgfname in filter_sig(fnames):
pkg_found = False
sig_found = False
for fpath in update_path.iterdir():
if fpath.is_dir():
continue
if fpath.name == pkgfname:
pkg_found = fpath
elif fpath.name == f'{pkgfname}.sig':
sig_found = fpath
if pkg_found and sig_found:
try:
bash(f'{GPG_VERIFY_CMD} {sig_found} {pkg_found}')
except CalledProcessError:
ret = f'{pkg_found} GPG verify error'
logger.error(ret)
print_exc_plus()
return ret
else:
# gpg verify success
continue
else:
return f'file missing: pkg {pkg_found} sig {sig_found}'
raise RuntimeError("unexpected error")
else:
# gpg verified for all
try:
if update(overwrite=overwrite):
return None
else:
raise RuntimeError('update return false')
except Exception:
print_exc_plus()
return f'{pkg_found} update error'
raise RuntimeError("unexpected error")
finally:
self.__init__()
else:
return "Wrong file"
def is_busy(self):
return bool(self.fnames)
pfm = pushFm()
def push_start(filenames, sizes):
pfm.tick()
return pfm.start(filenames, sizes)
def push_done(filenames, overwrite=False):
return pfm.done(filenames, overwrite=overwrite)
def push_fail(filename):
return pfm.fail(filename)
def push_add_time(filename, atime):
return pfm.add_time(filename, atime)
# server part
def run(funcname, args=list(), kwargs=dict()):
if funcname in ('clean', 'regenerate', 'remove',
'update', 'push_start', 'push_done',
'push_fail', 'push_add_time'):
logger.info('running: %s %s %s', funcname, args, kwargs)
ret = eval(funcname)(*args, **kwargs)
logger.info('done: %s %s',funcname, ret)
return ret
else:
logger.error('unexpected: %s %s %s',funcname, args, kwargs)
return False
if __name__ == '__main__':
logger.info('Buildbot.repod started.')
while True:
try:
with Listener(REPOD_BIND_ADDRESS, authkey=REPOD_BIND_PASSWD) as listener:
with listener.accept() as conn:
logger.debug('connection accepted from %s', listener.last_accepted)
myrecv = conn.recv()
if type(myrecv) is list and len(myrecv) == 3:
(funcname, args, kwargs) = myrecv
funcname = str(funcname)
conn.send(run(funcname, args=args, kwargs=kwargs))
except Exception:
print_exc_plus()
except KeyboardInterrupt:
logger.info('KeyboardInterrupt')
print_exc_plus()
break