aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/steps/package/rpm/mock.py
blob: 8c7e3491cafb7e3737a2485f10abbfb1f42ad87c (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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.
#
# Portions Copyright Buildbot Team Members
# Portions Copyright Marius Rieder <marius.rieder@durchmesser.ch>
"""
Steps and objects related to mock building.
"""

import re

from buildbot.steps.shell import ShellCommand
from buildbot.process import buildstep
from buildbot import config

class MockStateObserver(buildstep.LogLineObserver):
    _line_re = re.compile(r'^.*State Changed: (.*)$')

    def outLineReceived(self, line):
        m = self._line_re.search(line.strip())
        if m:
            state = m.group(1)
            if not state == 'end':
                self.step.descriptionSuffix = ["[%s]"%m.group(1)]
            else:
                self.step.descriptionSuffix = None
            self.step.step_status.setText(self.step.describe(False))

class Mock(ShellCommand):
    """Add the mock logfiles and clean them if they already exist. Add support
    for the root and resultdir parameter of mock."""

    name = "mock"

    haltOnFailure = 1
    flunkOnFailure = 1

    mock_logfiles = ['build.log', 'root.log', 'state.log']

    root = None
    resultdir = None

    def __init__(self,
                 root=None,
                 resultdir=None,
                 **kwargs):
        """
        Creates the Mock object.

        @type root: str
        @param root: the name of the mock buildroot
        @type resultdir: str
        @param resultdir: the path of the result dir
        @type kwargs: dict
        @param kwargs: All further keyword arguments.
        """
        ShellCommand.__init__(self, **kwargs)
        if root:
            self.root = root
        if resultdir:
            self.resultdir = resultdir

        if not self.root:
            config.error("You must specify a mock root")


        self.command = ['mock', '--root', self.root]
        if self.resultdir:
            self.command += ['--resultdir', self.resultdir]

    def start(self):
        """
        Try to remove the old mock logs first.
        """
        if self.resultdir:
            for lname in self.mock_logfiles:
                self.logfiles[lname] = self.build.path_module.join(self.resultdir,
                                                                   lname)
        else:
            for lname in self.mock_logfiles:
                self.logfiles[lname] = lname
        self.addLogObserver('state.log', MockStateObserver())

        cmd = buildstep.RemoteCommand('rmdir', {'dir': 
                map(lambda l: self.build.path_module.join('build', self.logfiles[l]),
                self.mock_logfiles)})
        d = self.runCommand(cmd)
        def removeDone(cmd):
            ShellCommand.start(self)
        d.addCallback(removeDone)
        d.addErrback(self.failed)

class MockBuildSRPM(Mock):
    """Build a srpm within a mock. Requires a spec file and a sources dir."""
    
    name = "mockbuildsrpm"
    
    description = ["mock buildsrpm"]
    descriptionDone = ["mock buildsrpm"]

    spec = None
    sources = '.'

    def __init__(self,
                 spec=None,
                 sources=None,
                 **kwargs):
        """
        Creates the MockBuildSRPM object.
        
        @type spec: str
        @param spec: the path of the specfiles.
        @type sources: str
        @param sources: the path of the sources dir.
        @type kwargs: dict
        @param kwargs: All further keyword arguments.
        """
        Mock.__init__(self, **kwargs)
        if spec:
            self.spec = spec
        if sources:
            self.sources = sources

        if not self.spec:
            config.error("You must specify a spec file")
        if not self.sources:
            config.error("You must specify a sources dir")

        self.command += ['--buildsrpm', '--spec', self.spec,
                         '--sources', self.sources]

    def commandComplete(self, cmd):
        out = cmd.logs['build.log'].getText()
        m = re.search(r"Wrote: .*/([^/]*.src.rpm)", out)
        if m:
            self.setProperty("srpm", m.group(1), 'MockBuildSRPM')

class MockRebuild(Mock):
    """Rebuild a srpm within a mock. Requires a srpm file."""
    
    name = "mock"
    
    description = ["mock rebuilding srpm"]
    descriptionDone = ["mock rebuild srpm"]
    
    srpm = None

    def __init__(self, srpm=None, **kwargs):
        """
        Creates the MockRebuildRPM object.
        
        @type srpm: str
        @param srpm: the path of the srpm file.
        @type kwargs: dict
        @param kwargs: All further keyword arguments.
        """
        Mock.__init__(self, **kwargs)
        if srpm:
            self.srpm = srpm

        if not self.srpm:
            config.error("You must specify a srpm")
        
        self.command += ['--rebuild', self.srpm]