From: kaliko Date: Sat, 2 May 2020 14:09:24 +0000 (+0200) Subject: Add Debian archive plugin X-Git-Tag: 0.1.0~24 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=1dc1e55c0be9da954d4399cac31bd924bed449da;p=sid.git Add Debian archive plugin --- diff --git a/sid/archive.py b/sid/archive.py new file mode 100644 index 0000000..ea286af --- /dev/null +++ b/sid/archive.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2020 kaliko + +# 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, version 3 only. + +# 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 . + + +from re import compile as re_compile + +from .plugin import Plugin, botcmd +from .lib import get_pkg + + +class Archive(Plugin): + """Fetch package info from the archive + """ + stable_codename = 'buster' + re_pkg = re_compile(r'(?P[0-9a-z.+-]+)$') + + def __init__(self, bot): + Plugin.__init__(self, bot) + bot.add_event_handler("muc::%s::message" % + self.bot.room, self.muc_message) + + def muc_message(self, msg): + """Handler method dealing with MUC incoming messages""" + # Does not reply to myself + if msg['mucnick'] == self.bot.nick: + return + if '#' not in msg['body']: + return + bugs = list() + for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())): + self.log.debug('got bug id: %s', bug_id) + query = debianbts.get_status(bug_id) + if len(query) == 1: + bug = query[0] + url = debianbts.BTS_URL + bug_id + bugs.append({'id': bug_id, + 'package': bug.package, + 'summary': bug.subject, + 'url': url}) + else: + self.log.warning('Wrong bug number "%s"?', bug_id) + bugs.append({'id': bug_id}) + for bug in bugs: + if len(bug) == 1: + message = 'Invalid bug id: {id}'.format(**bug) + self.reply(msg, message) + else: + message = {'mhtml': '#%(id)s: %(package)s “ %(summary)s ”' % bug, + 'mbody': '#%(id)s: %(package)s “ %(summary)s ” %(url)s' % bug} + self.reply(msg, message) + + @botcmd + def archive(self, rcv, args): + """Fetch pkg info from the archive: + + !archive pkg-name : Returns package versions (by suite) + """ + if not args: + return + if len(args) > 1: + self.log.info('more than one packages provided') + pkg = Archive.re_pkg.match(args[0]) + if not pkg: + msg = 'Wrong package name format re: "{}"'.format( + Archive.re_pkg.pattern) + self.reply(rcv, msg) + return + pkg_name = pkg.groupdict('package').get('package') + info = get_pkg(pkg_name) + if not info: + self.reply(rcv, 'pakage not found') + return + messages = [] + suites_av = set(info.keys() & {'stable', 'testing', 'unstable', + f'{Archive.stable_codename}-backports'}) + for suite in sorted(suites_av): + component = '/'.join({v['component'] for v in info[suite].values()}) + versions = '/'.join(info[suite].keys()) + messages.append(f'{suite:16}: {versions} {component}') + msg = '\n'.join(messages) + self.reply(rcv, msg) diff --git a/sid/lib.py b/sid/lib.py new file mode 100644 index 0000000..eeb99ff --- /dev/null +++ b/sid/lib.py @@ -0,0 +1,48 @@ +# coding: utf-8 + +import json +import logging + +from urllib.error import URLError, HTTPError +from urllib.request import Request, urlopen +from urllib.parse import urlencode + + +def get_pkg(pkg): + """ + Uses the DAK HTTP api + https://ftp-team.pages.debian.net/dak/epydoc/dakweb.queries.madison-module.html + """ + logger = logging.getLogger(__package__) + data = {} + data['package'] = pkg + data['f'] = '' + values = urlencode(data) + url = f'https://api.ftp-master.debian.org/madison?{values}' + logger.debug(url) + req = Request(url, method='GET') + try: + response = urlopen(req) + ans = response.read() + #print(response.getcode()) + except HTTPError as err: + logger.info('The server couldn\'t fulfill the request.') + logger.debug('Error code: %s', err.code) + except URLError as err: + logger.info('We failed to reach a server.') + logger.debug('Reason: %s', err.reason) + try: + pkg_info = json.loads(ans) + except Exception as err: + logger.error('Failed to decode json') + if not pkg_info: + return [] + return pkg_info[0][pkg] + + +# Script starts here +if __name__ == '__main__': + pass + +# VIM MODLINE +# vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8