Package buildbot :: Package db :: Module sourcestamps
[frames] | no frames]

Source Code for Module buildbot.db.sourcestamps

  1  # This file is part of Buildbot.  Buildbot is free software: you can 
  2  # redistribute it and/or modify it under the terms of the GNU General Public 
  3  # License as published by the Free Software Foundation, version 2. 
  4  # 
  5  # This program is distributed in the hope that it will be useful, but WITHOUT 
  6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  8  # details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 51 
 12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 13  # 
 14  # Copyright Buildbot Team Members 
 15   
 16  import base64 
 17  import sqlalchemy as sa 
 18  from twisted.internet import defer 
 19  from twisted.python import log 
 20  from buildbot.db import base 
21 22 -class SsDict(dict):
23 pass
24
25 -class SsList(list):
26 pass
27
28 -class SourceStampsConnectorComponent(base.DBConnectorComponent):
29 # Documentation is in developer/database.rst 30
31 - def addSourceStamp(self, branch, revision, repository, 32 project, sourcestampsetid, codebase='', 33 patch_body=None, patch_level=0, patch_author="", 34 patch_comment="", patch_subdir=None, changeids=[]):
35 def thd(conn): 36 transaction = conn.begin() 37 38 # handle inserting a patch 39 patchid = None 40 if patch_body is not None: 41 ins = self.db.model.patches.insert() 42 r = conn.execute(ins, dict( 43 patchlevel=patch_level, 44 patch_base64=base64.b64encode(patch_body), 45 patch_author=patch_author, 46 patch_comment=patch_comment, 47 subdir=patch_subdir)) 48 patchid = r.inserted_primary_key[0] 49 50 # insert the sourcestamp itself 51 tbl = self.db.model.sourcestamps 52 self.check_length(tbl.c.branch, branch) 53 self.check_length(tbl.c.revision, revision) 54 self.check_length(tbl.c.repository, repository) 55 self.check_length(tbl.c.project, project) 56 57 r = conn.execute(tbl.insert(), dict( 58 branch=branch, 59 revision=revision, 60 patchid=patchid, 61 repository=repository, 62 codebase=codebase, 63 project=project, 64 sourcestampsetid=sourcestampsetid)) 65 ssid = r.inserted_primary_key[0] 66 67 # handle inserting change ids 68 if changeids: 69 ins = self.db.model.sourcestamp_changes.insert() 70 conn.execute(ins, [ 71 dict(sourcestampid=ssid, changeid=changeid) 72 for changeid in changeids ]) 73 74 transaction.commit() 75 76 # and return the new ssid 77 return ssid
78 return self.db.pool.do(thd)
79 80 @base.cached("sssetdicts") 81 @defer.inlineCallbacks
82 - def getSourceStamps(self,sourcestampsetid):
83 def getSourceStampIds(sourcestampsetid): 84 def thd(conn): 85 tbl = self.db.model.sourcestamps 86 q = sa.select([tbl.c.id], 87 whereclause=(tbl.c.sourcestampsetid == sourcestampsetid)) 88 res = conn.execute(q) 89 return [ row.id for row in res.fetchall() ]
90 return self.db.pool.do(thd) 91 ssids = yield getSourceStampIds(sourcestampsetid) 92 93 sslist=SsList() 94 for ssid in ssids: 95 sourcestamp = yield self.getSourceStamp(ssid) 96 sslist.append(sourcestamp) 97 defer.returnValue(sslist) 98 99 @base.cached("ssdicts")
100 - def getSourceStamp(self, ssid):
101 def thd(conn): 102 tbl = self.db.model.sourcestamps 103 q = tbl.select(whereclause=(tbl.c.id == ssid)) 104 res = conn.execute(q) 105 row = res.fetchone() 106 if not row: 107 return None 108 ssdict = SsDict(ssid=ssid, branch=row.branch, sourcestampsetid=row.sourcestampsetid, 109 revision=row.revision, patch_body=None, patch_level=None, 110 patch_author=None, patch_comment=None, patch_subdir=None, 111 repository=row.repository, codebase=row.codebase, 112 project=row.project, 113 changeids=set([])) 114 patchid = row.patchid 115 res.close() 116 117 # fetch the patch, if necessary 118 if patchid is not None: 119 tbl = self.db.model.patches 120 q = tbl.select(whereclause=(tbl.c.id == patchid)) 121 res = conn.execute(q) 122 row = res.fetchone() 123 if row: 124 # note the subtle renaming here 125 ssdict['patch_level'] = row.patchlevel 126 ssdict['patch_subdir'] = row.subdir 127 ssdict['patch_author'] = row.patch_author 128 ssdict['patch_comment'] = row.patch_comment 129 body = base64.b64decode(row.patch_base64) 130 ssdict['patch_body'] = body 131 else: 132 log.msg('patchid %d, referenced from ssid %d, not found' 133 % (patchid, ssid)) 134 res.close() 135 136 # fetch change ids 137 tbl = self.db.model.sourcestamp_changes 138 q = tbl.select(whereclause=(tbl.c.sourcestampid == ssid)) 139 res = conn.execute(q) 140 for row in res: 141 ssdict['changeids'].add(row.changeid) 142 res.close() 143 144 return ssdict
145 return self.db.pool.do(thd) 146