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

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

# 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.web import html 

from twisted.internet import defer, reactor 

from twisted.web.util import Redirect, DeferredResource 

 

import urllib, time 

from twisted.python import log 

from buildbot.status.web.base import HtmlResource, \ 

     css_classes, path_to_build, path_to_builder, path_to_slave, \ 

     getAndCheckProperties, ActionResource, path_to_authzfail, \ 

     getRequestCharset 

from buildbot.schedulers.forcesched import ForceScheduler, TextParameter 

from buildbot.status.web.step import StepsResource 

from buildbot.status.web.tests import TestsResource 

from buildbot import util, interfaces 

 

class ForceBuildActionResource(ActionResource): 

 

    def __init__(self, build_status, builder): 

        self.build_status = build_status 

        self.builder = builder 

        self.action = "forceBuild" 

 

    @defer.inlineCallbacks 

    def performAction(self, req): 

        url = None 

        authz = self.getAuthz(req) 

        res = yield authz.actionAllowed(self.action, req, self.builder) 

 

        if not res: 

            url = path_to_authzfail(req) 

        else: 

            # get a control object 

            c = interfaces.IControl(self.getBuildmaster(req)) 

            bc = c.getBuilder(self.builder.getName()) 

 

            b = self.build_status 

            builder_name = self.builder.getName() 

            log.msg("web rebuild of build %s:%s" % (builder_name, b.getNumber())) 

            name =authz.getUsernameFull(req) 

            comments = req.args.get("comments", ["<no reason specified>"])[0] 

            comments.decode(getRequestCharset(req)) 

            reason = ("The web-page 'rebuild' button was pressed by " 

                      "'%s': %s\n" % (name, comments)) 

            msg = "" 

            extraProperties = getAndCheckProperties(req) 

            if not bc or not b.isFinished() or extraProperties is None: 

                msg = "could not rebuild: " 

                if b.isFinished(): 

                    msg += "build still not finished " 

                if bc: 

                    msg += "could not get builder control" 

            else: 

                tup = yield bc.rebuildBuild(b, reason, extraProperties) 

                # check that (bsid, brids) were properly stored 

                if not (isinstance(tup, tuple) and 

                        isinstance(tup[0], int) and 

                        isinstance(tup[1], dict)): 

                    msg = "rebuilding a build failed "+ str(tup) 

            # we're at 

            # http://localhost:8080/builders/NAME/builds/5/rebuild?[args] 

            # Where should we send them? 

            # 

            # Ideally it would be to the per-build page that they just started, 

            # but we don't know the build number for it yet (besides, it might 

            # have to wait for a current build to finish). The next-most 

            # preferred place is somewhere that the user can see tangible 

            # evidence of their build starting (or to see the reason that it 

            # didn't start). This should be the Builder page. 

 

            url = path_to_builder(req, self.builder), msg 

        defer.returnValue(url) 

 

 

class StopBuildActionResource(ActionResource): 

 

    def __init__(self, build_status): 

        self.build_status = build_status 

        self.action = "stopBuild" 

 

    @defer.inlineCallbacks 

    def performAction(self, req): 

        authz = self.getAuthz(req) 

        res = yield authz.actionAllowed(self.action, req, self.build_status) 

 

        if not res: 

            defer.returnValue(path_to_authzfail(req)) 

            return 

 

        b = self.build_status 

        log.msg("web stopBuild of build %s:%s" % \ 

                    (b.getBuilder().getName(), b.getNumber())) 

        name = authz.getUsernameFull(req) 

        comments = req.args.get("comments", ["<no reason specified>"])[0] 

        comments.decode(getRequestCharset(req)) 

        # html-quote both the username and comments, just to be safe 

        reason = ("The web-page 'stop build' button was pressed by " 

                  "'%s': %s\n" % (html.escape(name), html.escape(comments))) 

 

        c = interfaces.IControl(self.getBuildmaster(req)) 

        bldrc = c.getBuilder(self.build_status.getBuilder().getName()) 

        if bldrc: 

            bldc = bldrc.getBuild(self.build_status.getNumber()) 

            if bldc: 

                bldc.stopBuild(reason) 

 

        defer.returnValue(path_to_builder(req, self.build_status.getBuilder())) 

 

