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

# 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.internet import reactor 

from buildbot.db import base 

from buildbot.util import epoch2datetime 

 

class BuildsConnectorComponent(base.DBConnectorComponent): 

    # Documentation is in developer/database.rst 

 

    def getBuild(self, bid): 

        def thd(conn): 

            tbl = self.db.model.builds 

            res = conn.execute(tbl.select(whereclause=(tbl.c.id == bid))) 

            row = res.fetchone() 

 

            rv = None 

            if row: 

                rv = self._bdictFromRow(row) 

            res.close() 

            return rv 

        return self.db.pool.do(thd) 

 

    def getBuildsForRequest(self, brid): 

        def thd(conn): 

            tbl = self.db.model.builds 

            q = tbl.select(whereclause=(tbl.c.brid == brid)) 

            res = conn.execute(q) 

            return [ self._bdictFromRow(row) for row in res.fetchall() ] 

        return self.db.pool.do(thd) 

 

    def addBuild(self, brid, number, _reactor=reactor): 

        def thd(conn): 

            start_time = _reactor.seconds() 

            r = conn.execute(self.db.model.builds.insert(), 

                    dict(number=number, brid=brid, start_time=start_time, 

                        finish_time=None)) 

            return r.inserted_primary_key[0] 

        return self.db.pool.do(thd) 

 

    def finishBuilds(self, bids, _reactor=reactor): 

        def thd(conn): 

            transaction = conn.begin() 

            tbl = self.db.model.builds 

            now = _reactor.seconds() 

 

            # split the bids into batches, so as not to overflow the parameter 

            # lists of the database interface 

            remaining = bids 

            while remaining: 

                batch, remaining = remaining[:100], remaining[100:] 

                q = tbl.update(whereclause=(tbl.c.id.in_(batch))) 

                conn.execute(q, finish_time=now) 

 

            transaction.commit() 

        return self.db.pool.do(thd) 

 

    def _bdictFromRow(self, row): 

        def mkdt(epoch): 

            if epoch: 

                return epoch2datetime(epoch) 

 

        return dict( 

            bid=row.id, 

            brid=row.brid, 

            number=row.number, 

            start_time=mkdt(row.start_time), 

            finish_time=mkdt(row.finish_time))