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, project, sourcestampsetid, 32 patch_body=None, patch_level=0, patch_author="", 33 patch_comment="", patch_subdir=None, changeids=[]):
34 def thd(conn): 35 transaction = conn.begin() 36 37 # handle inserting a patch 38 patchid = None 39 if patch_body is not None: 40 ins = self.db.model.patches.insert() 41 r = conn.execute(ins, dict( 42 patchlevel=patch_level, 43 patch_base64=base64.b64encode(patch_body), 44 patch_author=patch_author, 45 patch_comment=patch_comment, 46 subdir=patch_subdir)) 47 patchid = r.inserted_primary_key[0] 48 49 # insert the sourcestamp itself 50 tbl = self.db.model.sourcestamps 51 self.check_length(tbl.c.branch, branch) 52 self.check_length(tbl.c.revision, revision) 53 self.check_length(tbl.c.repository, repository) 54 self.check_length(tbl.c.project, project) 55 56 r = conn.execute(tbl.insert(), dict( 57 branch=branch, 58 revision=revision, 59 patchid=patchid, 60 repository=repository, 61 project=project, 62 sourcestampsetid=sourcestampsetid)) 63 ssid = r.inserted_primary_key[0] 64 65 # handle inserting change ids 66 if changeids: 67 ins = self.db.model.sourcestamp_changes.insert() 68 conn.execute(ins, [ 69 dict(sourcestampid=ssid, changeid=changeid) 70 for changeid in changeids ]) 71 72 transaction.commit() 73 74 # and return the new ssid 75 return ssid
76 return self.db.pool.do(thd)
77 78 @base.cached("sssetdicts") 79 @defer.deferredGenerator
80 - def getSourceStamps(self,sourcestampsetid):
81 def getSourceStampIds(sourcestampsetid): 82 def thd(conn): 83 tbl = self.db.model.sourcestamps 84 q = sa.select([tbl.c.id], 85 whereclause=(tbl.c.sourcestampsetid == sourcestampsetid)) 86 res = conn.execute(q) 87 return [ row.id for row in res.fetchall() ]
88 return self.db.pool.do(thd) 89 wfd = defer.waitForDeferred(getSourceStampIds(sourcestampsetid)) 90 yield wfd 91 ssids = wfd.getResult() 92 sslist=SsList() 93 for ssid in ssids: 94 wfd = defer.waitForDeferred(self.getSourceStamp(ssid)) 95 yield wfd 96 sourcestamp = wfd.getResult() 97 sslist.append(sourcestamp) 98 yield sslist 99 100 @base.cached("ssdicts")
101 - def getSourceStamp(self, ssid):
102 def thd(conn): 103 tbl = self.db.model.sourcestamps 104 q = tbl.select(whereclause=(tbl.c.id == ssid)) 105 res = conn.execute(q) 106 row = res.fetchone() 107 if not row: 108 return None 109 ssdict = SsDict(ssid=ssid, branch=row.branch, sourcestampsetid=row.sourcestampsetid, 110 revision=row.revision, patch_body=None, patch_level=None, 111 patch_author=None, patch_comment=None, patch_subdir=None, 112 repository=row.repository, 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