-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiuparts-master.py
231 lines (183 loc) · 6.81 KB
/
piuparts-master.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/python
#
# Copyright 2005 Lars Wirzenius ([email protected])
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""Distributed piuparts processing, master program
Lars Wirzenius <[email protected]>
"""
import sys
import logging
import ConfigParser
import os
import fcntl
import piupartslib
CONFIG_FILE = "/etc/piuparts/piuparts.conf"
def setup_logging(log_level, log_file_name):
logger = logging.getLogger()
logger.setLevel(log_level)
if log_file_name:
handler = logging.FileHandler(log_file_name)
else:
handler = logging.StreamHandler(sys.stderr)
logger.addHandler(handler)
class Config(piupartslib.conf.Config):
def __init__(self, section="master"):
piupartslib.conf.Config.__init__(self, section,
{
"log-file": None,
"packages-url": None,
"master-directory": ".",
}, "")
class CommandSyntaxError(Exception):
def __init__(self, msg):
self.args = msg
class ProtocolError(Exception):
def __init__(self):
self.args = "EOF, missing space in long part, or other protocol error"
class Protocol:
def __init__(self, input, output):
self._input = input
self._output = output
def _readline(self):
line = self._input.readline()
logging.debug(">> " + line.rstrip())
return line
def _writeline(self, line):
logging.debug("<< " + line)
self._output.write(line + "\n")
self._output.flush()
def _short_response(self, *words):
self._writeline(" ".join(words))
def _read_long_part(self):
lines = []
while True:
line = self._readline()
if not line:
raise ProtocolError()
if line == ".\n":
break
if line[0] != " ":
raise ProtocolError()
lines.append(line[1:])
return "".join(lines)
class Master(Protocol):
_failed_states = (
"failed-testing",
)
_passed_states = (
"successfully-tested",
)
def __init__(self, input, output, packages_file, section=None):
Protocol.__init__(self, input, output)
self._commands = {
"status": self._status,
"reserve": self._reserve,
"unreserve": self._unreserve,
"pass": self._pass,
"fail": self._fail,
"untestable": self._untestable,
}
self._binary_db = piupartslib.packagesdb.PackagesDB(prefix=section)
self._binary_db.create_subdirs()
self._binary_db.read_packages_file(packages_file)
self._writeline("hello")
def do_transaction(self):
line = self._readline()
if line:
parts = line.split()
if len(parts) > 0:
command = parts[0]
args = parts[1:]
self._commands[command](command, args)
return True
else:
return False
def _check_args(self, count, command, args):
if len(args) != count:
raise CommandSyntaxError("Need exactly %d args: %s %s" %
(count, command, " ".join(args)))
def dump_pkgs(self):
for st in self._binary_db.get_states():
for name in self._binary_db.get_pkg_names_in_state(st):
logging.debug("%s : %s\n" % (st,name))
def _status(self, command, args):
self._check_args(0, command, args)
stats = ""
total = 0
for state in self._binary_db.get_states():
count = len(self._binary_db.get_pkg_names_in_state(state))
total += count
stats += "%s=%d " % (state, count)
stats += "total=%d" % total
self._short_response("ok", stats)
def _reserve(self, command, args):
self._check_args(0, command, args)
package = self._binary_db.reserve_package()
if package is None:
self._short_response("error")
else:
self._short_response("ok",
package["Package"],
package["Version"])
def _unreserve(self, command, args):
self._check_args(2, command, args)
self._binary_db.unreserve_package(args[0], args[1])
self._short_response("ok")
def _pass(self, command, args):
self._check_args(2, command, args)
log = self._read_long_part()
self._binary_db.pass_package(args[0], args[1], log)
self._short_response("ok")
def _fail(self, command, args):
self._check_args(2, command, args)
log = self._read_long_part()
self._binary_db.fail_package(args[0], args[1], log)
self._short_response("ok")
def _untestable(self, command, args):
self._check_args(2, command, args)
log = self._read_long_part()
self._binary_db.make_package_untestable(args[0], args[1], log)
self._short_response("ok")
def main():
# piuparts-master is always called by the slave with a section as argument
if len(sys.argv) == 2:
global_config = Config(section="global")
global_config.read(CONFIG_FILE)
master_directory = global_config["master-directory"]
section = sys.argv[1]
config = Config(section=section)
config.read(CONFIG_FILE)
setup_logging(logging.DEBUG, config["log-file"])
if not os.path.exists(os.path.join(master_directory, section)):
os.makedirs(os.path.join(master_directory, section))
lock = open(os.path.join(master_directory, section, "master.lock"), "we")
try:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
print 'busy'
sys.exit(1)
logging.info("Fetching %s" % config["packages-url"])
packages_file = piupartslib.open_packages_url(config["packages-url"])
m = Master(sys.stdin, sys.stdout, packages_file, section=section)
while m.do_transaction():
pass
packages_file.close()
else:
print 'piuparts-master needs to be called with a valid sectionname as argument, exiting...'
sys.exit(1)
if __name__ == "__main__":
main()
# vi:set et ts=4 sw=4 :