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

# 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 

 

from __future__ import with_statement 

 

import os 

import re 

import sys 

import warnings 

from buildbot.util import safeTranslate 

from buildbot import interfaces 

from buildbot import locks 

from buildbot.revlinks import default_revlink_matcher 

from twisted.python import log, failure 

from twisted.internet import defer 

from twisted.application import service 

 

class ConfigErrors(Exception): 

 

    def __init__(self, errors=[]): 

        self.errors = errors[:] 

 

    def __str__(self): 

        return "\n".join(self.errors) 

 

    def addError(self, msg): 

        self.errors.append(msg) 

 

    def __nonzero__(self): 

        return len(self.errors) 

 

_errors = None 

def error(error): 

    if _errors is not None: 

        _errors.addError(error) 

    else: 

        raise ConfigErrors([error]) 

 

class MasterConfig(object): 

 

    def __init__(self): 

        # local import to avoid circular imports 

        from buildbot.process import properties 

        # default values for all attributes 

 

        # global 

        self.title = 'Buildbot' 

        self.titleURL = 'http://buildbot.net' 

        self.buildbotURL = 'http://localhost:8080/' 

        self.changeHorizon = None 

        self.eventHorizon = 50 

        self.logHorizon = None 

        self.buildHorizon = None 

        self.logCompressionLimit = 4*1024 

        self.logCompressionMethod = 'bz2' 

        self.logMaxTailSize = None 

        self.logMaxSize = None 

        self.properties = properties.Properties() 

        self.mergeRequests = None 

        self.codebaseGenerator = None 

        self.prioritizeBuilders = None 

        self.slavePortnum = None 

        self.multiMaster = False 

        self.debugPassword = None 

        self.manhole = None 

 

        self.validation = dict( 

            branch=re.compile(r'^[\w.+/~-]*$'), 

            revision=re.compile(r'^[ \w\.\-\/]*$'), 

            property_name=re.compile(r'^[\w\.\-\/\~:]*$'), 

            property_value=re.compile(r'^[\w\.\-\/\~:]*$'), 

        ) 

        self.db = dict( 

            db_url='sqlite:///state.sqlite', 

            db_poll_interval=None, 

        ) 

        self.metrics = None 

        self.caches = dict( 

            Builds=15, 

            Changes=10, 

        ) 

        self.schedulers = {} 

        self.builders = [] 

        self.slaves = [] 

        self.change_sources = [] 

        self.status = [] 

        self.user_managers = [] 

        self.revlink = default_revlink_matcher 

 

    _known_config_keys = set([ 

        "buildbotURL", "buildCacheSize", "builders", "buildHorizon", "caches", 

        "change_source", "codebaseGenerator", "changeCacheSize", "changeHorizon", 

        'db', "db_poll_interval", "db_url", "debugPassword", "eventHorizon", 

        "logCompressionLimit", "logCompressionMethod", "logHorizon", 

        "logMaxSize", "logMaxTailSize", "manhole", "mergeRequests", "metrics", 

        "multiMaster", "prioritizeBuilders", "projectName", "projectURL", 

        "properties", "revlink", "schedulers", "slavePortnum", "slaves", 

        "status", "title", "titleURL", "user_managers", "validation" 

    ]) 

 

    @classmethod 

    def loadConfig(cls, basedir, filename): 

        if not os.path.isdir(basedir): 

            raise ConfigErrors([ 

                "basedir '%s' does not exist" % (basedir,), 

            ]) 

        filename = os.path.join(basedir, filename) 

        if not os.path.exists(filename): 

            raise ConfigErrors([ 

                "configuration file '%s' does not exist" % (filename,), 

            ]) 

 

        try: 

            f = open(filename, "r") 

        except IOError, e: 

            raise ConfigErrors([ 

                "unable to open configuration file %r: %s" % (filename, e), 

            ]) 

 

        log.msg("Loading configuration from %r" % (filename,)) 

 

        # execute the config file 

        localDict = { 

            'basedir': os.path.expanduser(basedir), 

            '__file__': os.path.abspath(filename), 

        } 

 

        # from here on out we can batch errors together for the user's 

        # convenience 

        global _errors 

        _errors = errors = ConfigErrors() 

 

        old_sys_path = sys.path[:] 

        sys.path.append(basedir) 

        try: 

            try: 

                exec f in localDict 

            except ConfigErrors, e: 

                for error in e.errors: 

                    errors.addError(error) 

                raise errors 

            except: 

                log.err(failure.Failure(), 'error while parsing config file:') 

                errors.addError( 

                    "error while parsing config file: %s (traceback in logfile)" % 

                        (sys.exc_info()[1],), 

                ) 

                raise errors 

        finally: 

            f.close() 

            sys.path[:] = old_sys_path 

            _errors = None 

 

        if 'BuildmasterConfig' not in localDict: 

            errors.addError( 

                "Configuration file %r does not define 'BuildmasterConfig'" 

                    % (filename,), 

            ) 

            raise errors 

 

        config_dict = localDict['BuildmasterConfig'] 

 

        # check for unknown keys 

        unknown_keys = set(config_dict.keys()) - cls._known_config_keys 

        if unknown_keys: 

            if len(unknown_keys) == 1: 

                errors.addError('Unknown BuildmasterConfig key %s' % 

                        (unknown_keys.pop())) 

            else: 

                errors.addError('Unknown BuildmasterConfig keys %s' % 

                        (', '.join(sorted(unknown_keys)))) 

 

        # instantiate a new config object, which will apply defaults 

        # automatically 

        config = cls() 

 

        # and defer the rest to sub-functions, for code clarity 

        config.load_global(filename, config_dict, errors) 

        config.load_validation(filename, config_dict, errors) 

        config.load_db(filename, config_dict, errors) 

        config.load_metrics(filename, config_dict, errors) 

        config.load_caches(filename, config_dict, errors) 

        config.load_schedulers(filename, config_dict, errors) 

        config.load_builders(filename, config_dict, errors) 

        config.load_slaves(filename, config_dict, errors) 

        config.load_change_sources(filename, config_dict, errors) 

        config.load_status(filename, config_dict, errors) 

        config.load_user_managers(filename, config_dict, errors) 

 

        # run some sanity checks 

        config.check_single_master(errors) 

        config.check_schedulers(errors) 

        config.check_locks(errors) 

        config.check_builders(errors) 

        config.check_status(errors) 

        config.check_horizons(errors) 

        config.check_slavePortnum(errors) 

 

        if errors: 

            raise errors 

 

        return config 

 

    def load_global(self, filename, config_dict, errors): 

        def copy_param(name, alt_key=None, 

                       check_type=None, check_type_name=None): 

            if name in config_dict: 

                v = config_dict[name] 

            elif alt_key and alt_key in config_dict: 

                v = config_dict[alt_key] 

            else: 

                return 

            if v is not None and check_type and not isinstance(v, check_type): 

                errors.addError("c['%s'] must be %s" % 

                                (name, check_type_name)) 

            else: 

                setattr(self, name, v) 

 

        def copy_int_param(name, alt_key=None): 

            copy_param(name, alt_key=alt_key, 

                    check_type=int, check_type_name='an int') 

 

        def copy_str_param(name, alt_key=None): 

            copy_param(name, alt_key=alt_key, 

                    check_type=basestring, check_type_name='a string') 

 

        copy_str_param('title', alt_key='projectName') 

        copy_str_param('titleURL', alt_key='projectURL') 

        copy_str_param('buildbotURL') 

 

        copy_int_param('changeHorizon') 

        copy_int_param('eventHorizon') 

        copy_int_param('logHorizon') 

        copy_int_param('buildHorizon') 

 

        copy_int_param('logCompressionLimit') 

 

        if 'logCompressionMethod' in config_dict: 

            logCompressionMethod = config_dict.get('logCompressionMethod') 

            if logCompressionMethod not in ('bz2', 'gz'): 

                errors.addError( 

                        "c['logCompressionMethod'] must be 'bz2' or 'gz'") 

            self.logCompressionMethod = logCompressionMethod 

 

        copy_int_param('logMaxSize') 

        copy_int_param('logMaxTailSize') 

 

        properties = config_dict.get('properties', {}) 

        if not isinstance(properties, dict): 

            errors.addError("c['properties'] must be a dictionary") 

        else: 

            self.properties.update(properties, filename) 

 

        mergeRequests = config_dict.get('mergeRequests') 

        if (mergeRequests not in (None, True, False) 

            and not callable(mergeRequests)): 

            errors.addError("mergeRequests must be a callable, True, or False") 

        else: 

            self.mergeRequests = mergeRequests 

 

        codebaseGenerator = config_dict.get('codebaseGenerator') 

        if (codebaseGenerator is not None and 

            not callable(codebaseGenerator)): 

            errors.addError("codebaseGenerator must be a callable accepting a dict and returning a str") 

        else: 

            self.codebaseGenerator = codebaseGenerator 

 

        prioritizeBuilders = config_dict.get('prioritizeBuilders') 

        if prioritizeBuilders is not None and not callable(prioritizeBuilders): 

            errors.addError("prioritizeBuilders must be a callable") 

        else: 

            self.prioritizeBuilders = prioritizeBuilders 

 

        if 'slavePortnum' in config_dict: 

            slavePortnum = config_dict.get('slavePortnum') 

            if isinstance(slavePortnum, int): 

                slavePortnum = "tcp:%d" % slavePortnum 

            self.slavePortnum = slavePortnum 

 

        if 'multiMaster' in config_dict: 

            self.multiMaster = config_dict["multiMaster"] 

 

        copy_str_param('debugPassword') 

 

        if 'manhole' in config_dict: 

            # we don't check that this is a manhole instance, since that 

            # requires importing buildbot.manhole for every user, and currently 

            # that will fail if pycrypto isn't installed 

            self.manhole = config_dict['manhole'] 

 

        if 'revlink' in config_dict: 

            revlink = config_dict['revlink'] 

            if not callable(revlink): 

                errors.addError("revlink must be a callable") 

            else: 

                self.revlink = revlink 

 

    def load_validation(self, filename, config_dict, errors): 

        validation = config_dict.get("validation", {}) 

        if not isinstance(validation, dict): 

            errors.addError("c['validation'] must be a dictionary") 

        else: 

            unknown_keys = ( 

                set(validation.keys()) - set(self.validation.keys())) 

            if unknown_keys: 

                errors.addError("unrecognized validation key(s): %s" % 

                                    (", ".join(unknown_keys))) 

            else: 

                self.validation.update(validation) 

 

 

    def load_db(self, filename, config_dict, errors): 

        if 'db' in config_dict: 

            db = config_dict['db'] 

            if set(db.keys()) > set(['db_url', 'db_poll_interval']): 

                errors.addError("unrecognized keys in c['db']") 

            self.db.update(db) 

        if 'db_url' in config_dict: 

            self.db['db_url'] = config_dict['db_url'] 

        if 'db_poll_interval' in config_dict: 

            self.db['db_poll_interval'] = config_dict["db_poll_interval"] 

 

        # we don't attempt to parse db URLs here - the engine strategy will do so 

 

        # check the db_poll_interval 

        db_poll_interval = self.db['db_poll_interval'] 

        if db_poll_interval is not None and \ 

                    not isinstance(db_poll_interval, int): 

            errors.addError("c['db_poll_interval'] must be an int") 

        else: 

            self.db['db_poll_interval'] = db_poll_interval 

 

 

    def load_metrics(self, filename, config_dict, errors): 

        # we don't try to validate metrics keys 

        if 'metrics' in config_dict: 

            metrics = config_dict["metrics"] 

            if not isinstance(metrics, dict): 

                errors.addError("c['metrics'] must be a dictionary") 

            else: 

                self.metrics = metrics 

 

 

    def load_caches(self, filename, config_dict, errors): 

        explicit = False 

        if 'caches' in config_dict: 

            explicit = True 

            caches = config_dict['caches'] 

            if not isinstance(caches, dict): 

                errors.addError("c['caches'] must be a dictionary") 

            else: 

                self.caches.update(caches) 

 

        if 'buildCacheSize' in config_dict: 

            if explicit: 

                msg = "cannot specify c['caches'] and c['buildCacheSize']" 

                errors.addError(msg) 

            self.caches['Builds'] = config_dict['buildCacheSize'] 

        if 'changeCacheSize' in config_dict: 

            if explicit: 

                msg = "cannot specify c['caches'] and c['changeCacheSize']" 

                errors.addError(msg) 

            self.caches['Changes'] = config_dict['changeCacheSize'] 

 

 

    def load_schedulers(self, filename, config_dict, errors): 

        if 'schedulers' not in config_dict: 

            return 

        schedulers = config_dict['schedulers'] 

 

        ok = True 

        if not isinstance(schedulers, (list, tuple)): 

            ok = False 

        else: 

            for s in schedulers: 

                if not interfaces.IScheduler.providedBy(s): 

                    ok = False 

        if not ok: 

            msg="c['schedulers'] must be a list of Scheduler instances" 

            errors.addError(msg) 

 

        # convert from list to dict, first looking for duplicates 

        seen_names = set() 

        for s in schedulers: 

            if s.name in seen_names: 

                errors.addError("scheduler name '%s' used multiple times" % 

                                s.name) 

            seen_names.add(s.name) 

 

        self.schedulers = dict((s.name, s) for s in schedulers) 

 

 

    def load_builders(self, filename, config_dict, errors): 

        if 'builders' not in config_dict: 

            return 

        builders = config_dict['builders'] 

 

        if not isinstance(builders, (list, tuple)): 

            errors.addError("c['builders'] must be a list") 

            return 

 

        # convert all builder configs to BuilderConfig instances 

        def mapper(b): 

            if isinstance(b, BuilderConfig): 

                return b 

            elif isinstance(b, dict): 

                return BuilderConfig(**b) 

            else: 

                raise RuntimeError() # signal for the try/except below 

        try: 

            builders = [ mapper(b) for b in builders ] 

        except RuntimeError: 

            errors.addError("c['builders'] must be a list of builder configs") 

            return 

 

        for builder in builders: 

            if os.path.isabs(builder.builddir): 

                warnings.warn("Absolute path '%s' for builder may cause " 

                        "mayhem.  Perhaps you meant to specify slavebuilddir " 

                        "instead.") 

 

        self.builders = builders 

 

 

    def load_slaves(self, filename, config_dict, errors): 

        if 'slaves' not in config_dict: 

            return 

        slaves = config_dict['slaves'] 

 

        if not isinstance(slaves, (list, tuple)): 

            errors.addError("c['slaves'] must be a list") 

            return 

 

        for sl in slaves: 

            if not interfaces.IBuildSlave.providedBy(sl): 

                msg = "c['slaves'] must be a list of BuildSlave instances" 

                errors.addError(msg) 

                return 

 

            if sl.slavename in ("debug", "change", "status"): 

                msg = "slave name '%s' is reserved" % sl.slavename 

                errors.addError(msg) 

 

        self.slaves = config_dict['slaves'] 

 

 

    def load_change_sources(self, filename, config_dict, errors): 

        change_source = config_dict.get('change_source', []) 

        if isinstance(change_source, (list, tuple)): 

            change_sources = change_source 

        else: 

            change_sources = [change_source] 

 

        for s in change_sources: 

            if not interfaces.IChangeSource.providedBy(s): 

                msg = "c['change_source'] must be a list of change sources" 

                errors.addError(msg) 

                return 

 

        self.change_sources = change_sources 

 

    def load_status(self, filename, config_dict, errors): 

        if 'status' not in config_dict: 

            return 

        status = config_dict.get('status', []) 

 

        msg = "c['status'] must be a list of status receivers" 

        if not isinstance(status, (list, tuple)): 

            errors.addError(msg) 

            return 

 

        for s in status: 

            if not interfaces.IStatusReceiver.providedBy(s): 

                errors.addError(msg) 

                return 

 

        self.status = status 

 

 

    def load_user_managers(self, filename, config_dict, errors): 

        if 'user_managers' not in config_dict: 

            return 

        user_managers = config_dict['user_managers'] 

 

        msg = "c['user_managers'] must be a list of user managers" 

        if not isinstance(user_managers, (list, tuple)): 

            errors.addError(msg) 

            return 

 

        self.user_managers = user_managers 

 

 

    def check_single_master(self, errors): 

        # check additional problems that are only valid in a single-master 

        # installation 

        if self.multiMaster: 

            return 

 

        if not self.slaves: 

            errors.addError("no slaves are configured") 

 

        if not self.builders: 

            errors.addError("no builders are configured") 

 

        # check that all builders are implemented on this master 

        unscheduled_buildernames = set([ b.name for b in self.builders ]) 

        for s in self.schedulers.itervalues(): 

            for n in s.listBuilderNames(): 

                if n in unscheduled_buildernames: 

                    unscheduled_buildernames.remove(n) 

        if unscheduled_buildernames: 

            errors.addError("builder(s) %s have no schedulers to drive them" 

                            % (', '.join(unscheduled_buildernames),)) 

 

 

    def check_schedulers(self, errors): 

        all_buildernames = set([ b.name for b in self.builders ]) 

 

        for s in self.schedulers.itervalues(): 

            for n in s.listBuilderNames(): 

                if n not in all_buildernames: 

                    errors.addError("Unknown builder '%s' in scheduler '%s'" 

                                    % (n, s.name)) 

 

 

    def check_locks(self, errors): 

        # assert that all locks used by the Builds and their Steps are 

        # uniquely named. 

        lock_dict = {} 

        def check_lock(l): 

            if isinstance(l, locks.LockAccess): 

                l = l.lockid 

            if lock_dict.has_key(l.name): 

                if lock_dict[l.name] is not l: 

                    msg = "Two locks share the same name, '%s'" % l.name 

                    errors.addError(msg) 

            else: 

                lock_dict[l.name] = l 

 

        for b in self.builders: 

            if b.locks: 

                for l in b.locks: 

                    check_lock(l) 

 

    def check_builders(self, errors): 

        # look both for duplicate builder names, and for builders pointing 

        # to unknown slaves 

        slavenames = set([ s.slavename for s in self.slaves ]) 

        seen_names = set() 

        seen_builddirs = set() 

 

        for b in self.builders: 

            unknowns = set(b.slavenames) - slavenames 

            if unknowns: 

                errors.addError("builder '%s' uses unknown slaves %s" % 

                            (b.name, ", ".join(`u` for u in unknowns))) 

            if b.name in seen_names: 

                errors.addError("duplicate builder name '%s'" % b.name) 

            seen_names.add(b.name) 

 

            if b.builddir in seen_builddirs: 

                errors.addError("duplicate builder builddir '%s'" % b.builddir) 

            seen_builddirs.add(b.builddir) 

 

 

    def check_status(self, errors): 

        # allow status receivers to check themselves against the rest of the 

        # receivers 

        for s in self.status: 

            s.checkConfig(self.status, errors) 

 

 

    def check_horizons(self, errors): 

        if self.logHorizon is not None and self.buildHorizon is not None: 

            if self.logHorizon > self.buildHorizon: 

                errors.addError( 

                    "logHorizon must be less than or equal to buildHorizon") 

 

    def check_slavePortnum(self, errors): 

        if self.slavePortnum: 

            return 

 

        if self.slaves: 

            errors.addError( 

                    "slaves are configured, but no slavePortnum is set") 

        if self.debugPassword: 

            errors.addError( 

                    "debug client is configured, but no slavePortnum is set") 

 

 

