summaryrefslogtreecommitdiff
path: root/icklib/step_debian_binary.py
blob: b8f5c6ca97d5c1e04d89085d8ead6465b8ce6328 (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
# 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 os

import icklib


class CreateCIDebianBinaryPackages(icklib.BuildStep):

    def build(self):
        if not self.run_state.build_using_debian_ci:
            return

        self.run_state.progress['step'] = 'Creating debs'
        self.run_state.logger.important('Creating debs')
        with self.run_state.logger:
            archs = self._get_architectures_from_source(
                self.run_state.clones[0])

            arch_all_built = set()
            for target in self.targets:
                remote_tmp = target.mkdtemp()
                remote_git_dir = target.sync_to_target(
                    self.run_state.clones[0].dirname, remote_tmp, 'git')
                remote_artifacts_dir = target.sync_to_target(
                    self.run_state.build_info.create_artifacts_directory(),
                    remote_tmp, 'artifacts')

                cleanly_path = self.project.copy_cleanly_to_target(
                    target, remote_tmp)
                cleanly_options = [
                    '--results', remote_artifacts_dir,
                    '--pbuilder-tgz', target.pbuilder_ci_tgz,
                    '--ci',
                    '--buildno', str(self.run_state.build_info.build_number),
                    '--debian-release', target.debian_release,
                    '--debian-codename', target.debian_codename,
                ]

                build_it = True
                if 'all' in archs:
                    if target.debian_codename not in arch_all_built:
                        cleanly_options += ['--build-arch-all-also']
                        arch_all_built.add(target.debian_codename)
                    elif archs == ['all']:
                        build_it = False

                if build_it:
                    self.project.run_cleanly(
                        target, remote_git_dir, cleanly_path,
                        cleanly_options + ['deb'])

                    target.sync_from_target(
                        remote_artifacts_dir,
                        self.run_state.build_info.create_artifacts_directory())

                target.remove_all([remote_tmp])

    def _get_architectures_from_source(self, clone):
        archs = set()
        control = os.path.join(clone.dirname, 'debian', 'control')
        with open(control) as f:
            for line in f:
                if line.startswith('Architecture: '):
                    for arch in line.split()[1:]:
                        archs.add(arch)
        return list(archs)


class CreateDebianBinaryPackagesForRelease(icklib.BuildStep):

    def build(self):
        for tag in self.run_state.build_tags_using_debian_release:
            self.run_state.progress['what'] = (
                'Building debs for release {}'.format(tag))
            self.run_state.logger.important(
                'Building debs for release {tag}',
                tag=tag)
            with self.run_state.logger:
                self.run_state.clones[0].checkout(tag)

                archs = self._get_architectures_from_source(
                    self.run_state.clones[0])

                arch_all_built = set()
                for target in self.targets:
                    remote_tmp = target.mkdtemp()
                    remote_git_dir = target.sync_to_target(
                        self.run_state.clones[0].dirname, remote_tmp, 'git')
                    remote_artifacts_dir = target.sync_to_target(
                        self.run_state.build_info.create_artifacts_directory(),
                        remote_tmp, 'artifacts')

                    cleanly_path = self.project.copy_cleanly_to_target(
                        target, remote_tmp)
                    cleanly_options = [
                        '--results', remote_artifacts_dir,
                        '--pbuilder-tgz', target.pbuilder_ci_tgz,
                        '--release',
                        '--buildno',
                        str(self.run_state.build_info.build_number),
                        '--debian-release', target.debian_release,
                        '--debian-codename', target.debian_codename,
                    ]

                    build_it = True
                    if 'all' in archs:
                        if target.debian_codename not in arch_all_built:
                            cleanly_options += ['--build-arch-all-also']
                            arch_all_built.add(target.debian_codename)
                        elif archs == ['all']:
                            build_it = False

                    if build_it:
                        self.project.run_cleanly(
                            target, remote_git_dir, cleanly_path,
                            cleanly_options + ['deb'])

                        build_info = self.run_state.build_info
                        target.sync_from_target(
                            remote_artifacts_dir,
                            build_info.create_artifacts_directory())

                    target.remove_all([remote_tmp])

    def _get_architectures_from_source(self, clone):
        archs = set()
        control = os.path.join(clone.dirname, 'debian', 'control')
        with open(control) as f:
            for line in f:
                if line.startswith('Architecture: '):
                    for arch in line.split()[1:]:
                        archs.add(arch)
        return list(archs)