summaryrefslogtreecommitdiff
path: root/icklib/step_build_info.py
blob: ccc3b59ba77cc1aee00c93fdf6e3711af17ad709 (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
# Copyright 2016  Lars Wirzenius
#
# This program 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import time
import urlparse

import icklib


class CreateBuildInfo(icklib.BuildStep):

    def build(self):
        # Only store a build info if we're doing a build.
        if not self.going_to_build(self.run_state):
            return

        build_infos = self.statedir.get_build_infos(self.project.name)

        build_number = self.get_next_build_number(self.run_state)
        self.run_state.project_info.save()  # Don't want to lose this if crash.
        self.run_state.build_info = self.statedir.create_new_build(
            self.project.name, build_number)
        self.run_state.build_info.status = 'RUNNING'

        self.pivot_to_real_build_log_file(self.run_state)

        # Fill in some fields from previous build, if any.
        if build_infos:
            for field in ['commit']:
                if hasattr(build_infos[-1], field):
                    value = getattr(build_infos[-1], field)
                    setattr(self.run_state.build_info, field, value)

        # Copy in new values from run_state.
        for field in ['commits']:
            if hasattr(self.run_state, field):
                value = getattr(self.run_state, field)
                if value is not None:
                    setattr(self.run_state.build_info, field, value)

        self.run_state.build_info.pipeline = self.pipeline.name

        self.run_state.build_info.save()

    def going_to_build(self, run_state):
        names = [
            'build_using_shell',
            'build_using_local_shell',
            'build_using_debian_ci',
            'build_tags_using_debian_release',
        ]
        return any(getattr(run_state, x, False) for x in names)

    def get_next_build_number(self, run_state):
        pi = run_state.project_info
        if pi.next_build_number is None:
            pi.next_build_number = 1
        result = pi.next_build_number
        pi.next_build_number += 1
        return result

    def pivot_to_real_build_log_file(self, run_state):
        filename = run_state.build_info.get_build_log_filename()
        fileurl = urlparse.urljoin('file://', filename)
        run_state.logger.important('Build log at {location}', location=fileurl)
        f = open(filename, 'w')
        f.write(run_state.log_catcher.getvalue())
        run_state.logger.drop_output_file(run_state.log_catcher)
        run_state.logger.add_output_file(f, False)


class FinishBuildInfo(icklib.BuildStep):

    def build(self):
        if self.run_state.build_info is not None:
            self.run_state.build_info.status = 'SUCCESS'
            self.run_state.build_info.duration = (
                time.time() - self.run_state.started)
            self.run_state.build_info.save()