summaryrefslogtreecommitdiff
path: root/icklib/step_debian_publish.py
blob: 94d4934d17aafb77e03772ff5720606763830560 (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
# 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 glob
import os
import shutil
import tempfile

import icklib


class PublishDebianPackages(icklib.BuildStep):

    def build(self):
        have_changes = self.run_state.changes_files
        have_targets = self.project.dput_targets
        have_unstable = self.project.dput_unstable
        need_to_run = have_changes and (have_targets or have_unstable)
        if not need_to_run:
            return

        self.run_state.progress['step'] = (
            'Publish built release packages to public APT repositories')
        self.run_state.logger.important(
            'Publish built release packages to public APT repositories')
        with self.run_state.logger:
            tempdir = self._copy_packages()
            self._remove_dbgsym_packages_from_changes_files(tempdir)
            self._debsign(tempdir)
            self._dput_targets(tempdir)
            self._dput_unstable(tempdir)
            shutil.rmtree(tempdir)

    def _copy_packages(self):
        tempdir = tempfile.mkdtemp()
        artifacts_dir = self.run_state.build_info.get_artifacts_directory()
        self.project.run_locally(
            ['rsync', '-a', artifacts_dir + '/.', tempdir + '/.'],
            'rsync artifacts', '.')
        return tempdir

    def _remove_dbgsym_packages_from_changes_files(self, tempdir):
        for filename in self._changes_files(tempdir):
            with open(filename) as f:
                text = f.read()
            modified = ''.join(
                '{}\n'.format(line)
                for line in text.splitlines()
                if '-dbgsym' not in line)
            with open(filename, 'w') as f:
                f.write(modified)

    def _changes_files(self, tempdir):
        return glob.glob(os.path.join(tempdir, '*.changes'))

    def _debsign(self, tempdir):
        argv = ['debsign']
        if self.project.package_signing_key is not None:
            argv += ['-k', self.project.package_signing_key]
        argv += self._changes_files(tempdir)
        self.project.run_locally(argv, 'debsign *.changes', tempdir)

    def _dput_targets(self, tempdir):
        files = self._changes_files(tempdir)
        self._dput_some_targets(tempdir, files, self.project.dput_targets)

    def _dput_unstable(self, tempdir):
        files = self._changes_files(tempdir)
        unstable = [f for f in files if self._is_source_for_unstable(f)]
        self._dput_some_targets(tempdir, unstable, self.project.dput_unstable)

    def _is_source_for_unstable(self, filename):
        with open(filename) as f:
            text = f.read()
        is_source = '\nArchitecture: source\n' in text
        is_for_unstable = '\nDistribution: unstable\n' in text
        return is_source and is_for_unstable

    def _dput_some_targets(self, tempdir, files, dput_targets):
        for dput_target in dput_targets:
            self.project.run_locally(
                ['dput', dput_target] + files,
                'dput {} *.changes'.format(dput_target),
                tempdir)