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

# 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 zope.interface import implements 

from twisted.python import log 

from twisted.internet import defer 

from buildbot import interfaces 

from buildbot.util.eventual import eventually 

 

class BuildRequestStatus: 

    implements(interfaces.IBuildRequestStatus) 

 

    def __init__(self, buildername, brid, status): 

        self.buildername = buildername 

        self.brid = brid 

        self.status = status 

        self.master = status.master 

 

        self._buildrequest = None 

        self._buildrequest_lock = defer.DeferredLock() 

 

    @defer.inlineCallbacks 

    def _getBuildRequest(self): 

        """ 

        Get the underlying BuildRequest object for this status.  This is a slow 

        operation! 

 

        @returns: BuildRequest instance or None, via Deferred 

        """ 

        # late binding to avoid an import cycle 

        from buildbot.process import buildrequest 

 

        # this is only set once, so no need to lock if we already have it 

        if self._buildrequest: 

            defer.returnValue(self._buildrequest) 

            return 

 

        yield self._buildrequest_lock.acquire() 

 

        try: 

            if not self._buildrequest: 

                brd = yield self.master.db.buildrequests.getBuildRequest( 

                                                                    self.brid) 

 

                br = yield buildrequest.BuildRequest.fromBrdict(self.master, 

                                                                    brd) 

                self._buildrequest = br 

        except: # try/finally isn't allowed in generators in older Pythons 

            self._buildrequest_lock.release() 

            raise 

 

        self._buildrequest_lock.release() 

 

        defer.returnValue(self._buildrequest) 

 

    def buildStarted(self, build): 

        self.status._buildrequest_buildStarted(build.status) 

        self.builds.append(build.status) 

 

    # methods called by our clients 

    @defer.inlineCallbacks 

    def getBsid(self): 

        br = yield self._getBuildRequest() 

        defer.returnValue(br.bsid) 

 

    @defer.inlineCallbacks 

    def getSourceStamp(self): 

        br = yield self._getBuildRequest() 

        defer.returnValue(br.source) 

 

    def getBuilderName(self): 

        return self.buildername 

 

    @defer.inlineCallbacks 

    def getBuilds(self): 

        builder = self.status.getBuilder(self.getBuilderName()) 

        builds = [] 

 

        bdicts = yield self.master.db.builds.getBuildsForRequest(self.brid) 

 

        buildnums = sorted([ bdict['number'] for bdict in bdicts ]) 

 

        for buildnum in buildnums: 

            bs = builder.getBuild(buildnum) 

            if bs: 

                builds.append(bs) 

        defer.returnValue(builds) 

 

    def subscribe(self, observer): 

        d = self.getBuilds() 

        def notify_old(oldbuilds): 

            for bs in oldbuilds: 

                eventually(observer, bs) 

        d.addCallback(notify_old) 

        d.addCallback(lambda _ : 

            self.status._buildrequest_subscribe(self.brid, observer)) 

        d.addErrback(log.err, 'while notifying subscribers') 

 

    def unsubscribe(self, observer): 

        self.status._buildrequest_unsubscribe(self.brid, observer) 

 

    @defer.inlineCallbacks 

    def getSubmitTime(self): 

        br = yield self._getBuildRequest() 

        defer.returnValue(br.submittedAt) 

 

    def asDict(self): 

        result = {} 

        # Constant 

        result['source'] = None # not available sync, sorry 

        result['builderName'] = self.buildername 

        result['submittedAt'] = None # not availably sync, sorry 

 

        # Transient 

        result['builds'] = [] # not available async, sorry 

        return result 

 

    @defer.inlineCallbacks 

    def asDict_async(self): 

        result = {} 

 

        ss = yield self.getSourceStamp() 

        result['source'] = ss.asDict() 

        result['builderName'] = self.getBuilderName() 

        result['submittedAt'] = yield self.getSubmitTime() 

 

        builds = yield self.getBuilds() 

        result['builds'] = [ build.asDict() for build in builds ] 

 

        defer.returnValue(result)