Blame doc/rhbug.py

Packit 3a9065
# rhbug.py
Packit 3a9065
# rhbug Sphinx extension.
Packit 3a9065
#
Packit 3a9065
# Copyright (C) 2012-2014  Red Hat, Inc.
Packit 3a9065
#
Packit 3a9065
# This copyrighted material is made available to anyone wishing to use,
Packit 3a9065
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 3a9065
# the GNU General Public License v.2, or (at your option) any later version.
Packit 3a9065
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 3a9065
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 3a9065
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 3a9065
# Public License for more details.  You should have received a copy of the
Packit 3a9065
# GNU General Public License along with this program; if not, write to the
Packit 3a9065
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 3a9065
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 3a9065
# source code or documentation are not subject to the GNU General Public
Packit 3a9065
# License and may only be used or replicated with the express permission of
Packit 3a9065
# Red Hat, Inc.
Packit 3a9065
#
Packit 3a9065
Packit 3a9065
# the original version of this file is at
Packit 3a9065
# https://raw.githubusercontent.com/rpm-software-management/dnf/master/doc/rhbug.py
Packit 3a9065
Packit 3a9065
from docutils import nodes
Packit 3a9065
Packit 3a9065
try:
Packit 3a9065
    import bugzilla
Packit 3a9065
except ImportError:
Packit 3a9065
    bugzilla = None
Packit 3a9065
import json
Packit 3a9065
import os
Packit 3a9065
Packit 3a9065
class Summary(object):
Packit 3a9065
    def __init__(self, cache_fn):
Packit 3a9065
        self.cache_fn = cache_fn
Packit 3a9065
Packit 3a9065
    def __call__(self, bug_id):
Packit 3a9065
        bug_id = int(bug_id)
Packit 3a9065
        summary = self._from_cache(bug_id)
Packit 3a9065
        if summary is not None:
Packit 3a9065
            return summary
Packit 3a9065
        summary = self._from_bugzilla(bug_id)
Packit 3a9065
        self._store_in_cache(bug_id, summary)
Packit 3a9065
        return summary
Packit 3a9065
Packit 3a9065
    def _from_bugzilla(self, bug_id):
Packit 3a9065
        if bugzilla is None:
Packit 3a9065
            return ''
Packit 3a9065
        rhbz = bugzilla.RHBugzilla(url="https://bugzilla.redhat.com/xmlrpc.cgi")
Packit 3a9065
        query = rhbz.build_query(bug_id=bug_id)
Packit 3a9065
        bug = rhbz.query(query)[0]
Packit 3a9065
        return bug.summary
Packit 3a9065
Packit 3a9065
    def _from_cache(self, bug_id):
Packit 3a9065
        try:
Packit 3a9065
            with open(self.cache_fn, 'r') as json_file:
Packit 3a9065
                cache = json.load(json_file)
Packit 3a9065
                summary = [entry[1] for entry in cache if entry[0] == bug_id]
Packit 3a9065
                return summary[0]
Packit 3a9065
        except (IOError, IndexError):
Packit 3a9065
            return None
Packit 3a9065
Packit 3a9065
    def _store_in_cache(self, bug_id, summary):
Packit 3a9065
        if bugzilla is None:
Packit 3a9065
            return
Packit 3a9065
        try:
Packit 3a9065
            with open(self.cache_fn, 'r') as json_file:
Packit 3a9065
                cache = json.load(json_file)
Packit 3a9065
        except IOError:
Packit 3a9065
            cache = []
Packit 3a9065
        cache.append((bug_id, summary))
Packit 3a9065
        with open(self.cache_fn, 'w') as json_file:
Packit 3a9065
            json.dump(cache, json_file, indent=4)
Packit 3a9065
Packit 3a9065
def RhBug_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
Packit 3a9065
    source = inliner.document.settings._source
Packit 3a9065
    summaries_fn = '%s/summaries_cache' % os.path.dirname(source)
Packit 3a9065
    summary = Summary(summaries_fn)(text)
Packit 3a9065
    link_name = 'Bug %s - %s' % (text, summary)
Packit 3a9065
    url = 'https://bugzilla.redhat.com/show_bug.cgi?id=%s' % text
Packit 3a9065
    node = nodes.reference(rawtext, link_name, refuri=url)
Packit 3a9065
    return [node], []
Packit 3a9065
Packit 3a9065
def setup(app):
Packit 3a9065
    app.add_role('rhbug', RhBug_role)