This file is indexed.

/usr/share/pyshared/buildbot/status/client.py is in buildbot 0.8.6p1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# 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 twisted.spread import pb
from twisted.python import components, log as twlog
from twisted.internet import reactor
from twisted.application import strports
from twisted.cred import portal, checkers

from buildbot import interfaces
from zope.interface import Interface, implements
from buildbot.status import logfile, base
from buildbot.changes import changes

class IRemote(Interface):
    pass

def makeRemote(obj):
    # we want IRemote(None) to be None, but you can't really do that with
    # adapters, so we fake it
    if obj is None:
        return None
    return IRemote(obj)


class RemoteBuildSet(pb.Referenceable):
    def __init__(self, buildset):
        self.b = buildset

    def remote_getSourceStamp(self):
        return self.b.getSourceStamp()

    def remote_getReason(self):
        return self.b.getReason()

    def remote_getID(self):
        return self.b.getID()

    def remote_getBuilderNames(self):
        return self.b.getBuilderNames() # note: passes along the Deferred

    def remote_getBuildRequests(self):
        """Returns a list of (builderName, BuildRequest) tuples."""
        d = self.b.getBuilderNamesAndBuildRequests()
        def add_remote(buildrequests):
            for k,v in buildrequests.iteritems():
                buildrequests[k] = IRemote(v)
            return buildrequests.items()
        d.addCallback(add_remote)
        return d

    def remote_isFinished(self):
        return self.b.isFinished()

    def remote_waitUntilFinished(self):
        d = self.b.waitUntilFinished()
        d.addCallback(lambda res: self)
        return d

    def remote_getResults(self):
        return self.b.getResults()

components.registerAdapter(RemoteBuildSet,
                           interfaces.IBuildSetStatus, IRemote)    


class RemoteBuilder(pb.Referenceable):
    def __init__(self, builder):
        self.b = builder

    def remote_getName(self):
        return self.b.getName()

    def remote_getCategory(self):
        return self.b.getCategory()

    def remote_getState(self):
        state, builds = self.b.getState()
        return (state,
                None, # TODO: remove leftover ETA
                [makeRemote(b) for b in builds])

    def remote_getSlaves(self):
        return [IRemote(s) for s in self.b.getSlaves()]

    def remote_getLastFinishedBuild(self):
        return makeRemote(self.b.getLastFinishedBuild())

    def remote_getCurrentBuilds(self):
        return [IRemote(b) for b in self.b.getCurrentBuilds()]

    def remote_getBuild(self, number):
        return makeRemote(self.b.getBuild(number))

    def remote_getEvent(self, number):
        return IRemote(self.b.getEvent(number))

components.registerAdapter(RemoteBuilder,
                           interfaces.IBuilderStatus, IRemote)    


class RemoteBuildRequest(pb.Referenceable):
    def __init__(self, buildreq):
        self.b = buildreq
        # mapping of observers (RemoteReference instances) to local callable
        # objects that have been passed to BuildRequestStatus.subscribe
        self.observers = []

    def remote_getSourceStamp(self):
        # note that this now returns a Deferred
        return self.b.getSourceStamp()

    def remote_getBuilderName(self):
        return self.b.getBuilderName()

    def remote_subscribe(self, observer):
        """The observer's remote_newbuild method will be called (with two
        arguments: the RemoteBuild object, and our builderName) for each new
        Build that is created to handle this BuildRequest."""
        def send(bs):
            d = observer.callRemote("newbuild",
                                    IRemote(bs), self.b.getBuilderName())
            d.addErrback(twlog.err,
                    "while calling client-side remote_newbuild")
        self.observers.append((observer, send))
        self.b.subscribe(send)

    def remote_unsubscribe(self, observer):
        for i, (obs, send) in enumerate(self.observers):
            if obs == observer:
                del self.observers[i]
                self.b.unsubscribe(send)
                break

components.registerAdapter(RemoteBuildRequest,
                           interfaces.IBuildRequestStatus, IRemote)    

