~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/importd/bzrscan.py

  • Committer: Robert Collins
  • Date: 2005-10-31 18:29:12 UTC
  • mfrom: (1102.1.126)
  • mto: (1102.1.138) (63.1.155)
  • mto: This revision was merged to the branch mainline in revision 2836.
  • Revision ID: robertc@robertcollins.net-20051031182912-5b96cbfc568d7a46
Merge ddaa and my branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2004-2005 Canonical Ltd.  All rights reserved.
 
2
 
 
3
"""Fill the database with information from Bazaar branch."""
 
4
 
 
5
import sys
 
6
import os
 
7
from datetime import datetime
 
8
 
 
9
from buildbot.process.base import ConfigurableBuildFactory, ConfigurableBuild
 
10
from buildbot.process.step import ShellCommand
 
11
 
 
12
from canonical.launchpad.database import Branch
 
13
from canonical.database.constants import UTC_NOW
 
14
 
 
15
from importd.util import (
 
16
    getTxnManager, tryToAbortTransaction,
 
17
    NotifyingBuild)
 
18
 
 
19
__all__ = ['bzrScanBuilders']
 
20
 
 
21
__metaclass__ = type
 
22
 
 
23
 
 
24
def bzrScanBuilders(slavenames, runner_path=None, periodic=None):
 
25
    builders = []
 
26
    getTxnManager().begin()
 
27
    try:
 
28
        branches = list(Branch.select())
 
29
        branches.sort(key=lambda x: x.id)
 
30
        for branch in branches:
 
31
            name = 'branch-%03d' % (branch.id,)
 
32
            slavename = slavenames[hash(name) % len(slavenames)]
 
33
            builddir = 'bzrscan-jobs'
 
34
            if runner_path is None:
 
35
                runner_path = os.path.join(
 
36
                    os.path.dirname(__file__), 'bzrsync.py')
 
37
            else:
 
38
                runner_path = str(runner_path)
 
39
            if periodic is None:
 
40
                periodic = 24 * 60 * 60 # one day
 
41
            branch_name = '%s %s %s' % (
 
42
                branch.owner.name, branch.product_name, branch.name)
 
43
            scanjob = {'id': branch.id, 'branch_name': branch_name,
 
44
                       'interval': periodic}
 
45
            factory = BzrScanBuildFactory(scanjob, runner_path)
 
46
            builderDict = {'name': name, 'slavename': slavename,
 
47
                           'builddir': builddir, 'factory': factory,
 
48
                           'periodicBuildTime': periodic}
 
49
            builders.append(builderDict)
 
50
        getTxnManager().abort()
 
51
    except:
 
52
        tryToAbortTransaction()
 
53
        raise
 
54
    return builders
 
55
 
 
56
 
 
57
class BzrScanBuildFactory(ConfigurableBuildFactory):
 
58
 
 
59
    @property
 
60
    def buildClass(self):
 
61
        return BzrScanBuild
 
62
 
 
63
    def __init__(self, scanjob, runner_path):
 
64
        self.steps = []
 
65
        self.scanjob = scanjob
 
66
        self.runner_path = runner_path
 
67
        self.addSteps()
 
68
 
 
69
    def addSteps(self):
 
70
        self.steps.append((BzrScanShellCommand, {
 
71
            'timeout': 1200,
 
72
            'workdir': None,
 
73
            'branch_name': self.scanjob['branch_name'],
 
74
            'command': [sys.executable, self.runner_path,
 
75
                        str(self.scanjob['id'])]}))
 
76
 
 
77
    def newBuild(self):
 
78
        build = ConfigurableBuildFactory.newBuild(self)
 
79
        build.scanjob = self.scanjob
 
80
        return build
 
81
 
 
82
 
 
83
class BzrScanShellCommand(ShellCommand):
 
84
 
 
85
    def __init__(self, branch_name, **kwargs):
 
86
        ShellCommand.__init__(self, **kwargs)
 
87
        self.branch_name = branch_name
 
88
 
 
89
    def words(self):
 
90
        """Short display of BzrScan steps in buildbot."""
 
91
        return [self.branch_name]
 
92
 
 
93
 
 
94
class BzrScanBuild(NotifyingBuild):
 
95
 
 
96
    def getObserver(self):
 
97
        return BzrScanBuildObserver(self)
 
98
 
 
99
 
 
100
class BzrScanBuildObserver:
 
101
 
 
102
    def __init__(self, build):
 
103
        self.build = build
 
104
        scanjob = build.scanjob
 
105
        self.branch_id = scanjob['id']
 
106
 
 
107
    def startBuild(self):
 
108
        getTxnManager().begin()
 
109
        # do nothing
 
110
        getTxnManager().commit()
 
111
 
 
112
    def buildFinished(self, successful):
 
113
        getTxnManager().begin()
 
114
        self.setLastMirrorAttempt()
 
115
        getTxnManager().commit()
 
116
 
 
117
    def setLastMirrorAttempt(self):
 
118
        self.getBranch().last_mirror_attempt = UTC_NOW
 
119
 
 
120
    def getBranch(self):
 
121
        return Branch.get(self.branch_id)