File: run-tests

package info (click to toggle)
adequate 0.12.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 332 kB
  • ctags: 55
  • sloc: perl: 1,114; python: 350; makefile: 139; sh: 89; ansic: 21
file content (242 lines) | stat: -rwxr-xr-x 8,352 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3

# Copyright © 2013, 2014 Jakub Wilk <jwilk@debian.org>
#
# 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 argparse
import collections
import difflib
import errno
import functools
import os
import re
import subprocess as ipc
import sys

import apt_pkg

@functools.total_ordering
class debversion(str):
    def __lt__(self, other):
        return apt_pkg.version_compare(self, other) < 0
    def __eq__(self, other):
        return apt_pkg.version_compare(self, other) == 0

def log_start(s):
    print(s, end=' ... ')
    sys.stdout.flush()

def log_end(s):
    print(s)

def log_stderr(s):
    for line in s.splitlines():
        print('!', line)

class DpkgError(Exception):
    pass

class Uninstallable(DpkgError):
    pass

class DebPkg(object):

    def __init__(self, path):
        self.depends = self.pre_depends = ''
        self.conflicts = self.breaks = ''
        cmdline = [
            'dpkg-deb',
            '-f', path,
        ]
        output = ipc.check_output(cmdline)
        output = output.decode('ASCII').splitlines()
        attr = None
        def a_get():
            return getattr(self, attr, '')
        def a_set(value):
            return setattr(self, attr, value)
        def a_append(value):
            return setattr(self, attr, a_get() + '\n' + line)
        for line in output:
            if line[0] == ' ':
                a_append(line)
            else:
                key, value = line.split(': ', 1)
                attr = key.lower().replace('-', '_')
                a_set(value)
        self.path = path
        self.name = os.path.basename(path).split('_', 1)[0]

    def check_installability(self):
        depends = '{0.pre_depends}, {0.depends}'.format(self)
        conflicts = '{0.conflicts}, {0.breaks}'.format(self)
        cmdline = [
            'dpkg-checkbuilddeps',
            '-d', depends,
            '-c', conflicts,
            os.devnull
        ]
        child = ipc.Popen(cmdline, stdout=ipc.PIPE, stderr=ipc.PIPE)
        stdout, stderr = child.communicate()
        if child.wait() != 0:
            error = stderr.decode('ASCII', 'replace').strip()
            error = re.sub('^dpkg-checkbuilddeps: ', '', error)
            error = re.sub('^Unmet build dependencies', 'unmet dependencies', error)
            error = re.sub('^Build conflicts', 'conflicts', error)
            raise Uninstallable(error)

    def install(self):
        cmdline = ['dpkg', '-i', self.path]
        child = ipc.Popen(cmdline, stdout=ipc.PIPE, stderr=ipc.PIPE)
        stdout, stderr = child.communicate()
        if child.wait() != 0:
            stderr = stderr.decode('ASCII', 'replace')
            raise DpkgError(stderr)

    def purge(self):
        cmdline = ['dpkg', '-P', self.name]
        child = ipc.Popen(cmdline, stdout=ipc.PIPE, stderr=ipc.PIPE)
        stdout, stderr = child.communicate()
        if child.wait() != 0:
            stderr = stderr.decode('ASCII', 'replace')
            raise DpkgError(stderr)

    def get_version(self):
        return self.version

def format_os_error(exc):
    try:
        prefix = errno.errorcode[exc.args[0]]
    except KeyError:
        prefix = 'errno={}'.format(exc.args[0])
    return '{}: {}'.format(prefix, exc.args[1])

def main():
    apt_pkg.init()
    ap = argparse.ArgumentParser()
    ap.add_argument('paths', metavar='DEB-OR-CHANGES', nargs='+')
    ap.add_argument('--no-skip', action='store_true')
    ap.add_argument('--adequate', default='adequate')
    options = ap.parse_args()
    if os.getuid() != 0:
        print('{}: error: administrator privileges are required'.format(ap.prog), file=sys.stderr)
        sys.exit(1)
    packages = collections.defaultdict(list)
    for path in options.paths:
        if path.endswith('.changes'):
            with open(path, 'rt', encoding='UTF-8') as file:
                for para in apt_pkg.TagFile(file):
                    for line in para['Files'].splitlines():
                        md5, size, section, priority, path = line.split()
                        debpkg = DebPkg(path)
                        packages[debpkg.name] += [debpkg]
        elif path.endswith('.deb'):
            debpkg = DebPkg(path)
            packages[debpkg.name] += [debpkg]
    rc = 0
    for name, debpkg_group in sorted(packages.items()):
        debpkg_group.sort(key=DebPkg.get_version)
        prev_version = None
        for debpkg in debpkg_group:
            if prev_version is None:
                if len(debpkg_group) == 1:
                    log_start('install {}'.format(name))
                else:
                    log_start('install {} ({})'.format(name, debpkg.version))
            else:
                log_start('upgrade {} ({} => {})'.format(name, prev_version, debpkg.version))
            try:
                debpkg.check_installability()
            except Uninstallable as exc:
                if options.no_skip:
                    action = 'ERROR'
                    rc = 1
                else:
                    action = 'SKIP'
                log_end('{} ({})'.format(action, exc))
                break
            try:
                debpkg.install()
            except DpkgError as exc:
                log_end('ERROR (dpkg failed)')
                log_stderr(str(exc))
                rc = 1
                break
            else:
                log_end('ok')
            log_start('check {}'.format(name))
            error = None
            try:
                child = ipc.Popen(
                    [options.adequate, name],
                    stdout=ipc.PIPE,
                    stderr=ipc.PIPE,
                )
            except OSError as exc:
                error = format_os_error(exc)
                stderr = b''
            else:
                stdout, stderr = child.communicate()
                if child.wait() != 0:
                    error = 'non-zero exit code'
                elif stderr:
                    error = 'non-empty stderr'
            if error is not None:
                log_end('ERROR ({})'.format(error))
                stderr = stderr.decode('ASCII', 'replace')
                log_stderr(stderr)
                rc = 1
                break
            output = stdout.decode('ASCII').splitlines()
            output = sorted(output)  # tag order is not deterministic
            if debpkg is debpkg_group[-1]:
                expected = [
                    '{}: {}'.format(name, line.strip())
                    for line in debpkg.description.splitlines(False)[1:]
                    if line
                ]
            else:
                expected = []
            if output == expected:
                log_end('ok')
            else:
                log_end('FAIL')
                diff = list(
                    difflib.unified_diff(expected, output, n=9999)
                )
                for line in diff[3:]:
                    print(line)
                rc = 1
            prev_version = debpkg.version
        log_start('remove {}'.format(name))
        try:
            debpkg.purge()
        except DpkgError as exc:
            log_end('ERROR (dpkg failed)')
            log_stderr(str(exc))
            rc = 1
        else:
            log_end('ok')
    sys.exit(rc)

if __name__ == '__main__':
    main()

# vim:ts=4 sw=4 et