class RemoteBuild(pb.Referenceable):
    def __init__(self, build):
        self.b = build
        self.observers = []

    def remote_getBuilderName(self):
        return self.b.getBuilder().getName()

    def remote_getNumber(self):
        return self.b.getNumber()

    def remote_getReason(self):
        return self.b.getReason()

    def remote_getChanges(self):
        return [IRemote(c) for c in self.b.getChanges()]

    def remote_getRevisions(self):
        return self.b.getRevisions()

    def remote_getResponsibleUsers(self):
        return self.b.getResponsibleUsers()

    def remote_getSteps(self):
        return [IRemote(s) for s in self.b.getSteps()]

    def remote_getTimes(self):
        return self.b.getTimes()

    def remote_isFinished(self):
        return self.b.isFinished()

    def remote_waitUntilFinished(self):
        # the Deferred returned by callRemote() will fire when this build is
        # finished
        d = self.b.waitUntilFinished()
        d.addCallback(lambda res: self)
        return d

    def remote_getETA(self):
        return self.b.getETA()

    def remote_getCurrentStep(self):
        return makeRemote(self.b.getCurrentStep())

    def remote_getText(self):
        return self.b.getText()

    def remote_getResults(self):
        return self.b.getResults()

    def remote_getLogs(self):
        logs = {}
        for name,log in self.b.getLogs().items():
            logs[name] = IRemote(log)
        return logs

    def remote_subscribe(self, observer, updateInterval=None):
        """The observer will have remote_stepStarted(buildername, build,
        stepname, step), remote_stepFinished(buildername, build, stepname,
        step, results), and maybe remote_buildETAUpdate(buildername, build,
        eta)) messages sent to it."""
        self.observers.append(observer)
        s = BuildSubscriber(observer)
        self.b.subscribe(s, updateInterval)

    def remote_unsubscribe(self, observer):
        # TODO: is the observer automatically unsubscribed when the build
        # finishes? Or are they responsible for unsubscribing themselves
        # anyway? How do we avoid a race condition here?
        for o in self.observers:
            if o == observer:
                self.observers.remove(o)


components.registerAdapter(RemoteBuild,
                           interfaces.IBuildStatus, IRemote)    

class BuildSubscriber:
    def __init__(self, observer):
        self.observer = observer

    def buildETAUpdate(self, build, eta):
        self.observer.callRemote("buildETAUpdate",
                                 build.getBuilder().getName(),
                                 IRemote(build),
                                 eta)

    def stepStarted(self, build, step):
        self.observer.callRemote("stepStarted",
                                 build.getBuilder().getName(),
                                 IRemote(build),
                                 step.getName(), IRemote(step))
        return None

    def stepFinished(self, build, step, results):
        self.observer.callRemote("stepFinished",
                                 build.getBuilder().getName(),
                                 IRemote(build),
                                 step.getName(), IRemote(step),
                                 results)


class RemoteBuildStep(pb.Referenceable):
    def __init__(self, step):
        self.s = step

    def remote_getName(self):
        return self.s.getName()

    def remote_getBuild(self):
        return IRemote(self.s.getBuild())

    def remote_getTimes(self):
        return self.s.getTimes()

    def remote_getExpectations(self):
        return self.s.getExpectations()

    def remote_getLogs(self):
        logs = {}
        for log in self.s.getLogs():
            logs[log.getName()] = IRemote(log)
        return logs

    def remote_isFinished(self):
        return self.s.isFinished()

    def remote_waitUntilFinished(self):
        return self.s.waitUntilFinished() # returns a Deferred

    def remote_getETA(self):
        return self.s.getETA()

    def remote_getText(self):
        return self.s.getText()

    def remote_getResults(self):
        return self.s.getResults()

components.registerAdapter(RemoteBuildStep,
                           interfaces.IBuildStepStatus, IRemote)    

class RemoteSlave:
    def __init__(self, slave):
        self.s = slave

    def remote_getName(self):
        return self.s.getName()
    def remote_getAdmin(self):
        return self.s.getAdmin()
    def remote_getHost(self):
        return self.s.getHost()
    def remote_isConnected(self):
        return self.s.isConnected()

components.registerAdapter(RemoteSlave,
                           interfaces.ISlaveStatus, IRemote)

class RemoteEvent:
    def __init__(self, event):
        self.e = event

    def remote_getTimes(self):
        return self.s.getTimes()
    def remote_getText(self):
        return self.s.getText()

components.registerAdapter(RemoteEvent,
                           interfaces.IStatusEvent, IRemote)

