]> git.pld-linux.org Git - projects/pld-builder.new.git/blame - PLD_Builder/request_handler.py
- use -p as a short form for --priority
[projects/pld-builder.new.git] / PLD_Builder / request_handler.py
CommitLineData
57b6e61d
MM
1import email
2import string
3import time
4import os
5import StringIO
cc94eb63 6import sys
91f92271 7import fnmatch
57b6e61d
MM
8
9import gpg
10import request
11import log
12import path
c8782384 13import util
6b44a683 14import wrap
bdc3292c 15import status
57b6e61d
MM
16from acl import acl
17from lock import lock
18from bqueue import B_Queue
b5a39692 19from config import config, init_conf
57b6e61d
MM
20
21def check_double_id(id):
22 id_nl = id + "\n"
23
24 ids = open(path.processed_ids_file)
25 for i in ids.xreadlines():
26 if i == id_nl:
b5a39692
MM
27 # FIXME: security email here?
28 log.alert("request %s already processed" % id)
57b6e61d
MM
29 return 1
30 ids.close()
31
32 ids = open(path.processed_ids_file, "a")
33 ids.write(id_nl)
34 ids.close()
35
36 return 0
37
34738db3 38def handle_group(r, user):
b5a39692
MM
39 def fail_mail(msg):
40 if len(r.batches) >= 1:
41 spec = r.batches[0].spec
42 else:
43 spec = "None.spec"
44 log.error("%s: %s" % (spec, msg))
45 m = user.message_to()
46 m.set_headers(subject = "building %s failed" % spec)
47 m.write_line(msg)
48 m.send()
49
57b6e61d 50 lock("request")
57b6e61d
MM
51 if check_double_id(r.id):
52 return
53
91f92271 54
2c21338e
JK
55 for batch in r.batches:
56 if not user.can_do("src", config.builder, batch.branch):
57 fail_mail("user %s is not allowed to src:%s:%s" \
58 % (user.get_login(), config.builder, batch.branch))
59 return
d2c1ec3f 60
2c21338e
JK
61 if "upgrade" in r.flags and not user.can_do("upgrade", config.builder, batch.branch):
62 fail_mail("user %s is not allowed to upgrade:%s:%s" \
63 % (user.get_login(), config.builder, batch.branch))
64 return
d2c1ec3f 65
2ba00366 66 batch.expand_builders(config.binary_builders)
6953ce5d 67 if not batch.is_command() and config.builder in batch.builders:
1089bec4 68 batch.builders.remove(config.builder)
91f92271 69 for bld in batch.builders:
1089bec4 70 batch.builders_status[bld] = '?'
7751018d 71 if bld not in config.binary_builders and bld != config.builder:
9e8a7e70
MM
72 fail_mail("I (src rpm builder '%s') do not handle binary builder '%s', only '%s'" % \
73 (config.builder, bld, string.join(config.binary_builders)))
7876147e 74 return
6953ce5d
MM
75 if batch.is_command():
76 if not user.can_do("command", bld):
77 fail_mail("user %s is not allowed to command:%s" \
78 % (user.get_login(), bld))
79 return
2c21338e 80 elif not user.can_do("binary", bld, batch.branch):
0c3c935f
MM
81 pkg = batch.spec
82 if pkg.endswith(".spec"):
83 pkg = pkg[:-5]
2c21338e
JK
84 if not user.can_do("binary-" + pkg, bld, batch.branch):
85 fail_mail("user %s is not allowed to binary-%s:%s:%s" \
86 % (user.get_login(), pkg, bld, batch.branch))
0c3c935f 87 return
60e340de
JK
88
89 r.priority = user.check_priority(r.priority,config.builder)
34738db3 90 r.requester = user.get_login()
e2cad913 91 r.requester_email = user.mail_to()
57b6e61d
MM
92 r.time = time.time()
93 log.notice("queued %s from %s" % (r.id, user.get_login()))
94 q = B_Queue(path.queue_file)
95 q.lock(0)
96 q.read()
97 q.add(r)
98 q.write()
99 q.unlock()
100
59ce7cd6
MM
101def handle_notification(r, user):
102 if not user.can_do("notify", r.builder):
103 log.alert("user %s is not allowed to notify:%s" % (user.login, r.builder))
104 q = B_Queue(path.req_queue_file)
105 q.lock(0)
106 q.read()
107 not_fin = filter(lambda (r): not r.is_done(), q.requests)
108 r.apply_to(q)
109 for r in not_fin:
110 if r.is_done():
f50e2253 111 util.clean_tmp(path.srpms_dir + r.id)
59ce7cd6
MM
112 now = time.time()
113 def leave_it(r):
e0caa85a 114 # for ,,done'' set timeout to 4d
497fae49 115 if r.is_done() and r.time + 4 * 24 * 60 * 60 < now:
5fa612dd 116 return False
e0caa85a 117 # and for not ,,done'' set it to 20d
497fae49 118 if r.time + 20 * 24 * 60 * 60 < now:
e0caa85a 119 util.clean_tmp(path.srpms_dir + r.id)
5fa612dd
MM
120 return False
121 return True
59ce7cd6
MM
122 q.requests = filter(leave_it, q.requests)
123 q.write()
124 q.dump(open(path.queue_stats_file, "w"))
0cdc1fef
MM
125 q.dump_html(open(path.queue_html_stats_file, "w"))
126 os.chmod(path.queue_html_stats_file, 0644)
9a8908ac 127 os.chmod(path.queue_stats_file, 0644)
59ce7cd6
MM
128 q.unlock()
129
57b6e61d
MM
130def handle_request(f):
131 sio = StringIO.StringIO()
4d9b6f71 132 util.sendfile(f, sio)
57b6e61d
MM
133 sio.seek(0)
134 (em, body) = gpg.verify_sig(sio)
2e33eed1 135 user = acl.user_by_email(em)
57b6e61d
MM
136 if user == None:
137 # FIXME: security email here
138 log.alert("invalid signature, or not in acl %s" % em)
139 return
6b44a683 140 acl.set_current_user(user)
bdc3292c 141 status.push("email from %s" % user.login)
57b6e61d 142 r = request.parse_request(body)
57b6e61d 143 if r.kind == 'group':
34738db3 144 handle_group(r, user)
59ce7cd6
MM
145 elif r.kind == 'notification':
146 handle_notification(r, user)
57b6e61d
MM
147 else:
148 msg = "%s: don't know how to handle requests of this kind '%s'" \
149 % (user.get_login(), r.kind)
150 log.alert(msg)
b5a39692
MM
151 m = user.message_to()
152 m.set_headers(subject = "unknown request")
153 m.write_line(msg)
154 m.send()
bdc3292c 155 status.pop()
cc94eb63 156
1f62bccc 157def main():
b5a39692 158 init_conf("src")
bdc3292c 159 status.push("handling email request")
cc94eb63 160 handle_request(sys.stdin)
bdc3292c 161 status.pop()
cc94eb63 162 sys.exit(0)
1f62bccc 163
e8ee9db8
MM
164if __name__ == '__main__':
165 wrap.wrap(main)
This page took 0.066344 seconds and 4 git commands to generate.