source_package.py 7.11 KB
Newer Older
1
2
3
4
5
#   source_package.py — Build a source package for testing
#
#   This file is part of debexpo
#   https://salsa.debian.org/mentors.debian.net-team/debexpo
#
6
#   Copyright © 2019-2020 Baptiste Beauplat <lyknode@cilg.org>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

import logging
30
from debian.debian_support import BaseVersion
31
32
33
34
35

from os import environ, remove
from os.path import isdir, join, dirname
from shutil import rmtree, copytree
from subprocess import Popen, PIPE, STDOUT
36
37
from tempfile import mkdtemp
from debexpo.tools.debian.changes import Changes
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

log = logging.getLogger(__name__)


class TestSourcePackageException(Exception):
    pass


class TestSourcePackage():
    """
    Build source packages

    From a source package:
        - creates orig tarball
        - build package
        - generate .changes
        - sign files with testing key
    """

    _GPG_KEY = """-----BEGIN PGP PRIVATE KEY BLOCK-----

lFgEW/GBqhYJKwYBBAHaRw8BAQdA+6hBA4PcdcPwgMsKGQXrqwbJemLBgS1PkKZg
RFlKdKgAAQD2uvclOFTAon1t+Auuy2cW3uc3Qf6l9ZYYskx3xYMwrBG6tCBwcmlt
YXJ5IGlkIDxwcmltYXJ5QGV4YW1wbGUub3JnPoiTBBMWCAA7AhsDBQsJCAcCBhUK
CQgLAgQWAgMBAh4BAheAFiEEVZMG7uHIwbLdHHOxxDR4HOhx898FAlvxgewCGQEA
CgkQxDR4HOhx89+RDgD/ZVwW/JZu9sT3jHY0S/k1PNz4pg9DWbq0H55y1WTruLQB
AKna8/9quNZknJyiRQJ0OaWp+RWJVJw0dlladZIhaD8LtB1UZXN0IHVzZXIgPGVt
YWlsQGV4YW1wbGUuY29tPoiQBBMWCAA4FiEEVZMG7uHIwbLdHHOxxDR4HOhx898F
AlvxgegCGwMFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQxDR4HOhx898q+gD/
TMjGs/nbDgt0f1p4xPBHTONpqX91ZCmNInEutCvwI90A/15CAKrPiLUAFKHqmTN4
4dB7y8FBEsZuhbQpbcMhpasFnF0EW/GBqhIKKwYBBAGXVQEFAQEHQDa5N6qv3j9U
xFnxWaoUdL3M+2AfXOfjp7zFfsTuFt50AwEIBwAA/3JjB8KfQJ5PCuMtztVeIKdP
9BTK+pcHY6BKw5vy6DvgEaSIeAQYFggAIBYhBFWTBu7hyMGy3RxzscQ0eBzocfPf
BQJb8YGqAhsMAAoJEMQ0eBzocfPf8IcA/RyHF6zgRu2Ds3wH8GgxjCZRW+YxWahX
55/++pi9+bqyAQDp8rMxJeWXDntQn3RSfKfE7AnWF4Sd+aA+S6LPuMUICg==
=zFbo
-----END PGP PRIVATE KEY BLOCK-----"""

76
77
78
79
80
81
    def __init__(self, source_dir, data_dir=None):
        if data_dir:
            self.data_dir = data_dir
        else:
            self.data_dir = join(dirname(__file__), 'sources')

82
        self.source_dir = join(self.data_dir, source_dir)
83
84
        self.package = self._parse_changelog('Source').decode()
        self.version = self._parse_changelog('Version').decode()
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
        self.workdir = mkdtemp(prefix='debexpo-source-package')
        self.gpgdir = mkdtemp(prefix='debexpo-source-package-gpg')
        self._import_testing_key()

    def __del__(self):
        for dirs in (self.workdir, self.gpgdir):
            if isdir(dirs):
                rmtree(dirs)

    def _run_command(self, command, args, workdir, env=environ):
        proc = Popen([command] + args,
                     stdout=PIPE,
                     stderr=STDOUT,
                     cwd=join(workdir),
                     env=env)
        (output, _) = proc.communicate()

        if proc.returncode != 0:
            raise TestSourcePackageException('command failed:\n'
                                             'command: {}\n'
                                             'args: {}\n'
                                             'workdir: {}\n\n'
                                             'output: {}'.format(command, args,
                                                                 workdir,
                                                                 output))

        return output.strip()

    def _parse_changelog(self, field):
        args = ['-S', field]
        command = 'dpkg-parsechangelog'

        return self._run_command(command, args, self.source_dir)

    def _get_env_with_gpg(self):
        env = environ.copy()
        env['GNUPGHOME'] = self.gpgdir

        return env

    def _import_testing_key(self):
126
127
128
        args = ['--import',
                join(dirname(__file__), '..', '..', 'keyring', 'secret.gpg')]
        command = '/usr/bin/gpg'
129

130
131
        self._run_command(command, args, self.source_dir,
                          self._get_env_with_gpg())
132

133
    def _build_package(self, sign):
134
135
        args = ['--build=source',
                '--no-check-builddeps',
136
                '--sign-key=559306EEE1C8C1B2DD1C73B1C434781CE871F3DF']
137
138
        command = 'dpkg-buildpackage'

139
140
141
142
143
        if sign:
            args.append('--force-sign')
        else:
            args.append('--no-sign')

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        log.debug('Build source package {}-{}'.format(self.package,
                                                      self.version))
        self._run_command(command, args, join(self.workdir, 'sources'),
                          self._get_env_with_gpg())

    def _gen_orig(self):
        tarball = self._get_orig_filename()
        args = ['--create',
                '--exclude=./debian',
                '--xz',
                '--file',
                tarball,
                '--directory=' + join(self.workdir, 'sources'),
                '.']
        command = 'tar'

        log.debug('Gen orig tarball {}'.format(join(self.workdir, tarball)))
        self._run_command(command, args, self.workdir)

    def _get_orig_filename(self):
164
        version = BaseVersion(self.version)
165

166
167
        return join(self.workdir,
                    f'{self.package}_{version.upstream_version}.orig.tar.xz')
168
169
170
171
172
173
174
175
176

    # Remove orig tarball when not referenced by .changes as it will not be
    # uploaded (copied) to the incoming spool.
    def _cleanup_orig(self):
        changes_filename = join(self.workdir, self.package + '_' + self.version
                                + '_source.changes')
        changes = Changes(changes_filename)

        found = False
177
178
        for referenced in changes.files.files:
            if '.orig.tar.xz' in str(referenced):
179
180
181
182
183
184
185
186
                found = True

        if not found:
            remove(self._get_orig_filename())

    def get_package_dir(self):
        return self.workdir

187
    def build(self, sign=True):
188
189
190
191
192
193
        # Copy sources files into workdir
        copytree(self.source_dir,
                 join(self.workdir, 'sources'))

        # Gen orig, build and sign
        self._gen_orig()
194
        self._build_package(sign)
195
196
197
198
199

        # Remove temporary source dir
        if isdir(join(self.workdir, 'sources')):
            rmtree(join(self.workdir, 'sources'))
        self._cleanup_orig()