class BuilderConfig: 

 

    def __init__(self, name=None, slavename=None, slavenames=None, 

            builddir=None, slavebuilddir=None, factory=None, category=None, 

            nextSlave=None, nextBuild=None, locks=None, env=None, 

            properties=None, mergeRequests=None): 

 

        errors = ConfigErrors([]) 

 

        # name is required, and can't start with '_' 

        if not name or type(name) not in (str, unicode): 

            errors.addError("builder's name is required") 

            name = '<unknown>' 

        elif name[0] == '_': 

            errors.addError( 

                "builder names must not start with an underscore: '%s'" % name) 

        self.name = name 

 

        # factory is required 

        if factory is None: 

            errors.addError("builder '%s' has no factory" % name) 

        self.factory = factory 

 

        # slavenames can be a single slave name or a list, and should also 

        # include slavename, if given 

        if type(slavenames) is str: 

            slavenames = [ slavenames ] 

        if slavenames: 

            if not isinstance(slavenames, list): 

                errors.addError( 

                    "builder '%s': slavenames must be a list or a string" % 

                        (name,)) 

        else: 

            slavenames = [] 

 

        if slavename: 

            if type(slavename) != str: 

                errors.addError( 

                    "builder '%s': slavename must be a string" % (name,)) 

            slavenames = slavenames + [ slavename ] 

        if not slavenames: 

            errors.addError( 

                "builder '%s': at least one slavename is required" % (name,)) 

 

        self.slavenames = slavenames 

 

        # builddir defaults to name 

        if builddir is None: 

            builddir = safeTranslate(name) 

        self.builddir = builddir 

 

        # slavebuilddir defaults to builddir 

        if slavebuilddir is None: 

            slavebuilddir = builddir 

        self.slavebuilddir = slavebuilddir 

 

        # remainder are optional 

        if category is not None and not isinstance(category, str): 

            errors.addError( 

                "builder '%s': category must be a string" % (name,)) 

 

        self.category = category or '' 

        self.nextSlave = nextSlave 

        if nextSlave and not callable(nextSlave): 

            errors.addError('nextSlave must be a callable') 

        self.nextBuild = nextBuild 

        if nextBuild and not callable(nextBuild): 

            errors.addError('nextBuild must be a callable') 

        self.locks = locks or [] 

        self.env = env or {} 

        if not isinstance(self.env, dict): 

            errors.addError("builder's env must be a dictionary") 

        self.properties = properties or {} 

        self.mergeRequests = mergeRequests 

 

        if errors: 

            raise errors 

 

 

    def getConfigDict(self): 

        # note: this method will disappear eventually - put your smarts in the 

        # constructor! 

        rv = { 

            'name': self.name, 

            'slavenames': self.slavenames, 

            'factory': self.factory, 

            'builddir': self.builddir, 

            'slavebuilddir': self.slavebuilddir, 

        } 

        if self.category: 

            rv['category'] = self.category 

        if self.nextSlave: 

            rv['nextSlave'] = self.nextSlave 

        if self.nextBuild: 

            rv['nextBuild'] = self.nextBuild 

        if self.locks: 

            rv['locks'] = self.locks 

        if self.env: 

            rv['env'] = self.env 

        if self.properties: 

            rv['properties'] = self.properties 

        if self.mergeRequests: 

            rv['mergeRequests'] = self.mergeRequests 

        return rv 

 

 

class ReconfigurableServiceMixin: 

 

    reconfig_priority = 128 

 

    @defer.inlineCallbacks 

    def reconfigService(self, new_config): 

        if not service.IServiceCollection.providedBy(self): 

            return 

 

        # get a list of child services to reconfigure 

        reconfigurable_services = [ svc 

                for svc in self 

                if isinstance(svc, ReconfigurableServiceMixin) ] 

 

        # sort by priority 

        reconfigurable_services.sort(key=lambda svc : -svc.reconfig_priority) 

 

        for svc in reconfigurable_services: 

            yield svc.reconfigService(new_config)