class RemoteLog(pb.Referenceable):
    def __init__(self, log):
        self.l = log

    def remote_getName(self):
        return self.l.getName()

    def remote_isFinished(self):
        return self.l.isFinished()
    def remote_waitUntilFinished(self):
        d = self.l.waitUntilFinished()
        d.addCallback(lambda res: self)
        return d

    def remote_getText(self):
        return self.l.getText()
    def remote_getTextWithHeaders(self):
        return self.l.getTextWithHeaders()
    def remote_getChunks(self):
        return self.l.getChunks()
    # TODO: subscription interface

components.registerAdapter(RemoteLog, logfile.LogFile, IRemote)
# TODO: something similar for builder.HTMLLogfile ?

class RemoteChange:
    def __init__(self, change):
        self.c = change

    def getWho(self):
        return self.c.who
    def getFiles(self):
        return self.c.files
    def getComments(self):
        return self.c.comments

components.registerAdapter(RemoteChange, changes.Change, IRemote)


class StatusClientPerspective(base.StatusReceiverPerspective):

    subscribed = None
    client = None

    def __init__(self, status):
        self.status = status # the IStatus
        self.subscribed_to_builders = [] # Builders to which we're subscribed
        self.subscribed_to = [] # everything else we're subscribed to

    def __getstate__(self):
        d = self.__dict__.copy()
        d['client'] = None
        return d

    def attached(self, mind):
        #twlog.msg("StatusClientPerspective.attached")
        return self

    def detached(self, mind):
        twlog.msg("PB client detached")
        self.client = None
        for name in self.subscribed_to_builders:
            twlog.msg(" unsubscribing from Builder(%s)" % name)
            self.status.getBuilder(name).unsubscribe(self)
        for s in self.subscribed_to:
            twlog.msg(" unsubscribe from %s" % s)
            s.unsubscribe(self)
        self.subscribed = None

    def perspective_subscribe(self, mode, interval, target):
        """The remote client wishes to subscribe to some set of events.
        'target' will be sent remote messages when these events happen.
        'mode' indicates which events are desired: it is a string with one
        of the following values:

        'builders': builderAdded, builderRemoved
        'builds': those plus builderChangedState, buildStarted, buildFinished
        'steps': all those plus buildETAUpdate, stepStarted, stepFinished
        'logs': all those plus stepETAUpdate, logStarted, logFinished
        'full': all those plus logChunk (with the log contents)
        

        Messages are defined by buildbot.interfaces.IStatusReceiver .
        'interval' is used to specify how frequently ETAUpdate messages
        should be sent.

        Raising or lowering the subscription level will take effect starting
        with the next build or step."""

        assert mode in ("builders", "builds", "steps", "logs", "full")
        assert target
        twlog.msg("PB subscribe(%s)" % mode)

        self.client = target
        self.subscribed = mode
        self.interval = interval
        self.subscribed_to.append(self.status)
        # wait a moment before subscribing, so the new-builder messages
        # won't appear before this remote method finishes
        reactor.callLater(0, self.status.subscribe, self)
        return None

    def perspective_unsubscribe(self):
        twlog.msg("PB unsubscribe")
        self.status.unsubscribe(self)
        self.subscribed_to.remove(self.status)
        self.client = None

    def perspective_getBuildSets(self):
        """This returns tuples of (buildset, bsid), because that is much more
        convenient for tryclient."""
        d = self.status.getBuildSets()
        def make_remotes(buildsets):
            return [(IRemote(s), s.id) for s in buildsets]
        d.addCallback(make_remotes)
        return d

    def perspective_getBuilderNames(self):
        return self.status.getBuilderNames()

    def perspective_getBuilder(self, name):
        b = self.status.getBuilder(name)
        return IRemote(b)

    def perspective_getSlave(self, name):
        s = self.status.getSlave(name)
        return IRemote(s)

    def perspective_ping(self):
        """Ping method to allow pb clients to validate their connections."""
        return "pong"

    # IStatusReceiver methods, invoked if we've subscribed

    # mode >= builder
    def builderAdded(self, name, builder):
        self.client.callRemote("builderAdded", name, IRemote(builder))
        if self.subscribed in ("builds", "steps", "logs", "full"):
            self.subscribed_to_builders.append(name)
            return self
        return None

    def builderChangedState(self, name, state):
        self.client.callRemote("builderChangedState", name, state, None)
        # TODO: remove leftover ETA argument

    def builderRemoved(self, name):
        if name in self.subscribed_to_builders:
            self.subscribed_to_builders.remove(name)
        self.client.callRemote("builderRemoved", name)

    def buildsetSubmitted(self, buildset):
        # TODO: deliver to client, somehow
        pass

    # mode >= builds
    def buildStarted(self, name, build):
        self.client.callRemote("buildStarted", name, IRemote(build))
        if self.subscribed in ("steps", "logs", "full"):
            self.subscribed_to.append(build)
            return (self, self.interval)
        return None

    def buildFinished(self, name, build, results):
        if build in self.subscribed_to:
            # we might have joined during the build
            self.subscribed_to.remove(build)
        self.client.callRemote("buildFinished",
                               name, IRemote(build), results)

    # mode >= steps
    def buildETAUpdate(self, build, eta):
        self.client.callRemote("buildETAUpdate",
                               build.getBuilder().getName(), IRemote(build),
                               eta)

    def stepStarted(self, build, step):
        # we add some information here so the client doesn't have to do an
        # extra round-trip
        self.client.callRemote("stepStarted",
                               build.getBuilder().getName(), IRemote(build),
                               step.getName(), IRemote(step))
        if self.subscribed in ("logs", "full"):
            self.subscribed_to.append(step)
            return (self, self.interval)
        return None

    def stepFinished(self, build, step, results):
        self.client.callRemote("stepFinished",
                               build.getBuilder().getName(), IRemote(build),
                               step.getName(), IRemote(step),
                               results)
        if step in self.subscribed_to:
            # eventually (through some new subscription method) we could
            # join in the middle of the step
            self.subscribed_to.remove(step)

    # mode >= logs
    def stepETAUpdate(self, build, step, ETA, expectations):
        self.client.callRemote("stepETAUpdate",
                               build.getBuilder().getName(), IRemote(build),
                               step.getName(), IRemote(step),
                               ETA, expectations)

    def logStarted(self, build, step, log):
        # TODO: make the HTMLLog adapter
        rlog = IRemote(log, None)
        if not rlog:
            print "hey, couldn't adapt %s to IRemote" % log
        self.client.callRemote("logStarted",
                               build.getBuilder().getName(), IRemote(build),
                               step.getName(), IRemote(step),
                               log.getName(), IRemote(log, None))
        if self.subscribed in ("full",):
            self.subscribed_to.append(log)
            return self
        return None

    def logFinished(self, build, step, log):
        self.client.callRemote("logFinished",
                               build.getBuilder().getName(), IRemote(build),
                               step.getName(), IRemote(step),
                               log.getName(), IRemote(log, None))
        if log in self.subscribed_to:
            self.subscribed_to.remove(log)

    # mode >= full
    def logChunk(self, build, step, log, channel, text):
        self.client.callRemote("logChunk",
                               build.getBuilder().getName(), IRemote(build),
                               step.getName(), IRemote(step),
                               log.getName(), IRemote(log),
                               channel, text)


class PBListener(base.StatusReceiverMultiService):
    """I am a listener for PB-based status clients."""

    compare_attrs = ["port", "cred"]
    implements(portal.IRealm)

    def __init__(self, port, user="statusClient", passwd="clientpw"):
        base.StatusReceiverMultiService.__init__(self)
        if type(port) is int:
            port = "tcp:%d" % port
        self.port = port
        self.cred = (user, passwd)
        p = portal.Portal(self)
        c = checkers.InMemoryUsernamePasswordDatabaseDontUse()
        c.addUser(user, passwd)
        p.registerChecker(c)
        f = pb.PBServerFactory(p)
        s = strports.service(port, f)
        s.setServiceParent(self)

    def setServiceParent(self, parent):
        base.StatusReceiverMultiService.setServiceParent(self, parent)
        self.status = parent

    def requestAvatar(self, avatarID, mind, interface):
        assert interface == pb.IPerspective
        p = StatusClientPerspective(self.status)
        p.attached(mind) # perhaps .callLater(0) ?
        return (pb.IPerspective, p,
                lambda p=p,mind=mind: p.detached(mind))