| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# Copyright © 2013, 2014 Jakub Wilk <[email protected]> |
| 4 |
# |
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
# of this software and associated documentation files (the “Software”), to deal |
| 7 |
# in the Software without restriction, including without limitation the rights |
| 8 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
# copies of the Software, and to permit persons to whom the Software is |
| 10 |
# furnished to do so, subject to the following conditions: |
| 11 |
# |
| 12 |
# The above copyright notice and this permission notice shall be included in |
| 13 |
# all copies or substantial portions of the Software. |
| 14 |
# |
| 15 |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
# SOFTWARE. |
| 22 |
|
| 23 |
import argparse |
| 24 |
import collections |
| 25 |
import difflib |
| 26 |
import errno |
| 27 |
import functools |
| 28 |
import os |
| 29 |
import re |
| 30 |
import subprocess as ipc |
| 31 |
import sys |
| 32 |
|
| 33 |
import apt_pkg |
| 34 |
|
| 35 |
@functools.total_ordering |
| 36 |
class debversion(str): |
| 37 |
def __lt__(self, other): |
| 38 |
return apt_pkg.version_compare(self, other) < 0 |
| 39 |
def __eq__(self, other): |
| 40 |
return apt_pkg.version_compare(self, other) == 0 |
| 41 |
|
| 42 |
def log_start(s): |
| 43 |
print(s, end=' ... ') |
| 44 |
sys.stdout.flush() |
| 45 |
|
| 46 |
def log_end(s): |
| 47 |
print(s) |
| 48 |
|
| 49 |
def log_stderr(s): |
| 50 |
for line in s.splitlines(): |
| 51 |
print('!', line) |
| 52 |
|
| 53 |
class DpkgError(Exception): |
| 54 |
pass |
| 55 |
|
| 56 |
class Uninstallable(DpkgError): |
| 57 |
pass |
| 58 |
|
| 59 |
class DebPkg(object): |
| 60 |
|
| 61 |
def __init__(self, path): |
| 62 |
self.depends = self.pre_depends = '' |
| 63 |
self.conflicts = self.breaks = '' |
| 64 |
cmdline = [ |
| 65 |
'dpkg-deb', |
| 66 |
'-f', path, |
| 67 |
] |
| 68 |
output = ipc.check_output(cmdline) |
| 69 |
output = output.decode('ASCII').splitlines() |
| 70 |
attr = None |
| 71 |
def a_get(): |
| 72 |
return getattr(self, attr, '') |
| 73 |
def a_set(value): |
| 74 |
return setattr(self, attr, value) |
| 75 |
def a_append(value): |
| 76 |
return setattr(self, attr, a_get() + '\n' + line) |
| 77 |
for line in output: |
| 78 |
if line[0] == ' ': |
| 79 |
a_append(line) |
| 80 |
else: |
| 81 |
key, value = line.split(': ', 1) |
| 82 |
attr = key.lower().replace('-', '_') |
| 83 |
a_set(value) |
| 84 |
self.path = path |
| 85 |
self.name = os.path.basename(path).split('_', 1)[0] |
| 86 |
|
| 87 |
def check_installability(self): |
| 88 |
depends = '{0.pre_depends}, {0.depends}'.format(self) |
| 89 |
conflicts = '{0.conflicts}, {0.breaks}'.format(self) |
| 90 |
cmdline = [ |
| 91 |
'dpkg-checkbuilddeps', |
| 92 |
'-d', depends, |
| 93 |
'-c', conflicts, |
| 94 |
os.devnull |
| 95 |
] |
| 96 |
child = ipc.Popen(cmdline, stdout=ipc.PIPE, stderr=ipc.PIPE) |
| 97 |
stdout, stderr = child.communicate() |
| 98 |
if child.wait() != 0: |
| 99 |
error = stderr.decode('ASCII', 'replace').strip() |
| 100 |
error = re.sub('^dpkg-checkbuilddeps: ', '', error) |
| 101 |
error = re.sub('^Unmet build dependencies', 'unmet dependencies', error) |
| 102 |
error = re.sub('^Build conflicts', 'conflicts', error) |
| 103 |
raise Uninstallable(error) |
| 104 |
|
| 105 |
def install(self): |
| 106 |
cmdline = ['dpkg', '-i', self.path] |
| 107 |
child = ipc.Popen(cmdline, stdout=ipc.PIPE, stderr=ipc.PIPE) |
| 108 |
stdout, stderr = child.communicate() |
| 109 |
if child.wait() != 0: |
| 110 |
stderr = stderr.decode('ASCII', 'replace') |
| 111 |
raise DpkgError(stderr) |
| 112 |
|
| 113 |
def purge(self): |
| 114 |
cmdline = ['dpkg', '-P', self.name] |
| 115 |
child = ipc.Popen(cmdline, stdout=ipc.PIPE, stderr=ipc.PIPE) |
| 116 |
stdout, stderr = child.communicate() |
| 117 |
if child.wait() != 0: |
| 118 |
stderr = stderr.decode('ASCII', 'replace') |
| 119 |
raise DpkgError(stderr) |
| 120 |
|
| 121 |
def get_version(self): |
| 122 |
return self.version |
| 123 |
|
| 124 |
def format_os_error(exc): |
| 125 |
try: |
| 126 |
prefix = errno.errorcode[exc.args[0]] |
| 127 |
except KeyError: |
| 128 |
prefix = 'errno={}'.format(exc.args[0]) |
| 129 |
return '{}: {}'.format(prefix, exc.args[1]) |
| 130 |
|
| 131 |
def main(): |
| 132 |
apt_pkg.init() |
| 133 |
ap = argparse.ArgumentParser() |
| 134 |
ap.add_argument('paths', metavar='DEB-OR-CHANGES', nargs='+') |
| 135 |
ap.add_argument('--no-skip', action='store_true') |
| 136 |
ap.add_argument('--adequate', default='adequate') |
| 137 |
options = ap.parse_args() |
| 138 |
if os.getuid() != 0: |
| 139 |
print('{}: error: administrator privileges are required'.format(ap.prog), file=sys.stderr) |
| 140 |
sys.exit(1) |
| 141 |
packages = collections.defaultdict(list) |
| 142 |
for path in options.paths: |
| 143 |
if path.endswith('.changes'): |
| 144 |
with open(path, 'rt', encoding='UTF-8') as file: |
| 145 |
for para in apt_pkg.TagFile(file): |
| 146 |
for line in para['Files'].splitlines(): |
| 147 |
md5, size, section, priority, path = line.split() |
| 148 |
if path.endswith('.deb'): |
| 149 |
debpkg = DebPkg(path) |
| 150 |
packages[debpkg.name] += [debpkg] |
| 151 |
elif path.endswith('.deb'): |
| 152 |
debpkg = DebPkg(path) |
| 153 |
packages[debpkg.name] += [debpkg] |
| 154 |
rc = 0 |
| 155 |
for name, debpkg_group in sorted(packages.items()): |
| 156 |
debpkg_group.sort(key=DebPkg.get_version) |
| 157 |
prev_version = None |
| 158 |
for debpkg in debpkg_group: |
| 159 |
if prev_version is None: |
| 160 |
if len(debpkg_group) == 1: |
| 161 |
log_start('install {}'.format(name)) |
| 162 |
else: |
| 163 |
log_start('install {} ({})'.format(name, debpkg.version)) |
| 164 |
else: |
| 165 |
log_start('upgrade {} ({} => {})'.format(name, prev_version, debpkg.version)) |
| 166 |
try: |
| 167 |
debpkg.check_installability() |
| 168 |
except Uninstallable as exc: |
| 169 |
if options.no_skip: |
| 170 |
action = 'ERROR' |
| 171 |
rc = 1 |
| 172 |
else: |
| 173 |
action = 'SKIP' |
| 174 |
log_end('{} ({})'.format(action, exc)) |
| 175 |
break |
| 176 |
try: |
| 177 |
debpkg.install() |
| 178 |
except DpkgError as exc: |
| 179 |
log_end('ERROR (dpkg failed)') |
| 180 |
log_stderr(str(exc)) |
| 181 |
rc = 1 |
| 182 |
break |
| 183 |
else: |
| 184 |
log_end('ok') |
| 185 |
log_start('check {}'.format(name)) |
| 186 |
error = None |
| 187 |
try: |
| 188 |
child = ipc.Popen( |
| 189 |
[options.adequate, name], |
| 190 |
stdout=ipc.PIPE, |
| 191 |
stderr=ipc.PIPE, |
| 192 |
) |
| 193 |
except OSError as exc: |
| 194 |
error = format_os_error(exc) |
| 195 |
stderr = b'' |
| 196 |
else: |
| 197 |
stdout, stderr = child.communicate() |
| 198 |
if child.wait() != 0: |
| 199 |
error = 'non-zero exit code' |
| 200 |
elif stderr: |
| 201 |
error = 'non-empty stderr' |
| 202 |
if error is not None: |
| 203 |
log_end('ERROR ({})'.format(error)) |
| 204 |
stderr = stderr.decode('ASCII', 'replace') |
| 205 |
log_stderr(stderr) |
| 206 |
rc = 1 |
| 207 |
break |
| 208 |
output = stdout.decode('ASCII').splitlines() |
| 209 |
output = sorted(output) # tag order is not deterministic |
| 210 |
if debpkg is debpkg_group[-1]: |
| 211 |
expected = [ |
| 212 |
'{}: {}'.format(name, line.strip()) |
| 213 |
for line in debpkg.description.splitlines(False)[1:] |
| 214 |
if line |
| 215 |
] |
| 216 |
else: |
| 217 |
expected = [] |
| 218 |
if output == expected: |
| 219 |
log_end('ok') |
| 220 |
else: |
| 221 |
log_end('FAIL') |
| 222 |
diff = list( |
| 223 |
difflib.unified_diff(expected, output, n=9999) |
| 224 |
) |
| 225 |
for line in diff[3:]: |
| 226 |
print(line) |
| 227 |
rc = 1 |
| 228 |
prev_version = debpkg.version |
| 229 |
log_start('remove {}'.format(name)) |
| 230 |
try: |
| 231 |
debpkg.purge() |
| 232 |
except DpkgError as exc: |
| 233 |
log_end('ERROR (dpkg failed)') |
| 234 |
log_stderr(str(exc)) |
| 235 |
rc = 1 |
| 236 |
else: |
| 237 |
log_end('ok') |
| 238 |
sys.exit(rc) |
| 239 |
|
| 240 |
if __name__ == '__main__': |
| 241 |
main() |
| 242 |
|
| 243 |
# vim:ts=4 sts=4 sw=4 et |