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

# 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 

 

import base64 

import sqlalchemy as sa 

from twisted.internet import defer 

from twisted.python import log 

from buildbot.db import base 

 

class SsDict(dict): 

    pass 

 

class SsList(list): 

    pass 

 

class SourceStampsConnectorComponent(base.DBConnectorComponent): 

    # Documentation is in developer/database.rst 

 

    def addSourceStamp(self, branch, revision, repository, 

                          project, sourcestampsetid, codebase='', 

                          patch_body=None, patch_level=0, patch_author="", 

                          patch_comment="", patch_subdir=None, changeids=[]): 

        def thd(conn): 

            transaction = conn.begin() 

 

            # handle inserting a patch 

            patchid = None 

            if patch_body is not None: 

                ins = self.db.model.patches.insert() 

                r = conn.execute(ins, dict( 

                    patchlevel=patch_level, 

                    patch_base64=base64.b64encode(patch_body), 

                    patch_author=patch_author, 

                    patch_comment=patch_comment, 

                    subdir=patch_subdir)) 

                patchid = r.inserted_primary_key[0] 

 

            # insert the sourcestamp itself 

            tbl = self.db.model.sourcestamps 

            self.check_length(tbl.c.branch, branch) 

            self.check_length(tbl.c.revision, revision) 

            self.check_length(tbl.c.repository, repository) 

            self.check_length(tbl.c.project, project) 

 

            r = conn.execute(tbl.insert(), dict( 

                branch=branch, 

                revision=revision, 

                patchid=patchid, 

                repository=repository, 

                codebase=codebase, 

                project=project, 

                sourcestampsetid=sourcestampsetid)) 

            ssid = r.inserted_primary_key[0] 

 

            # handle inserting change ids 

            if changeids: 

                ins = self.db.model.sourcestamp_changes.insert() 

                conn.execute(ins, [ 

                    dict(sourcestampid=ssid, changeid=changeid) 

                    for changeid in changeids ]) 

 

            transaction.commit() 

 

            # and return the new ssid 

            return ssid 

        return self.db.pool.do(thd) 

 

    @base.cached("sssetdicts") 

    @defer.inlineCallbacks 

    def getSourceStamps(self,sourcestampsetid): 

        def getSourceStampIds(sourcestampsetid): 

            def thd(conn): 

                tbl = self.db.model.sourcestamps 

                q = sa.select([tbl.c.id], 

                       whereclause=(tbl.c.sourcestampsetid == sourcestampsetid)) 

                res = conn.execute(q) 

                return [ row.id for row in res.fetchall() ] 

            return self.db.pool.do(thd) 

        ssids = yield getSourceStampIds(sourcestampsetid) 

 

        sslist=SsList() 

        for ssid in ssids: 

            sourcestamp = yield self.getSourceStamp(ssid) 

            sslist.append(sourcestamp) 

        defer.returnValue(sslist) 

 

    @base.cached("ssdicts") 

    def getSourceStamp(self, ssid): 

        def thd(conn): 

            tbl = self.db.model.sourcestamps 

            q = tbl.select(whereclause=(tbl.c.id == ssid)) 

            res = conn.execute(q) 

            row = res.fetchone() 

            if not row: 

                return None 

            ssdict = SsDict(ssid=ssid, branch=row.branch, sourcestampsetid=row.sourcestampsetid, 

                    revision=row.revision, patch_body=None, patch_level=None, 

                    patch_author=None, patch_comment=None, patch_subdir=None, 

                    repository=row.repository, codebase=row.codebase, 

                    project=row.project, 

                    changeids=set([])) 

            patchid = row.patchid 

            res.close() 

 

            # fetch the patch, if necessary 

            if patchid is not None: 

                tbl = self.db.model.patches 

                q = tbl.select(whereclause=(tbl.c.id == patchid)) 

                res = conn.execute(q) 

                row = res.fetchone() 

                if row: 

                    # note the subtle renaming here 

                    ssdict['patch_level'] = row.patchlevel 

                    ssdict['patch_subdir'] = row.subdir 

                    ssdict['patch_author'] = row.patch_author 

                    ssdict['patch_comment'] = row.patch_comment 

                    body = base64.b64decode(row.patch_base64) 

                    ssdict['patch_body'] = body 

                else: 

                    log.msg('patchid %d, referenced from ssid %d, not found' 

                            % (patchid, ssid)) 

                res.close() 

 

            # fetch change ids 

            tbl = self.db.model.sourcestamp_changes 

            q = tbl.select(whereclause=(tbl.c.sourcestampid == ssid)) 

            res = conn.execute(q) 

            for row in res: 

                ssdict['changeids'].add(row.changeid) 

            res.close() 

 

            return ssdict 

        return self.db.pool.do(thd)