# /builders/$builder/builds/$buildnum 

class StatusResourceBuild(HtmlResource): 

    addSlash = True 

 

    def __init__(self, build_status): 

        HtmlResource.__init__(self) 

        self.build_status = build_status 

 

    def getPageTitle(self, request): 

        return ("Buildbot: %s Build #%d" % 

                (self.build_status.getBuilder().getName(), 

                 self.build_status.getNumber())) 

 

    def content(self, req, cxt): 

        b = self.build_status 

        status = self.getStatus(req) 

        req.setHeader('Cache-Control', 'no-cache') 

 

        cxt['b'] = b 

        cxt['path_to_builder'] = path_to_builder(req, b.getBuilder()) 

 

        if not b.isFinished(): 

            step = b.getCurrentStep() 

            if not step: 

                cxt['current_step'] = "[waiting for Lock]" 

            else: 

                if step.isWaitingForLocks(): 

                    cxt['current_step'] = "%s [waiting for Lock]" % step.getName() 

                else: 

                    cxt['current_step'] = step.getName() 

            when = b.getETA() 

            if when is not None: 

                cxt['when'] = util.formatInterval(when) 

                cxt['when_time'] = time.strftime("%H:%M:%S", 

                                                time.localtime(time.time() + when)) 

 

        else: 

            cxt['result_css'] = css_classes[b.getResults()] 

            if b.getTestResults(): 

                cxt['tests_link'] = req.childLink("tests") 

 

        ssList = b.getSourceStamps() 

        sourcestamps = cxt['sourcestamps'] = ssList 

 

        all_got_revisions = b.getAllGotRevisions() 

        cxt['got_revisions'] = all_got_revisions 

 

        try: 

            cxt['slave_url'] = path_to_slave(req, status.getSlave(b.getSlavename())) 

        except KeyError: 

            pass 

 

        cxt['steps'] = [] 

 

        for s in b.getSteps(): 

            step = {'name': s.getName() } 

 

            if s.isFinished(): 

                if s.isHidden(): 

                    continue 

 

                step['css_class'] = css_classes[s.getResults()[0]] 

                (start, end) = s.getTimes() 

                step['time_to_run'] = util.formatInterval(end - start) 

            elif s.isStarted(): 

                if s.isWaitingForLocks(): 

                    step['css_class'] = "waiting" 

                    step['time_to_run'] = "waiting for locks" 

                else: 

                    step['css_class'] = "running" 

                    step['time_to_run'] = "running" 

            else: 

                step['css_class'] = "not_started" 

                step['time_to_run'] = "" 

 

            cxt['steps'].append(step) 

 

            step['link'] = req.childLink("steps/%s" % 

                                    urllib.quote(s.getName(), safe='')) 

            step['text'] = " ".join(s.getText()) 

            step['urls'] = map(lambda x:dict(url=x[1],logname=x[0]), s.getURLs().items()) 

 

            step['logs']= [] 

            for l in s.getLogs(): 

                logname = l.getName() 

                step['logs'].append({ 'link': req.childLink("steps/%s/logs/%s" % 

                                           (urllib.quote(s.getName(), safe=''), 

                                            urllib.quote(logname, safe=''))), 

                                      'name': logname }) 

 

        scheduler = b.getProperty("scheduler", None) 

        parameters = {} 

        master = self.getBuildmaster(req) 

        for sch in master.allSchedulers(): 

            if isinstance(sch, ForceScheduler) and scheduler == sch.name: 

                for p in sch.all_fields: 

                    parameters[p.name] = p 

 

        ps = cxt['properties'] = [] 

        for name, value, source in b.getProperties().asList(): 

            if not isinstance(value, dict): 

                cxt_value = unicode(value) 

            else: 

                cxt_value = value 

            p = { 'name': name, 'value': cxt_value, 'source': source} 

            if len(cxt_value) > 500: 

                p['short_value'] = cxt_value[:500] 

            if name in parameters: 

                param = parameters[name] 

                if isinstance(param, TextParameter): 

                    p['text'] = param.value_to_text(value) 

                    p['cols'] = param.cols 

                    p['rows'] = param.rows 

                p['label'] = param.label 

            ps.append(p) 

 

 

        cxt['responsible_users'] = list(b.getResponsibleUsers()) 

 

        (start, end) = b.getTimes() 

        cxt['start'] = time.ctime(start) 

        if end: 

            cxt['end'] = time.ctime(end) 

            cxt['elapsed'] = util.formatInterval(end - start) 

        else: 

            now = util.now() 

            cxt['elapsed'] = util.formatInterval(now - start) 

 

        exactly = True 

        has_changes = False 

        for ss in sourcestamps: 

            exactly = exactly and (ss.revision is not None) 

            has_changes = has_changes or ss.changes 

        cxt['exactly'] = (exactly) or b.getChanges() 

        cxt['has_changes'] = has_changes 

        cxt['build_url'] = path_to_build(req, b) 

        cxt['authz'] = self.getAuthz(req) 

 

        template = req.site.buildbot_service.templates.get_template("build.html") 

        return template.render(**cxt) 

 

    def stop(self, req, auth_ok=False): 

        # check if this is allowed 

        if not auth_ok: 

            return StopBuildActionResource(self.build_status) 

 

        b = self.build_status 

        log.msg("web stopBuild of build %s:%s" % \ 

                (b.getBuilder().getName(), b.getNumber())) 

 

        name = self.getAuthz(req).getUsernameFull(req) 

        comments = req.args.get("comments", ["<no reason specified>"])[0] 

        comments.decode(getRequestCharset(req)) 

        # html-quote both the username and comments, just to be safe 

        reason = ("The web-page 'stop build' button was pressed by " 

                  "'%s': %s\n" % (html.escape(name), html.escape(comments))) 

 

        c = interfaces.IControl(self.getBuildmaster(req)) 

        bldrc = c.getBuilder(self.build_status.getBuilder().getName()) 

        if bldrc: 

            bldc = bldrc.getBuild(self.build_status.getNumber()) 

            if bldc: 

                bldc.stopBuild(reason) 

 

        # we're at http://localhost:8080/svn-hello/builds/5/stop?[args] and 

        # we want to go to: http://localhost:8080/svn-hello 

        r = Redirect(path_to_builder(req, self.build_status.getBuilder())) 

        d = defer.Deferred() 

        reactor.callLater(1, d.callback, r) 

        return DeferredResource(d) 

 

    def rebuild(self, req): 

        return ForceBuildActionResource(self.build_status, 

                                        self.build_status.getBuilder()) 

 

    def getChild(self, path, req): 

        if path == "stop": 

            return self.stop(req) 

        if path == "rebuild": 

            return self.rebuild(req) 

        if path == "steps": 

            return StepsResource(self.build_status) 

        if path == "tests": 

            return TestsResource(self.build_status) 

 

        return HtmlResource.getChild(self, path, req) 

 

# /builders/$builder/builds 

class BuildsResource(HtmlResource): 

    addSlash = True 

 

    def __init__(self, builder_status): 

        HtmlResource.__init__(self) 

        self.builder_status = builder_status 

 

    def content(self, req, cxt): 

        return "subpages shows data for each build" 

 

    def getChild(self, path, req): 

        try: 

            num = int(path) 

        except ValueError: 

            num = None 

        if num is not None: 

            build_status = self.builder_status.getBuild(num) 

            if build_status: 

                return StatusResourceBuild(build_status) 

 

        return HtmlResource.getChild(self, path, req)