/
fakedb.py
2408 lines (1963 loc) · 76.7 KB
/
fakedb.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Buildbot. Buildbot 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, version 2.
#
# 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.
#
# Copyright Buildbot Team Members
"""
A complete re-implementation of the database connector components, but without
using a database. These classes should pass the same tests as are applied to
the real connector components.
"""
import base64
import copy
import hashlib
from buildbot.db import buildrequests
from buildbot.db import changesources
from buildbot.db import schedulers
from buildbot.test.util import validation
from buildbot.util import datetime2epoch
from buildbot.util import json
from twisted.internet import defer
from twisted.internet import reactor
# Fake DB Rows
class Row(object):
"""
Parent class for row classes, which are used to specify test data for
database-related tests.
@cvar defaults: default values for columns
@type defaults: dictionary
@cvar table: the table name
@cvar id_column: specify a column that should be assigned an
auto-incremented id. Auto-assigned id's begin at 1000, so any explicitly
specified ID's should be less than 1000.
@cvar required_columns: a tuple of columns that must be given in the
constructor
@cvar hashedColumns: a tuple of hash column and source columns designating
a hash to work around MySQL's inability to do indexing.
@ivar values: the values to be inserted into this row
"""
id_column = ()
required_columns = ()
lists = ()
dicts = ()
hashedColumns = []
foreignKeys = []
_next_id = None
def __init__(self, **kwargs):
self.values = self.defaults.copy()
self.values.update(kwargs)
if self.id_column:
if self.values[self.id_column] is None:
self.values[self.id_column] = self.nextId()
for col in self.required_columns:
assert col in kwargs, "%s not specified: %s" % (col, kwargs)
for col in self.lists:
setattr(self, col, [])
for col in self.dicts:
setattr(self, col, {})
for col in kwargs.keys():
assert col in self.defaults, "%s is not a valid column" % col
# cast to unicode
for k, v in self.values.iteritems():
if isinstance(v, str):
self.values[k] = unicode(v)
# calculate any necessary hashes
for hash_col, src_cols in self.hashedColumns:
self.values[hash_col] = self.hashColumns(
*(self.values[c] for c in src_cols))
# make the values appear as attributes
self.__dict__.update(self.values)
def __cmp__(self, other):
return cmp(self.__class__, other.__class__) \
or cmp(self.values, other.values)
def __repr__(self):
return '%s(**%r)' % (self.__class__.__name__, self.values)
def nextId(self):
id, Row._next_id = Row._next_id, (Row._next_id or 1) + 1
return id
def hashColumns(self, *args):
# copied from master/buildbot/db/base.py
def encode(x):
try:
return x.encode('utf8')
except AttributeError:
if x is None:
return '\xf5'
return str(x)
return hashlib.sha1('\0'.join(map(encode, args))).hexdigest()
@defer.inlineCallbacks
def checkForeignKeys(self, db, t):
accessors = dict(
buildsetid=db.buildsets.getBuildset,
buildslaveid=db.buildslaves.getBuildslave,
builderid=db.builders.getBuilder,
buildid=db.builds.getBuild,
changesourceid=db.changesources.getChangeSource,
changeid=db.changes.getChange,
buildrequestid=db.buildrequests.getBuildRequest,
sourcestampid=db.sourcestamps.getSourceStamp,
schedulerid=db.schedulers.getScheduler,
brid=db.buildrequests.getBuildRequest,
masterid=db.masters.getMaster)
for foreign_key in self.foreignKeys:
if foreign_key in accessors:
key = getattr(self, foreign_key)
if key is not None:
val = yield accessors[foreign_key](key)
t.assertTrue(val is not None,
"foreign key %s:%r does not exit" % (foreign_key, key))
else:
raise ValueError("warning, unsupported foreign key", foreign_key, self.table)
class BuildRequest(Row):
table = "buildrequests"
defaults = dict(
id=None,
buildsetid=None,
builderid=None,
buildername=None,
priority=0,
complete=0,
results=-1,
submitted_at=12345678,
complete_at=None,
waited_for=0,
)
foreignKeys = ('buildsetid',)
id_column = 'id'
required_columns = ('buildsetid',)
class BuildRequestClaim(Row):
table = "buildrequest_claims"
defaults = dict(
brid=None,
masterid=None,
claimed_at=None
)
foreignKeys = ('brid', 'masterid')
required_columns = ('brid', 'masterid', 'claimed_at')
class ChangeSource(Row):
table = "changesources"
defaults = dict(
id=None,
name='csname',
name_hash=None,
)
id_column = 'id'
hashedColumns = [('name_hash', ('name',))]
class ChangeSourceMaster(Row):
table = "changesource_masters"
defaults = dict(
changesourceid=None,
masterid=None,
)
foreignKeys = ('changesourceid', 'masterid')
required_columns = ('changesourceid', 'masterid')
class Change(Row):
table = "changes"
defaults = dict(
changeid=None,
author=u'frank',
comments=u'test change',
branch=u'master',
revision=u'abcd',
revlink=u'http://vc/abcd',
when_timestamp=1200000,
category=u'cat',
repository=u'repo',
codebase=u'',
project=u'proj',
sourcestampid=92,
parent_changeids=None,
)
lists = ('files', 'uids')
dicts = ('properties',)
id_column = 'changeid'
class ChangeFile(Row):
table = "change_files"
defaults = dict(
changeid=None,
filename=None,
)
foreignKeys = ('changeid',)
required_columns = ('changeid',)
class ChangeProperty(Row):
table = "change_properties"
defaults = dict(
changeid=None,
property_name=None,
property_value=None,
)
foreignKeys = ('changeid',)
required_columns = ('changeid',)
class ChangeUser(Row):
table = "change_users"
defaults = dict(
changeid=None,
uid=None,
)
foreignKeys = ('changeid',)
required_columns = ('changeid',)
class Patch(Row):
table = "patches"
defaults = dict(
id=None,
patchlevel=0,
patch_base64='aGVsbG8sIHdvcmxk', # 'hello, world',
patch_author=None,
patch_comment=None,
subdir=None,
)
id_column = 'id'
class SourceStamp(Row):
table = "sourcestamps"
defaults = dict(
id=None,
branch='master',
revision='abcd',
patchid=None,
repository='repo',
codebase='',
project='proj',
created_at=89834834,
ss_hash=None,
)
id_column = 'id'
hashedColumns = [('ss_hash', ('branch', 'revision', 'repository',
'project', 'codebase', 'patchid',))]
class Scheduler(Row):
table = "schedulers"
defaults = dict(
id=None,
name='schname',
name_hash=None,
)
id_column = 'id'
hashedColumns = [('name_hash', ('name',))]
class SchedulerMaster(Row):
table = "scheduler_masters"
defaults = dict(
schedulerid=None,
masterid=None,
)
foreignKeys = ('schedulerid', 'masterid')
required_columns = ('schedulerid', 'masterid')
class SchedulerChange(Row):
table = "scheduler_changes"
defaults = dict(
schedulerid=None,
changeid=None,
important=1,
)
foreignKeys = ('schedulerid', 'changeid')
required_columns = ('schedulerid', 'changeid')
class Buildset(Row):
table = "buildsets"
defaults = dict(
id=None,
external_idstring='extid',
reason='because',
submitted_at=12345678,
complete=0,
complete_at=None,
results=-1,
parent_buildid=None,
parent_relationship=None,
)
id_column = 'id'
class BuildsetProperty(Row):
table = "buildset_properties"
defaults = dict(
buildsetid=None,
property_name='prop',
property_value='[22, "fakedb"]',
)
foreignKeys = ('buildsetid',)
required_columns = ('buildsetid', )
class Buildslave(Row):
table = "buildslaves"
defaults = dict(
id=None,
name='some:slave',
info={"a": "b"},
)
id_column = 'id'
required_columns = ('name', )
class BuildsetSourceStamp(Row):
table = "buildset_sourcestamps"
defaults = dict(
id=None,
buildsetid=None,
sourcestampid=None,
)
foreignKeys = ('buildsetid', 'sourcestampid')
required_columns = ('buildsetid', 'sourcestampid', )
id_column = 'id'
class Object(Row):
table = "objects"
defaults = dict(
id=None,
name='nam',
class_name='cls',
)
id_column = 'id'
class ObjectState(Row):
table = "object_state"
defaults = dict(
objectid=None,
name='nam',
value_json='{}',
)
required_columns = ('objectid', )
class User(Row):
table = "users"
defaults = dict(
uid=None,
identifier='soap',
bb_username=None,
bb_password=None,
)
id_column = 'uid'
class UserInfo(Row):
table = "users_info"
defaults = dict(
uid=None,
attr_type='git',
attr_data='Tyler Durden <tyler@mayhem.net>',
)
foreignKeys = ('uid',)
required_columns = ('uid', )
class Build(Row):
table = "builds"
defaults = dict(
id=None,
number=29,
buildrequestid=None,
builderid=None,
buildslaveid=-1,
masterid=None,
started_at=1304262222,
complete_at=None,
state_string=u"test",
results=None)
id_column = 'id'
foreignKeys = ('buildrequestid', 'masterid', 'buildslaveid', 'builderid')
required_columns = ('buildrequestid', 'masterid', 'buildslaveid')
class BuildProperty(Row):
table = "build_properties"
defaults = dict(
buildid=None,
name='prop',
value=42,
source='fakedb'
)
foreignKeys = ('buildid',)
required_columns = ('buildid',)
class Step(Row):
table = "steps"
defaults = dict(
id=None,
number=29,
name='step29',
buildid=None,
started_at=1304262222,
complete_at=None,
state_string='',
results=None,
urls_json='[]')
id_column = 'id'
foreignKeys = ('buildid',)
required_columns = ('buildid', )
class Log(Row):
table = "logs"
defaults = dict(
id=None,
name='log29',
slug='log29',
stepid=None,
complete=0,
num_lines=0,
type='s')
id_column = 'id'
required_columns = ('stepid', )
class LogChunk(Row):
table = "logchunks"
defaults = dict(
logid=None,
first_line=0,
last_line=0,
content=u'',
compressed=0)
required_columns = ('logid', )
class Master(Row):
table = "masters"
defaults = dict(
id=None,
name='some:master',
name_hash=None,
active=1,
last_active=9998999,
)
id_column = 'id'
hashedColumns = [('name_hash', ('name',))]
class Builder(Row):
table = "builders"
defaults = dict(
id=None,
name='some:builder',
name_hash=None,
description=None,
)
id_column = 'id'
hashedColumns = [('name_hash', ('name',))]
class BuilderMaster(Row):
table = "builder_masters"
defaults = dict(
id=None,
builderid=None,
masterid=None
)
id_column = 'id'
required_columns = ('builderid', 'masterid')
class Tag(Row):
table = "tags"
defaults = dict(
id=None,
name='some:tag',
name_hash=None,
)
id_column = 'id'
hashedColumns = [('name_hash', ('name',))]
class BuildersTags(Row):
table = "builders_tags"
defaults = dict(
id=None,
builderid=None,
tagid=None,
)
foreignKeys = ('builderid', 'tagid')
required_columns = ('builderid', 'tagid', )
id_column = 'id'
class ConnectedBuildslave(Row):
table = "connected_buildslaves"
defaults = dict(
id=None,
masterid=None,
buildslaveid=None,
)
id_column = 'id'
required_columns = ('masterid', 'buildslaveid')
class ConfiguredBuildslave(Row):
table = "configured_buildslaves"
defaults = dict(
id=None,
buildermasterid=None,
buildslaveid=None,
)
id_column = 'id'
required_columns = ('buildermasterid', 'buildslaveid')
# Fake DB Components
class FakeDBComponent(object):
data2db = {}
def __init__(self, db, testcase):
self.db = db
self.t = testcase
self.setUp()
class FakeChangeSourcesComponent(FakeDBComponent):
def setUp(self):
self.changesources = {}
self.changesource_masters = {}
self.states = {}
def insertTestData(self, rows):
pass
for row in rows:
if isinstance(row, ChangeSource):
self.changesources[row.id] = row.name
if isinstance(row, ChangeSourceMaster):
self.changesource_masters[row.changesourceid] = row.masterid
# component methods
def findChangeSourceId(self, name):
for cs_id, cs_name in self.changesources.iteritems():
if cs_name == name:
return defer.succeed(cs_id)
new_id = (max(self.changesources) + 1) if self.changesources else 1
self.changesources[new_id] = name
return defer.succeed(new_id)
def getChangeSource(self, changesourceid):
if changesourceid in self.changesources:
rv = dict(
id=changesourceid,
name=self.changesources[changesourceid],
masterid=None)
# only set masterid if the relevant changesource master exists and
# is active
rv['masterid'] = self.changesource_masters.get(changesourceid)
return defer.succeed(rv)
else:
return None
def getChangeSources(self, active=None, masterid=None):
d = defer.DeferredList([
self.getChangeSource(id) for id in self.changesources
])
@d.addCallback
def filter(results):
# filter off the DeferredList results (we know it's good)
results = [r[1] for r in results]
# filter for masterid
if masterid is not None:
results = [r for r in results
if r['masterid'] == masterid]
# filter for active or inactive if necessary
if active:
results = [r for r in results
if r['masterid'] is not None]
elif active is not None:
results = [r for r in results
if r['masterid'] is None]
return results
return d
def setChangeSourceMaster(self, changesourceid, masterid):
current_masterid = self.changesource_masters.get(changesourceid)
if current_masterid and masterid is not None:
return defer.fail(changesources.ChangeSourceAlreadyClaimedError())
self.changesource_masters[changesourceid] = masterid
return defer.succeed(None)
# fake methods
def fakeChangeSource(self, name, changesourceid):
self.changesources[changesourceid] = name
def fakeChangeSourceMaster(self, changesourceid, masterid):
if masterid is not None:
self.changesource_masters[changesourceid] = masterid
else:
del self.changesource_masters[changesourceid]
# assertions
def assertChangeSourceMaster(self, changesourceid, masterid):
self.t.assertEqual(self.changesource_masters.get(changesourceid),
masterid)
class FakeChangesComponent(FakeDBComponent):
def setUp(self):
self.changes = {}
def insertTestData(self, rows):
for row in rows:
if isinstance(row, Change):
# copy this since we'll be modifying it (e.g., adding files)
ch = self.changes[row.changeid] = copy.deepcopy(row.values)
ch['files'] = []
ch['properties'] = {}
ch['uids'] = []
elif isinstance(row, ChangeFile):
ch = self.changes[row.changeid]
ch['files'].append(row.filename)
elif isinstance(row, ChangeProperty):
ch = self.changes[row.changeid]
n, vs = row.property_name, row.property_value
v, s = json.loads(vs)
ch['properties'][n] = (v, s)
elif isinstance(row, ChangeUser):
ch = self.changes[row.changeid]
ch['uids'].append(row.uid)
# component methods
@defer.inlineCallbacks
def addChange(self, author=None, files=None, comments=None, is_dir=None,
revision=None, when_timestamp=None, branch=None,
category=None, revlink='', properties={}, repository='',
codebase='', project='', uid=None, _reactor=reactor):
if self.changes:
changeid = max(self.changes.iterkeys()) + 1
else:
changeid = 500
ssid = yield self.db.sourcestamps.findSourceStampId(
revision=revision, branch=branch, repository=repository,
codebase=codebase, project=project, _reactor=_reactor)
parent_changeids = yield self.getParentChangeIds(branch, repository, project, codebase)
self.changes[changeid] = ch = dict(
changeid=changeid,
parent_changeids=parent_changeids,
author=author,
comments=comments,
revision=revision,
when_timestamp=datetime2epoch(when_timestamp),
branch=branch,
category=category,
revlink=revlink,
repository=repository,
project=project,
codebase=codebase,
uids=[],
files=files,
properties=properties,
sourcestampid=ssid)
if uid:
ch['uids'].append(uid)
defer.returnValue(changeid)
def getLatestChangeid(self):
if self.changes:
return defer.succeed(max(self.changes.iterkeys()))
return defer.succeed(None)
def getParentChangeIds(self, branch, repository, project, codebase):
if self.changes:
for changeid, change in self.changes.iteritems():
if (change['branch'] == branch and
change['repository'] == repository and
change['project'] == project and
change['codebase'] == codebase):
return defer.succeed([change['changeid']])
return defer.succeed([])
def getChange(self, key, no_cache=False):
try:
row = self.changes[key]
except KeyError:
return defer.succeed(None)
return defer.succeed(self._chdict(row))
def getChangeUids(self, changeid):
try:
ch_uids = self.changes[changeid]['uids']
except KeyError:
ch_uids = []
return defer.succeed(ch_uids)
def getRecentChanges(self, count):
ids = sorted(self.changes.keys())
chdicts = [self._chdict(self.changes[id]) for id in ids[-count:]]
return defer.succeed(chdicts)
def getChanges(self):
chdicts = [self._chdict(v) for v in self.changes.values()]
return defer.succeed(chdicts)
def getChangesCount(self):
return len(self.changes)
def getChangesForBuild(self, buildid):
pass
def _chdict(self, row):
chdict = row.copy()
del chdict['uids']
if chdict['parent_changeids'] is None:
chdict['parent_changeids'] = []
chdict['when_timestamp'] = _mkdt(chdict['when_timestamp'])
return chdict
# assertions
def assertChange(self, changeid, row):
row_only = self.changes[changeid].copy()
del row_only['files']
del row_only['properties']
del row_only['uids']
if not row_only['parent_changeids']:
# Convert [] to None
# None is the value stored in the DB.
# We need this kind of conversion, because for the moment we only support
# 1 parent for a change.
# When we will support multiple parent for change, then we will have a
# table parent_changes with at least 2 col: "changeid", "parent_changeid"
# And the col 'parent_changeids' of the table changes will be dropped
row_only['parent_changeids'] = None
self.t.assertEqual(row_only, row.values)
def assertChangeUsers(self, changeid, expectedUids):
self.t.assertEqual(self.changes[changeid]['uids'], expectedUids)
# fake methods
def fakeAddChangeInstance(self, change):
if not hasattr(change, 'number') or not change.number:
if self.changes:
changeid = max(self.changes.iterkeys()) + 1
else:
changeid = 500
else:
changeid = change.number
# make a row from the change
row = dict(
changeid=changeid,
author=change.who,
files=change.files,
comments=change.comments,
revision=change.revision,
when_timestamp=change.when,
branch=change.branch,
category=change.category,
revlink=change.revlink,
properties=change.properties,
repository=change.repository,
codebase=change.codebase,
project=change.project,
uids=[])
self.changes[changeid] = row
class FakeSchedulersComponent(FakeDBComponent):
def setUp(self):
self.schedulers = {}
self.scheduler_masters = {}
self.states = {}
self.classifications = {}
def insertTestData(self, rows):
for row in rows:
if isinstance(row, SchedulerChange):
cls = self.classifications.setdefault(row.schedulerid, {})
cls[row.changeid] = row.important
if isinstance(row, Scheduler):
self.schedulers[row.id] = row.name
if isinstance(row, SchedulerMaster):
self.scheduler_masters[row.schedulerid] = row.masterid
# component methods
def classifyChanges(self, schedulerid, classifications):
self.classifications.setdefault(schedulerid, {}).update(classifications)
return defer.succeed(None)
def flushChangeClassifications(self, schedulerid, less_than=None):
if less_than is not None:
classifications = self.classifications.setdefault(schedulerid, {})
for changeid in classifications.keys():
if changeid < less_than:
del classifications[changeid]
else:
self.classifications[schedulerid] = {}
return defer.succeed(None)
def getChangeClassifications(self, schedulerid, branch=-1, repository=-1,
project=-1, codebase=-1):
classifications = self.classifications.setdefault(schedulerid, {})
sentinel = dict(branch=object(), repository=object(),
project=object(), codebase=object())
if branch != -1:
# filter out the classifications for the requested branch
classifications = dict(
(k, v) for (k, v) in classifications.iteritems()
if self.db.changes.changes.get(k, sentinel)['branch'] == branch)
if repository != -1:
# filter out the classifications for the requested branch
classifications = dict(
(k, v) for (k, v) in classifications.iteritems()
if self.db.changes.changes.get(k, sentinel)['repository'] == repository)
if project != -1:
# filter out the classifications for the requested branch
classifications = dict(
(k, v) for (k, v) in classifications.iteritems()
if self.db.changes.changes.get(k, sentinel)['project'] == project)
if codebase != -1:
# filter out the classifications for the requested branch
classifications = dict(
(k, v) for (k, v) in classifications.iteritems()
if self.db.changes.changes.get(k, sentinel)['codebase'] == codebase)
return defer.succeed(classifications)
def findSchedulerId(self, name):
for sch_id, sch_name in self.schedulers.iteritems():
if sch_name == name:
return defer.succeed(sch_id)
new_id = (max(self.schedulers) + 1) if self.schedulers else 1
self.schedulers[new_id] = name
return defer.succeed(new_id)
def getScheduler(self, schedulerid):
if schedulerid in self.schedulers:
rv = dict(
id=schedulerid,
name=self.schedulers[schedulerid],
masterid=None)
# only set masterid if the relevant scheduler master exists and
# is active
rv['masterid'] = self.scheduler_masters.get(schedulerid)
return defer.succeed(rv)
else:
return None
def getSchedulers(self, active=None, masterid=None):
d = defer.DeferredList([
self.getScheduler(id) for id in self.schedulers
])
@d.addCallback
def filter(results):
# filter off the DeferredList results (we know it's good)
results = [r[1] for r in results]
# filter for masterid
if masterid is not None:
results = [r for r in results
if r['masterid'] == masterid]
# filter for active or inactive if necessary
if active:
results = [r for r in results
if r['masterid'] is not None]
elif active is not None:
results = [r for r in results
if r['masterid'] is None]