1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | """Model classes for objects persisted in the database.""" |
---|
12 | |
---|
13 | from trac.attachment import Attachment |
---|
14 | from trac.db import Table, Column, Index |
---|
15 | from trac.resource import Resource |
---|
16 | from trac.util.text import to_unicode |
---|
17 | from trac.util.datefmt import to_timestamp, utcmin, utcmax |
---|
18 | from datetime import datetime |
---|
19 | import codecs |
---|
20 | import os |
---|
21 | from bitten.util.repository import get_repos |
---|
22 | |
---|
23 | __docformat__ = 'restructuredtext en' |
---|
24 | |
---|
25 | |
---|
26 | class BuildConfig(object): |
---|
27 | """Representation of a build configuration.""" |
---|
28 | |
---|
29 | _schema = [ |
---|
30 | Table('bitten_config', key='name')[ |
---|
31 | Column('name'), Column('path'), Column('active', type='int'), |
---|
32 | Column('recipe'), Column('min_rev'), Column('max_rev'), |
---|
33 | Column('label'), Column('description') |
---|
34 | ] |
---|
35 | ] |
---|
36 | |
---|
37 | def __init__(self, env, name=None, path=None, active=False, recipe=None, |
---|
38 | min_rev=None, max_rev=None, label=None, description=None): |
---|
39 | """Initialize a new build configuration with the specified attributes. |
---|
40 | |
---|
41 | To actually create this configuration in the database, the `insert` |
---|
42 | method needs to be called. |
---|
43 | """ |
---|
44 | self.env = env |
---|
45 | self._old_name = None |
---|
46 | self.name = name |
---|
47 | self.path = path or '' |
---|
48 | self.active = bool(active) |
---|
49 | self.recipe = recipe or '' |
---|
50 | self.min_rev = min_rev or None |
---|
51 | self.max_rev = max_rev or None |
---|
52 | self.label = label or '' |
---|
53 | self.description = description or '' |
---|
54 | |
---|
55 | def __repr__(self): |
---|
56 | return '<%s %r>' % (type(self).__name__, self.name) |
---|
57 | |
---|
58 | exists = property(fget=lambda self: self._old_name is not None, |
---|
59 | doc='Whether this configuration exists in the database') |
---|
60 | resource = property(fget=lambda self: Resource('build', '%s' % self.name), |
---|
61 | doc='Build Config resource identification') |
---|
62 | |
---|
63 | def delete(self, db=None): |
---|
64 | """Remove a build configuration and all dependent objects from the |
---|
65 | database.""" |
---|
66 | assert self.exists, 'Cannot delete non-existing configuration' |
---|
67 | if not db: |
---|
68 | db = self.env.get_db_cnx() |
---|
69 | handle_ta = True |
---|
70 | else: |
---|
71 | handle_ta = False |
---|
72 | |
---|
73 | for platform in list(TargetPlatform.select(self.env, self.name, db=db)): |
---|
74 | platform.delete(db=db) |
---|
75 | |
---|
76 | for build in list(Build.select(self.env, config=self.name, db=db)): |
---|
77 | build.delete(db=db) |
---|
78 | |
---|
79 | # Delete attachments |
---|
80 | Attachment.delete_all(self.env, 'build', self.resource.id, db) |
---|
81 | |
---|
82 | cursor = db.cursor() |
---|
83 | cursor.execute("DELETE FROM bitten_config WHERE name=%s", (self.name,)) |
---|
84 | |
---|
85 | if handle_ta: |
---|
86 | db.commit() |
---|
87 | self._old_name = None |
---|
88 | |
---|
89 | def insert(self, db=None): |
---|
90 | """Insert a new configuration into the database.""" |
---|
91 | assert not self.exists, 'Cannot insert existing configuration' |
---|
92 | assert self.name, 'Configuration requires a name' |
---|
93 | if not db: |
---|
94 | db = self.env.get_db_cnx() |
---|
95 | handle_ta = True |
---|
96 | else: |
---|
97 | handle_ta = False |
---|
98 | |
---|
99 | cursor = db.cursor() |
---|
100 | cursor.execute("INSERT INTO bitten_config (name,path,active," |
---|
101 | "recipe,min_rev,max_rev,label,description) " |
---|
102 | "VALUES (%s,%s,%s,%s,%s,%s,%s,%s)", |
---|
103 | (self.name, self.path, int(self.active or 0), |
---|
104 | self.recipe or '', self.min_rev, self.max_rev, |
---|
105 | self.label or '', self.description or '')) |
---|
106 | |
---|
107 | if handle_ta: |
---|
108 | db.commit() |
---|
109 | self._old_name = self.name |
---|
110 | |
---|
111 | def update(self, db=None): |
---|
112 | """Save changes to an existing build configuration.""" |
---|
113 | assert self.exists, 'Cannot update a non-existing configuration' |
---|
114 | assert self.name, 'Configuration requires a name' |
---|
115 | if not db: |
---|
116 | db = self.env.get_db_cnx() |
---|
117 | handle_ta = True |
---|
118 | else: |
---|
119 | handle_ta = False |
---|
120 | |
---|
121 | cursor = db.cursor() |
---|
122 | cursor.execute("UPDATE bitten_config SET name=%s,path=%s,active=%s," |
---|
123 | "recipe=%s,min_rev=%s,max_rev=%s,label=%s," |
---|
124 | "description=%s WHERE name=%s", |
---|
125 | (self.name, self.path, int(self.active or 0), |
---|
126 | self.recipe, self.min_rev, self.max_rev, |
---|
127 | self.label, self.description, self._old_name)) |
---|
128 | if self.name != self._old_name: |
---|
129 | cursor.execute("UPDATE bitten_platform SET config=%s " |
---|
130 | "WHERE config=%s", (self.name, self._old_name)) |
---|
131 | cursor.execute("UPDATE bitten_build SET config=%s " |
---|
132 | "WHERE config=%s", (self.name, self._old_name)) |
---|
133 | |
---|
134 | if handle_ta: |
---|
135 | db.commit() |
---|
136 | self._old_name = self.name |
---|
137 | |
---|
138 | def fetch(cls, env, name, db=None): |
---|
139 | """Retrieve an existing build configuration from the database by |
---|
140 | name. |
---|
141 | """ |
---|
142 | if not db: |
---|
143 | db = env.get_db_cnx() |
---|
144 | |
---|
145 | cursor = db.cursor() |
---|
146 | cursor.execute("SELECT path,active,recipe,min_rev,max_rev,label," |
---|
147 | "description FROM bitten_config WHERE name=%s", (name,)) |
---|
148 | row = cursor.fetchone() |
---|
149 | if not row: |
---|
150 | return None |
---|
151 | |
---|
152 | config = BuildConfig(env) |
---|
153 | config.name = config._old_name = name |
---|
154 | config.path = row[0] or '' |
---|
155 | config.active = bool(row[1]) |
---|
156 | config.recipe = row[2] or '' |
---|
157 | config.min_rev = row[3] or None |
---|
158 | config.max_rev = row[4] or None |
---|
159 | config.label = row[5] or '' |
---|
160 | config.description = row[6] or '' |
---|
161 | return config |
---|
162 | |
---|
163 | fetch = classmethod(fetch) |
---|
164 | |
---|
165 | def select(cls, env, include_inactive=False, db=None): |
---|
166 | """Retrieve existing build configurations from the database that match |
---|
167 | the specified criteria. |
---|
168 | """ |
---|
169 | if not db: |
---|
170 | db = env.get_db_cnx() |
---|
171 | |
---|
172 | cursor = db.cursor() |
---|
173 | if include_inactive: |
---|
174 | cursor.execute("SELECT name,path,active,recipe,min_rev,max_rev," |
---|
175 | "label,description FROM bitten_config ORDER BY name") |
---|
176 | else: |
---|
177 | cursor.execute("SELECT name,path,active,recipe,min_rev,max_rev," |
---|
178 | "label,description FROM bitten_config " |
---|
179 | "WHERE active=1 ORDER BY name") |
---|
180 | for name, path, active, recipe, min_rev, max_rev, label, description \ |
---|
181 | in cursor: |
---|
182 | config = BuildConfig(env, name=name, path=path or '', |
---|
183 | active=bool(active), recipe=recipe or '', |
---|
184 | min_rev=min_rev or None, |
---|
185 | max_rev=max_rev or None, label=label or '', |
---|
186 | description=description or '') |
---|
187 | config._old_name = name |
---|
188 | yield config |
---|
189 | |
---|
190 | select = classmethod(select) |
---|
191 | |
---|
192 | def min_rev_time(self, env): |
---|
193 | """Returns the time of the minimum revision being built for this |
---|
194 | configuration. Returns utcmin if not specified. |
---|
195 | """ |
---|
196 | _name, repos, _path = get_repos(env, self.path, None) |
---|
197 | min_time = utcmin |
---|
198 | if self.min_rev: |
---|
199 | min_time = repos.get_changeset(self.min_rev).date |
---|
200 | |
---|
201 | if isinstance(min_time, datetime): # Trac>=0.11 |
---|
202 | min_time = to_timestamp(min_time) |
---|
203 | |
---|
204 | return min_time |
---|
205 | |
---|
206 | def max_rev_time(self, env): |
---|
207 | """Returns the time of the maximum revision being built for this |
---|
208 | configuration. Returns utcmax if not specified. |
---|
209 | """ |
---|
210 | _name, repos, _path = get_repos(env, self.path, None) |
---|
211 | max_time = utcmax |
---|
212 | if self.max_rev: |
---|
213 | max_time = repos.get_changeset(self.max_rev).date |
---|
214 | |
---|
215 | if isinstance(max_time, datetime): # Trac>=0.11 |
---|
216 | max_time = to_timestamp(max_time) |
---|
217 | |
---|
218 | return max_time |
---|
219 | |
---|
220 | |
---|
221 | class TargetPlatform(object): |
---|
222 | """Target platform for a build configuration.""" |
---|
223 | |
---|
224 | _schema = [ |
---|
225 | Table('bitten_platform', key='id')[ |
---|
226 | Column('id', auto_increment=True), Column('config'), Column('name') |
---|
227 | ], |
---|
228 | Table('bitten_rule', key=('id', 'propname'))[ |
---|
229 | Column('id', type='int'), Column('propname'), Column('pattern'), |
---|
230 | Column('orderno', type='int') |
---|
231 | ] |
---|
232 | ] |
---|
233 | |
---|
234 | def __init__(self, env, config=None, name=None): |
---|
235 | """Initialize a new target platform with the specified attributes. |
---|
236 | |
---|
237 | To actually create this platform in the database, the `insert` method |
---|
238 | needs to be called. |
---|
239 | """ |
---|
240 | self.env = env |
---|
241 | self.id = None |
---|
242 | self.config = config |
---|
243 | self.name = name |
---|
244 | self.rules = [] |
---|
245 | |
---|
246 | def __repr__(self): |
---|
247 | return '<%s %r>' % (type(self).__name__, self.id) |
---|
248 | |
---|
249 | exists = property(fget=lambda self: self.id is not None, |
---|
250 | doc='Whether this target platform exists in the database') |
---|
251 | |
---|
252 | def delete(self, db=None): |
---|
253 | """Remove the target platform from the database.""" |
---|
254 | if not db: |
---|
255 | db = self.env.get_db_cnx() |
---|
256 | handle_ta = True |
---|
257 | else: |
---|
258 | handle_ta = False |
---|
259 | |
---|
260 | for build in Build.select(self.env, platform=self.id, status=Build.PENDING, db=db): |
---|
261 | build.delete() |
---|
262 | |
---|
263 | cursor = db.cursor() |
---|
264 | cursor.execute("DELETE FROM bitten_rule WHERE id=%s", (self.id,)) |
---|
265 | cursor.execute("DELETE FROM bitten_platform WHERE id=%s", (self.id,)) |
---|
266 | if handle_ta: |
---|
267 | db.commit() |
---|
268 | |
---|
269 | def insert(self, db=None): |
---|
270 | """Insert a new target platform into the database.""" |
---|
271 | if not db: |
---|
272 | db = self.env.get_db_cnx() |
---|
273 | handle_ta = True |
---|
274 | else: |
---|
275 | handle_ta = False |
---|
276 | |
---|
277 | assert not self.exists, 'Cannot insert existing target platform' |
---|
278 | assert self.config, 'Target platform needs to be associated with a ' \ |
---|
279 | 'configuration' |
---|
280 | assert self.name, 'Target platform requires a name' |
---|
281 | |
---|
282 | cursor = db.cursor() |
---|
283 | cursor.execute("INSERT INTO bitten_platform (config,name) " |
---|
284 | "VALUES (%s,%s)", (self.config, self.name)) |
---|
285 | self.id = db.get_last_id(cursor, 'bitten_platform') |
---|
286 | if self.rules: |
---|
287 | cursor.executemany("INSERT INTO bitten_rule VALUES (%s,%s,%s,%s)", |
---|
288 | [(self.id, propname, pattern, idx) |
---|
289 | for idx, (propname, pattern) |
---|
290 | in enumerate(self.rules)]) |
---|
291 | |
---|
292 | if handle_ta: |
---|
293 | db.commit() |
---|
294 | |
---|
295 | def update(self, db=None): |
---|
296 | """Save changes to an existing target platform.""" |
---|
297 | assert self.exists, 'Cannot update a non-existing platform' |
---|
298 | assert self.config, 'Target platform needs to be associated with a ' \ |
---|
299 | 'configuration' |
---|
300 | assert self.name, 'Target platform requires a name' |
---|
301 | if not db: |
---|
302 | db = self.env.get_db_cnx() |
---|
303 | handle_ta = True |
---|
304 | else: |
---|
305 | handle_ta = False |
---|
306 | |
---|
307 | cursor = db.cursor() |
---|
308 | cursor.execute("UPDATE bitten_platform SET name=%s WHERE id=%s", |
---|
309 | (self.name, self.id)) |
---|
310 | cursor.execute("DELETE FROM bitten_rule WHERE id=%s", (self.id,)) |
---|
311 | if self.rules: |
---|
312 | cursor.executemany("INSERT INTO bitten_rule VALUES (%s,%s,%s,%s)", |
---|
313 | [(self.id, propname, pattern, idx) |
---|
314 | for idx, (propname, pattern) |
---|
315 | in enumerate(self.rules)]) |
---|
316 | |
---|
317 | if handle_ta: |
---|
318 | db.commit() |
---|
319 | |
---|
320 | def fetch(cls, env, id, db=None): |
---|
321 | """Retrieve an existing target platform from the database by ID.""" |
---|
322 | if not db: |
---|
323 | db = env.get_db_cnx() |
---|
324 | |
---|
325 | cursor = db.cursor() |
---|
326 | cursor.execute("SELECT config,name FROM bitten_platform " |
---|
327 | "WHERE id=%s", (id,)) |
---|
328 | row = cursor.fetchone() |
---|
329 | if not row: |
---|
330 | return None |
---|
331 | |
---|
332 | platform = TargetPlatform(env, config=row[0], name=row[1]) |
---|
333 | platform.id = id |
---|
334 | cursor.execute("SELECT propname,pattern FROM bitten_rule " |
---|
335 | "WHERE id=%s ORDER BY orderno", (id,)) |
---|
336 | for propname, pattern in cursor: |
---|
337 | platform.rules.append((propname, pattern)) |
---|
338 | return platform |
---|
339 | |
---|
340 | fetch = classmethod(fetch) |
---|
341 | |
---|
342 | def select(cls, env, config=None, db=None): |
---|
343 | """Retrieve existing target platforms from the database that match the |
---|
344 | specified criteria. |
---|
345 | """ |
---|
346 | if not db: |
---|
347 | db = env.get_db_cnx() |
---|
348 | |
---|
349 | where_clauses = [] |
---|
350 | if config is not None: |
---|
351 | where_clauses.append(("config=%s", config)) |
---|
352 | if where_clauses: |
---|
353 | where = "WHERE " + " AND ".join([wc[0] for wc in where_clauses]) |
---|
354 | else: |
---|
355 | where = "" |
---|
356 | |
---|
357 | cursor = db.cursor() |
---|
358 | cursor.execute("SELECT id FROM bitten_platform %s ORDER BY name" |
---|
359 | % where, [wc[1] for wc in where_clauses]) |
---|
360 | for (id,) in cursor: |
---|
361 | yield TargetPlatform.fetch(env, id) |
---|
362 | |
---|
363 | select = classmethod(select) |
---|
364 | |
---|
365 | |
---|
366 | class Build(object): |
---|
367 | """Representation of a build.""" |
---|
368 | |
---|
369 | _schema = [ |
---|
370 | Table('bitten_build', key='id')[ |
---|
371 | Column('id', auto_increment=True), Column('config'), Column('rev'), |
---|
372 | Column('rev_time', type='int'), Column('platform', type='int'), |
---|
373 | Column('slave'), Column('started', type='int'), |
---|
374 | Column('stopped', type='int'), Column('status', size=1), |
---|
375 | Column('last_activity', type='int'), |
---|
376 | Index(['config', 'rev', 'platform'], unique=True) |
---|
377 | ], |
---|
378 | Table('bitten_slave', key=('build', 'propname'))[ |
---|
379 | Column('build', type='int'), Column('propname'), Column('propvalue') |
---|
380 | ] |
---|
381 | ] |
---|
382 | |
---|
383 | # Build status codes |
---|
384 | PENDING = 'P' |
---|
385 | IN_PROGRESS = 'I' |
---|
386 | SUCCESS = 'S' |
---|
387 | FAILURE = 'F' |
---|
388 | |
---|
389 | # Standard slave properties |
---|
390 | IP_ADDRESS = 'ipnr' |
---|
391 | MAINTAINER = 'owner' |
---|
392 | OS_NAME = 'os' |
---|
393 | OS_FAMILY = 'family' |
---|
394 | OS_VERSION = 'version' |
---|
395 | MACHINE = 'machine' |
---|
396 | PROCESSOR = 'processor' |
---|
397 | TOKEN = 'token' |
---|
398 | |
---|
399 | def __init__(self, env, config=None, rev=None, platform=None, slave=None, |
---|
400 | started=0, stopped=0, last_activity=0, |
---|
401 | rev_time=0, status=PENDING): |
---|
402 | """Initialize a new build with the specified attributes. |
---|
403 | |
---|
404 | To actually create this build in the database, the `insert` method needs |
---|
405 | to be called. |
---|
406 | """ |
---|
407 | self.env = env |
---|
408 | self.id = None |
---|
409 | self.config = config |
---|
410 | self.rev = rev and str(rev) or None |
---|
411 | self.platform = platform |
---|
412 | self.slave = slave |
---|
413 | self.started = started or 0 |
---|
414 | self.stopped = stopped or 0 |
---|
415 | self.last_activity = last_activity or 0 |
---|
416 | self.rev_time = rev_time |
---|
417 | self.status = status |
---|
418 | self.slave_info = {} |
---|
419 | |
---|
420 | def __repr__(self): |
---|
421 | return '<%s %r>' % (type(self).__name__, self.id) |
---|
422 | |
---|
423 | exists = property(fget=lambda self: self.id is not None, |
---|
424 | doc='Whether this build exists in the database') |
---|
425 | completed = property(fget=lambda self: self.status != Build.IN_PROGRESS, |
---|
426 | doc='Whether the build has been completed') |
---|
427 | successful = property(fget=lambda self: self.status == Build.SUCCESS, |
---|
428 | doc='Whether the build was successful') |
---|
429 | resource = property(fget=lambda self: Resource('build', '%s/%s' % (self.config, self.id)), |
---|
430 | doc='Build resource identification') |
---|
431 | |
---|
432 | def delete(self, db=None): |
---|
433 | """Remove the build from the database.""" |
---|
434 | assert self.exists, 'Cannot delete a non-existing build' |
---|
435 | if not db: |
---|
436 | db = self.env.get_db_cnx() |
---|
437 | handle_ta = True |
---|
438 | else: |
---|
439 | handle_ta = False |
---|
440 | |
---|
441 | for step in list(BuildStep.select(self.env, build=self.id)): |
---|
442 | step.delete(db=db) |
---|
443 | |
---|
444 | # Delete attachments |
---|
445 | Attachment.delete_all(self.env, 'build', self.resource.id, db) |
---|
446 | |
---|
447 | cursor = db.cursor() |
---|
448 | cursor.execute("DELETE FROM bitten_slave WHERE build=%s", (self.id,)) |
---|
449 | cursor.execute("DELETE FROM bitten_build WHERE id=%s", (self.id,)) |
---|
450 | |
---|
451 | if handle_ta: |
---|
452 | db.commit() |
---|
453 | |
---|
454 | def insert(self, db=None): |
---|
455 | """Insert a new build into the database.""" |
---|
456 | assert not self.exists, 'Cannot insert an existing build' |
---|
457 | if not db: |
---|
458 | db = self.env.get_db_cnx() |
---|
459 | handle_ta = True |
---|
460 | else: |
---|
461 | handle_ta = False |
---|
462 | |
---|
463 | assert self.config and self.rev and self.rev_time and self.platform |
---|
464 | assert self.status in (self.PENDING, self.IN_PROGRESS, self.SUCCESS, |
---|
465 | self.FAILURE) |
---|
466 | if not self.slave: |
---|
467 | assert self.status == self.PENDING |
---|
468 | |
---|
469 | cursor = db.cursor() |
---|
470 | cursor.execute("INSERT INTO bitten_build (config,rev,rev_time,platform," |
---|
471 | "slave,started,stopped,last_activity,status) " |
---|
472 | "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)", |
---|
473 | (self.config, self.rev, int(self.rev_time), |
---|
474 | self.platform, self.slave or '', self.started or 0, |
---|
475 | self.stopped or 0, self.last_activity or 0, |
---|
476 | self.status)) |
---|
477 | self.id = db.get_last_id(cursor, 'bitten_build') |
---|
478 | if self.slave_info: |
---|
479 | cursor.executemany("INSERT INTO bitten_slave VALUES (%s,%s,%s)", |
---|
480 | [(self.id, name, value) for name, value |
---|
481 | in self.slave_info.items()]) |
---|
482 | |
---|
483 | if handle_ta: |
---|
484 | db.commit() |
---|
485 | |
---|
486 | def update(self, db=None): |
---|
487 | """Save changes to an existing build.""" |
---|
488 | assert self.exists, 'Cannot update a non-existing build' |
---|
489 | if not db: |
---|
490 | db = self.env.get_db_cnx() |
---|
491 | handle_ta = True |
---|
492 | else: |
---|
493 | handle_ta = False |
---|
494 | |
---|
495 | assert self.config and self.rev |
---|
496 | assert self.status in (self.PENDING, self.IN_PROGRESS, self.SUCCESS, |
---|
497 | self.FAILURE) |
---|
498 | if not self.slave: |
---|
499 | assert self.status == self.PENDING |
---|
500 | |
---|
501 | cursor = db.cursor() |
---|
502 | cursor.execute("UPDATE bitten_build SET slave=%s,started=%s," |
---|
503 | "stopped=%s,last_activity=%s,status=%s WHERE id=%s", |
---|
504 | (self.slave or '', self.started or 0, |
---|
505 | self.stopped or 0, self.last_activity or 0, |
---|
506 | self.status, self.id)) |
---|
507 | cursor.execute("DELETE FROM bitten_slave WHERE build=%s", (self.id,)) |
---|
508 | if self.slave_info: |
---|
509 | cursor.executemany("INSERT INTO bitten_slave VALUES (%s,%s,%s)", |
---|
510 | [(self.id, name, value) for name, value |
---|
511 | in self.slave_info.items()]) |
---|
512 | if handle_ta: |
---|
513 | db.commit() |
---|
514 | |
---|
515 | def fetch(cls, env, id, db=None): |
---|
516 | """Retrieve an existing build from the database by ID.""" |
---|
517 | if not db: |
---|
518 | db = env.get_db_cnx() |
---|
519 | |
---|
520 | cursor = db.cursor() |
---|
521 | cursor.execute("SELECT config,rev,rev_time,platform,slave,started," |
---|
522 | "stopped,last_activity,status FROM bitten_build WHERE " |
---|
523 | "id=%s", (id,)) |
---|
524 | row = cursor.fetchone() |
---|
525 | if not row: |
---|
526 | return None |
---|
527 | |
---|
528 | build = Build(env, config=row[0], rev=row[1], rev_time=int(row[2]), |
---|
529 | platform=int(row[3]), slave=row[4], |
---|
530 | started=row[5] and int(row[5]) or 0, |
---|
531 | stopped=row[6] and int(row[6]) or 0, |
---|
532 | last_activity=row[7] and int(row[7]) or 0, |
---|
533 | status=row[8]) |
---|
534 | build.id = int(id) |
---|
535 | cursor.execute("SELECT propname,propvalue FROM bitten_slave " |
---|
536 | "WHERE build=%s", (id,)) |
---|
537 | for propname, propvalue in cursor: |
---|
538 | build.slave_info[propname] = propvalue |
---|
539 | return build |
---|
540 | |
---|
541 | fetch = classmethod(fetch) |
---|
542 | |
---|
543 | def select(cls, env, config=None, rev=None, platform=None, slave=None, |
---|
544 | status=None, db=None, min_rev_time=None, max_rev_time=None): |
---|
545 | """Retrieve existing builds from the database that match the specified |
---|
546 | criteria. |
---|
547 | """ |
---|
548 | if not db: |
---|
549 | db = env.get_db_cnx() |
---|
550 | |
---|
551 | where_clauses = [] |
---|
552 | if config is not None: |
---|
553 | where_clauses.append(("config=%s", config)) |
---|
554 | if rev is not None: |
---|
555 | where_clauses.append(("rev=%s", str(rev))) |
---|
556 | if platform is not None: |
---|
557 | where_clauses.append(("platform=%s", platform)) |
---|
558 | if slave is not None: |
---|
559 | where_clauses.append(("slave=%s", slave)) |
---|
560 | if status is not None: |
---|
561 | where_clauses.append(("status=%s", status)) |
---|
562 | if min_rev_time is not None: |
---|
563 | where_clauses.append(("rev_time>=%s", min_rev_time)) |
---|
564 | if max_rev_time is not None: |
---|
565 | where_clauses.append(("rev_time<=%s", max_rev_time)) |
---|
566 | if where_clauses: |
---|
567 | where = "WHERE " + " AND ".join([wc[0] for wc in where_clauses]) |
---|
568 | else: |
---|
569 | where = "" |
---|
570 | |
---|
571 | cursor = db.cursor() |
---|
572 | cursor.execute("SELECT id FROM bitten_build %s " |
---|
573 | "ORDER BY rev_time DESC,config,slave" |
---|
574 | % where, [wc[1] for wc in where_clauses]) |
---|
575 | for (id,) in cursor: |
---|
576 | yield Build.fetch(env, id) |
---|
577 | select = classmethod(select) |
---|
578 | |
---|
579 | |
---|
580 | class BuildStep(object): |
---|
581 | """Represents an individual step of an executed build.""" |
---|
582 | |
---|
583 | _schema = [ |
---|
584 | Table('bitten_step', key=('build', 'name'))[ |
---|
585 | Column('build', type='int'), Column('name'), Column('description'), |
---|
586 | Column('status', size=1), Column('started', type='int'), |
---|
587 | Column('stopped', type='int') |
---|
588 | ], |
---|
589 | Table('bitten_error', key=('build', 'step', 'orderno'))[ |
---|
590 | Column('build', type='int'), Column('step'), Column('message'), |
---|
591 | Column('orderno', type='int') |
---|
592 | ] |
---|
593 | ] |
---|
594 | |
---|
595 | # Step status codes |
---|
596 | SUCCESS = 'S' |
---|
597 | IN_PROGRESS = 'I' |
---|
598 | FAILURE = 'F' |
---|
599 | |
---|
600 | def __init__(self, env, build=None, name=None, description=None, |
---|
601 | status=None, started=None, stopped=None): |
---|
602 | """Initialize a new build step with the specified attributes. |
---|
603 | |
---|
604 | To actually create this build step in the database, the `insert` method |
---|
605 | needs to be called. |
---|
606 | """ |
---|
607 | self.env = env |
---|
608 | self.build = build |
---|
609 | self.name = name |
---|
610 | self.description = description |
---|
611 | self.status = status |
---|
612 | self.started = started |
---|
613 | self.stopped = stopped |
---|
614 | self.errors = [] |
---|
615 | self._exists = False |
---|
616 | |
---|
617 | exists = property(fget=lambda self: self._exists, |
---|
618 | doc='Whether this build step exists in the database') |
---|
619 | successful = property(fget=lambda self: self.status == BuildStep.SUCCESS, |
---|
620 | doc='Whether the build step was successful') |
---|
621 | completed = property(fget=lambda self: self.status == BuildStep.SUCCESS or self.status == BuildStep.FAILURE, |
---|
622 | doc='Whether this build step has completed processing') |
---|
623 | def delete(self, db=None): |
---|
624 | """Remove the build step from the database.""" |
---|
625 | if not db: |
---|
626 | db = self.env.get_db_cnx() |
---|
627 | handle_ta = True |
---|
628 | else: |
---|
629 | handle_ta = False |
---|
630 | |
---|
631 | for log in list(BuildLog.select(self.env, build=self.build, |
---|
632 | step=self.name, db=db)): |
---|
633 | log.delete(db=db) |
---|
634 | for report in list(Report.select(self.env, build=self.build, |
---|
635 | step=self.name, db=db)): |
---|
636 | report.delete(db=db) |
---|
637 | |
---|
638 | cursor = db.cursor() |
---|
639 | cursor.execute("DELETE FROM bitten_step WHERE build=%s AND name=%s", |
---|
640 | (self.build, self.name)) |
---|
641 | cursor.execute("DELETE FROM bitten_error WHERE build=%s AND step=%s", |
---|
642 | (self.build, self.name)) |
---|
643 | |
---|
644 | if handle_ta: |
---|
645 | db.commit() |
---|
646 | self._exists = False |
---|
647 | |
---|
648 | def insert(self, db=None): |
---|
649 | """Insert a new build step into the database.""" |
---|
650 | if not db: |
---|
651 | db = self.env.get_db_cnx() |
---|
652 | handle_ta = True |
---|
653 | else: |
---|
654 | handle_ta = False |
---|
655 | |
---|
656 | assert self.build and self.name |
---|
657 | assert self.status in (self.SUCCESS, self.IN_PROGRESS, self.FAILURE) |
---|
658 | |
---|
659 | cursor = db.cursor() |
---|
660 | cursor.execute("INSERT INTO bitten_step (build,name,description,status," |
---|
661 | "started,stopped) VALUES (%s,%s,%s,%s,%s,%s)", |
---|
662 | (self.build, self.name, self.description or '', |
---|
663 | self.status, self.started or 0, self.stopped or 0)) |
---|
664 | if self.errors: |
---|
665 | cursor.executemany("INSERT INTO bitten_error (build,step,message," |
---|
666 | "orderno) VALUES (%s,%s,%s,%s)", |
---|
667 | [(self.build, self.name, message, idx) |
---|
668 | for idx, message in enumerate(self.errors)]) |
---|
669 | |
---|
670 | if handle_ta: |
---|
671 | db.commit() |
---|
672 | self._exists = True |
---|
673 | |
---|
674 | def fetch(cls, env, build, name, db=None): |
---|
675 | """Retrieve an existing build from the database by build ID and step |
---|
676 | name.""" |
---|
677 | if not db: |
---|
678 | db = env.get_db_cnx() |
---|
679 | |
---|
680 | cursor = db.cursor() |
---|
681 | cursor.execute("SELECT description,status,started,stopped " |
---|
682 | "FROM bitten_step WHERE build=%s AND name=%s", |
---|
683 | (build, name)) |
---|
684 | row = cursor.fetchone() |
---|
685 | if not row: |
---|
686 | return None |
---|
687 | step = BuildStep(env, build, name, row[0] or '', row[1], |
---|
688 | row[2] and int(row[2]), row[3] and int(row[3])) |
---|
689 | step._exists = True |
---|
690 | |
---|
691 | cursor.execute("SELECT message FROM bitten_error WHERE build=%s " |
---|
692 | "AND step=%s ORDER BY orderno", (build, name)) |
---|
693 | for row in cursor: |
---|
694 | step.errors.append(row[0] or '') |
---|
695 | return step |
---|
696 | |
---|
697 | fetch = classmethod(fetch) |
---|
698 | |
---|
699 | def select(cls, env, build=None, name=None, status=None, db=None): |
---|
700 | """Retrieve existing build steps from the database that match the |
---|
701 | specified criteria. |
---|
702 | """ |
---|
703 | if not db: |
---|
704 | db = env.get_db_cnx() |
---|
705 | |
---|
706 | assert status in (None, BuildStep.SUCCESS, BuildStep.IN_PROGRESS, BuildStep.FAILURE) |
---|
707 | |
---|
708 | where_clauses = [] |
---|
709 | if build is not None: |
---|
710 | where_clauses.append(("build=%s", build)) |
---|
711 | if name is not None: |
---|
712 | where_clauses.append(("name=%s", name)) |
---|
713 | if status is not None: |
---|
714 | where_clauses.append(("status=%s", status)) |
---|
715 | if where_clauses: |
---|
716 | where = "WHERE " + " AND ".join([wc[0] for wc in where_clauses]) |
---|
717 | else: |
---|
718 | where = "" |
---|
719 | |
---|
720 | cursor = db.cursor() |
---|
721 | cursor.execute("SELECT build,name FROM bitten_step %s ORDER BY started" |
---|
722 | % where, [wc[1] for wc in where_clauses]) |
---|
723 | for build, name in cursor: |
---|
724 | yield BuildStep.fetch(env, build, name, db=db) |
---|
725 | |
---|
726 | select = classmethod(select) |
---|
727 | |
---|
728 | |
---|
729 | class BuildLog(object): |
---|
730 | """Represents a build log.""" |
---|
731 | |
---|
732 | _schema = [ |
---|
733 | Table('bitten_log', key='id')[ |
---|
734 | Column('id', auto_increment=True), Column('build', type='int'), |
---|
735 | Column('step'), Column('generator'), Column('orderno', type='int'), |
---|
736 | Column('filename'), |
---|
737 | Index(['build', 'step']) |
---|
738 | ], |
---|
739 | ] |
---|
740 | |
---|
741 | # Message levels |
---|
742 | DEBUG = 'D' |
---|
743 | INFO = 'I' |
---|
744 | WARNING = 'W' |
---|
745 | ERROR = 'E' |
---|
746 | UNKNOWN = '' |
---|
747 | LEVELS_SUFFIX = '.levels' |
---|
748 | |
---|
749 | def __init__(self, env, build=None, step=None, generator=None, |
---|
750 | orderno=None, filename=None): |
---|
751 | """Initialize a new build log with the specified attributes. |
---|
752 | |
---|
753 | To actually create this build log in the database, the `insert` method |
---|
754 | needs to be called. |
---|
755 | """ |
---|
756 | self.env = env |
---|
757 | self.id = None |
---|
758 | self.build = build |
---|
759 | self.step = step |
---|
760 | self.generator = generator or '' |
---|
761 | self.orderno = orderno and int(orderno) or 0 |
---|
762 | self.filename = filename or None |
---|
763 | self.messages = [] |
---|
764 | self.logs_dir = env.config.get('bitten', 'logs_dir', 'log/bitten') |
---|
765 | if not os.path.isabs(self.logs_dir): |
---|
766 | self.logs_dir = os.path.join(env.path, self.logs_dir) |
---|
767 | if not os.path.exists(self.logs_dir): |
---|
768 | os.makedirs(self.logs_dir) |
---|
769 | |
---|
770 | exists = property(fget=lambda self: self.id is not None, |
---|
771 | doc='Whether this build log exists in the database') |
---|
772 | |
---|
773 | def get_log_file(self, filename): |
---|
774 | """Returns the full path to the log file""" |
---|
775 | if filename != os.path.basename(filename): |
---|
776 | raise ValueError("Filename may not contain path: %s" % (filename,)) |
---|
777 | return os.path.join(self.logs_dir, filename) |
---|
778 | |
---|
779 | def delete(self, db=None): |
---|
780 | """Remove the build log from the database.""" |
---|
781 | assert self.exists, 'Cannot delete a non-existing build log' |
---|
782 | if not db: |
---|
783 | db = self.env.get_db_cnx() |
---|
784 | handle_ta = True |
---|
785 | else: |
---|
786 | handle_ta = False |
---|
787 | |
---|
788 | if self.filename: |
---|
789 | log_file = self.get_log_file(self.filename) |
---|
790 | if os.path.exists(log_file): |
---|
791 | try: |
---|
792 | self.env.log.debug("Deleting log file: %s" % log_file) |
---|
793 | os.remove(log_file) |
---|
794 | except Exception, e: |
---|
795 | self.env.log.warning("Error removing log file %s: %s" % (log_file, e)) |
---|
796 | level_file = log_file + self.LEVELS_SUFFIX |
---|
797 | if os.path.exists(level_file): |
---|
798 | try: |
---|
799 | self.env.log.debug("Deleting level file: %s" % level_file) |
---|
800 | os.remove(level_file) |
---|
801 | except Exception, e: |
---|
802 | self.env.log.warning("Error removing level file %s: %s" \ |
---|
803 | % (level_file, e)) |
---|
804 | |
---|
805 | cursor = db.cursor() |
---|
806 | cursor.execute("DELETE FROM bitten_log WHERE id=%s", (self.id,)) |
---|
807 | |
---|
808 | if handle_ta: |
---|
809 | db.commit() |
---|
810 | self.id = None |
---|
811 | |
---|
812 | def insert(self, db=None): |
---|
813 | """Insert a new build log into the database.""" |
---|
814 | if not db: |
---|
815 | db = self.env.get_db_cnx() |
---|
816 | handle_ta = True |
---|
817 | else: |
---|
818 | handle_ta = False |
---|
819 | |
---|
820 | assert self.build and self.step |
---|
821 | |
---|
822 | cursor = db.cursor() |
---|
823 | cursor.execute("INSERT INTO bitten_log (build,step,generator,orderno) " |
---|
824 | "VALUES (%s,%s,%s,%s)", (self.build, self.step, |
---|
825 | self.generator, self.orderno)) |
---|
826 | id = db.get_last_id(cursor, 'bitten_log') |
---|
827 | log_file = "%s.log" % (id,) |
---|
828 | cursor.execute("UPDATE bitten_log SET filename=%s WHERE id=%s", (log_file, id)) |
---|
829 | if self.messages: |
---|
830 | log_file_name = self.get_log_file(log_file) |
---|
831 | level_file_name = log_file_name + self.LEVELS_SUFFIX |
---|
832 | codecs.open(log_file_name, "wb", "UTF-8").writelines([to_unicode(msg[1]+"\n") for msg in self.messages]) |
---|
833 | codecs.open(level_file_name, "wb", "UTF-8").writelines([to_unicode(msg[0]+"\n") for msg in self.messages]) |
---|
834 | |
---|
835 | if handle_ta: |
---|
836 | db.commit() |
---|
837 | self.id = id |
---|
838 | |
---|
839 | def fetch(cls, env, id, db=None): |
---|
840 | """Retrieve an existing build from the database by ID.""" |
---|
841 | if not db: |
---|
842 | db = env.get_db_cnx() |
---|
843 | |
---|
844 | cursor = db.cursor() |
---|
845 | cursor.execute("SELECT build,step,generator,orderno,filename FROM bitten_log " |
---|
846 | "WHERE id=%s", (id,)) |
---|
847 | row = cursor.fetchone() |
---|
848 | if not row: |
---|
849 | return None |
---|
850 | log = BuildLog(env, int(row[0]), row[1], row[2], row[3], row[4]) |
---|
851 | log.id = id |
---|
852 | if log.filename: |
---|
853 | log_filename = log.get_log_file(log.filename) |
---|
854 | if os.path.exists(log_filename): |
---|
855 | log_lines = codecs.open(log_filename, "rb", "UTF-8").readlines() |
---|
856 | else: |
---|
857 | log_lines = [] |
---|
858 | level_filename = log.get_log_file(log.filename + cls.LEVELS_SUFFIX) |
---|
859 | if os.path.exists(level_filename): |
---|
860 | log_levels = dict(enumerate(codecs.open(level_filename, "rb", "UTF-8").readlines())) |
---|
861 | else: |
---|
862 | log_levels = {} |
---|
863 | log.messages = [(log_levels.get(line_num, BuildLog.UNKNOWN).rstrip("\n"), line.rstrip("\n")) for line_num, line in enumerate(log_lines)] |
---|
864 | else: |
---|
865 | log.messages = [] |
---|
866 | |
---|
867 | return log |
---|
868 | |
---|
869 | fetch = classmethod(fetch) |
---|
870 | |
---|
871 | def select(cls, env, build=None, step=None, generator=None, db=None): |
---|
872 | """Retrieve existing build logs from the database that match the |
---|
873 | specified criteria. |
---|
874 | """ |
---|
875 | if not db: |
---|
876 | db = env.get_db_cnx() |
---|
877 | |
---|
878 | where_clauses = [] |
---|
879 | if build is not None: |
---|
880 | where_clauses.append(("build=%s", build)) |
---|
881 | if step is not None: |
---|
882 | where_clauses.append(("step=%s", step)) |
---|
883 | if generator is not None: |
---|
884 | where_clauses.append(("generator=%s", generator)) |
---|
885 | if where_clauses: |
---|
886 | where = "WHERE " + " AND ".join([wc[0] for wc in where_clauses]) |
---|
887 | else: |
---|
888 | where = "" |
---|
889 | |
---|
890 | cursor = db.cursor() |
---|
891 | cursor.execute("SELECT id FROM bitten_log %s ORDER BY orderno" |
---|
892 | % where, [wc[1] for wc in where_clauses]) |
---|
893 | for (id, ) in cursor: |
---|
894 | yield BuildLog.fetch(env, id, db=db) |
---|
895 | |
---|
896 | select = classmethod(select) |
---|
897 | |
---|
898 | |
---|
899 | class Report(object): |
---|
900 | """Represents a generated report.""" |
---|
901 | |
---|
902 | _schema = [ |
---|
903 | Table('bitten_report', key='id')[ |
---|
904 | Column('id', auto_increment=True), Column('build', type='int'), |
---|
905 | Column('step'), Column('category'), Column('generator'), |
---|
906 | Index(['build', 'step', 'category']) |
---|
907 | ], |
---|
908 | Table('bitten_report_item', key=('report', 'item', 'name'))[ |
---|
909 | Column('report', type='int'), Column('item', type='int'), |
---|
910 | Column('name'), Column('value') |
---|
911 | ] |
---|
912 | ] |
---|
913 | |
---|
914 | def __init__(self, env, build=None, step=None, category=None, |
---|
915 | generator=None): |
---|
916 | """Initialize a new report with the specified attributes. |
---|
917 | |
---|
918 | To actually create this build log in the database, the `insert` method |
---|
919 | needs to be called. |
---|
920 | """ |
---|
921 | self.env = env |
---|
922 | self.id = None |
---|
923 | self.build = build |
---|
924 | self.step = step |
---|
925 | self.category = category |
---|
926 | self.generator = generator or '' |
---|
927 | self.items = [] |
---|
928 | |
---|
929 | exists = property(fget=lambda self: self.id is not None, |
---|
930 | doc='Whether this report exists in the database') |
---|
931 | |
---|
932 | def delete(self, db=None): |
---|
933 | """Remove the report from the database.""" |
---|
934 | assert self.exists, 'Cannot delete a non-existing report' |
---|
935 | if not db: |
---|
936 | db = self.env.get_db_cnx() |
---|
937 | handle_ta = True |
---|
938 | else: |
---|
939 | handle_ta = False |
---|
940 | |
---|
941 | cursor = db.cursor() |
---|
942 | cursor.execute("DELETE FROM bitten_report_item WHERE report=%s", |
---|
943 | (self.id,)) |
---|
944 | cursor.execute("DELETE FROM bitten_report WHERE id=%s", (self.id,)) |
---|
945 | |
---|
946 | if handle_ta: |
---|
947 | db.commit() |
---|
948 | self.id = None |
---|
949 | |
---|
950 | def insert(self, db=None): |
---|
951 | """Insert a new build log into the database.""" |
---|
952 | if not db: |
---|
953 | db = self.env.get_db_cnx() |
---|
954 | handle_ta = True |
---|
955 | else: |
---|
956 | handle_ta = False |
---|
957 | |
---|
958 | assert self.build and self.step and self.category |
---|
959 | |
---|
960 | # Enforce uniqueness of build-step-category. |
---|
961 | # This should be done by the database, but the DB schema helpers in Trac |
---|
962 | # currently don't support UNIQUE() constraints |
---|
963 | assert not list(Report.select(self.env, build=self.build, |
---|
964 | step=self.step, category=self.category, |
---|
965 | db=db)), 'Report already exists' |
---|
966 | |
---|
967 | cursor = db.cursor() |
---|
968 | cursor.execute("INSERT INTO bitten_report " |
---|
969 | "(build,step,category,generator) VALUES (%s,%s,%s,%s)", |
---|
970 | (self.build, self.step, self.category, self.generator)) |
---|
971 | id = db.get_last_id(cursor, 'bitten_report') |
---|
972 | for idx, item in enumerate([item for item in self.items if item]): |
---|
973 | cursor.executemany("INSERT INTO bitten_report_item " |
---|
974 | "(report,item,name,value) VALUES (%s,%s,%s,%s)", |
---|
975 | [(id, idx, key, value) for key, value |
---|
976 | in item.items()]) |
---|
977 | |
---|
978 | if handle_ta: |
---|
979 | db.commit() |
---|
980 | self.id = id |
---|
981 | |
---|
982 | def fetch(cls, env, id, db=None): |
---|
983 | """Retrieve an existing build from the database by ID.""" |
---|
984 | if not db: |
---|
985 | db = env.get_db_cnx() |
---|
986 | |
---|
987 | cursor = db.cursor() |
---|
988 | cursor.execute("SELECT build,step,category,generator " |
---|
989 | "FROM bitten_report WHERE id=%s", (id,)) |
---|
990 | row = cursor.fetchone() |
---|
991 | if not row: |
---|
992 | return None |
---|
993 | report = Report(env, int(row[0]), row[1], row[2] or '', row[3] or '') |
---|
994 | report.id = id |
---|
995 | |
---|
996 | cursor.execute("SELECT item,name,value FROM bitten_report_item " |
---|
997 | "WHERE report=%s ORDER BY item", (id,)) |
---|
998 | items = {} |
---|
999 | for item, name, value in cursor: |
---|
1000 | items.setdefault(item, {})[name] = value |
---|
1001 | report.items = items.values() |
---|
1002 | |
---|
1003 | return report |
---|
1004 | |
---|
1005 | fetch = classmethod(fetch) |
---|
1006 | |
---|
1007 | def select(cls, env, config=None, build=None, step=None, category=None, |
---|
1008 | db=None): |
---|
1009 | """Retrieve existing reports from the database that match the specified |
---|
1010 | criteria. |
---|
1011 | """ |
---|
1012 | where_clauses = [] |
---|
1013 | joins = [] |
---|
1014 | if config is not None: |
---|
1015 | where_clauses.append(("config=%s", config)) |
---|
1016 | joins.append("INNER JOIN bitten_build ON (bitten_build.id=build)") |
---|
1017 | if build is not None: |
---|
1018 | where_clauses.append(("build=%s", build)) |
---|
1019 | if step is not None: |
---|
1020 | where_clauses.append(("step=%s", step)) |
---|
1021 | if category is not None: |
---|
1022 | where_clauses.append(("category=%s", category)) |
---|
1023 | |
---|
1024 | if where_clauses: |
---|
1025 | where = "WHERE " + " AND ".join([wc[0] for wc in where_clauses]) |
---|
1026 | else: |
---|
1027 | where = "" |
---|
1028 | |
---|
1029 | if not db: |
---|
1030 | db = env.get_db_cnx() |
---|
1031 | cursor = db.cursor() |
---|
1032 | cursor.execute("SELECT bitten_report.id FROM bitten_report %s %s " |
---|
1033 | "ORDER BY category" % (' '.join(joins), where), |
---|
1034 | [wc[1] for wc in where_clauses]) |
---|
1035 | for (id, ) in cursor: |
---|
1036 | yield Report.fetch(env, id, db=db) |
---|
1037 | |
---|
1038 | select = classmethod(select) |
---|
1039 | |
---|
1040 | |
---|
1041 | schema = BuildConfig._schema + TargetPlatform._schema + Build._schema + \ |
---|
1042 | BuildStep._schema + BuildLog._schema + Report._schema |
---|
1043 | schema_version = 12 |
---|