Blame src/plugins/abrt-action-find-bodhi-update

Packit 8ea169
#!/usr/bin/python3 -u
Packit 8ea169
# This program is free software; you can redistribute it and/or modify
Packit 8ea169
# it under the terms of the GNU General Public License as published by
Packit 8ea169
# the Free Software Foundation; either version 2 of the License, or
Packit 8ea169
# (at your option) any later version.
Packit 8ea169
#
Packit 8ea169
# This program is distributed in the hope that it will be useful,
Packit 8ea169
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8ea169
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8ea169
# GNU General Public License for more details.
Packit 8ea169
#
Packit 8ea169
# You should have received a copy of the GNU General Public License
Packit 8ea169
# along with this program; if not, write to the Free Software
Packit 8ea169
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 8ea169
Packit 8ea169
"""
Packit 8ea169
abrt-action-find-bodhi-update - find bodhi update based on ABRT's problem dir
Packit 8ea169
Packit 8ea169
The tool reads duphash file in problem directory and searches for a new updates
Packit 8ea169
according to the crash.
Packit 8ea169
Packit 8ea169
OPTIONS
Packit 8ea169
    -v, --verbose
Packit 8ea169
        Be verbose
Packit 8ea169
Packit 8ea169
    -b, --without-bodhi
Packit 8ea169
        Run without abrt-bodhi. Prints only Bugzilla bug id of duplicate bug,
Packit 8ea169
        if exists.
Packit 8ea169
Packit 8ea169
    -d, --problem-dir PROBLEM_DIR
Packit 8ea169
        Problem directory [Default: current directory]
Packit 8ea169
Packit 8ea169
ENVIRONMENT VARIABLES
Packit 8ea169
    Bugzilla_Product
Packit 8ea169
        Product bug field value. Useful if you needed different product than
Packit 8ea169
        specified in PROBLEM_DIR/os_info
Packit 8ea169
"""
Packit 8ea169
import os
Packit 8ea169
import sys
Packit 8ea169
from argparse import ArgumentParser
Packit 8ea169
from subprocess import Popen, PIPE
Packit 8ea169
Packit 8ea169
from reportclient import (_, log_warning, log1, set_verbosity, verbose, RETURN_OK,
Packit 8ea169
                          RETURN_FAILURE, error_msg)
Packit 8ea169
import report
Packit 8ea169
Packit 8ea169
FILENAME_DUPHASH = "duphash"
Packit 8ea169
FILENAME_OSINFO = "os_info"
Packit 8ea169
OSINFO_BUGZILLA_PRODUCT = "REDHAT_BUGZILLA_PRODUCT="
Packit 8ea169
OSINFO_BUGZILLA_PRODUCT_VERSION = "REDHAT_BUGZILLA_PRODUCT_VERSION="
Packit 8ea169
Packit 8ea169
def parse_os_release_line(l):
Packit 8ea169
    """Parse key-value line and returns value"""
Packit 8ea169
    return l.split('=')[1]
Packit 8ea169
Packit 8ea169
if __name__ == "__main__":
Packit 8ea169
    CMDARGS = ArgumentParser(
Packit 8ea169
            description=("Search bodhi updates based on ABRT's problem dir"))
Packit 8ea169
    CMDARGS.add_argument("-d", "--problem-dir",
Packit 8ea169
            type=str, default=".",
Packit 8ea169
            help="Path to problem directory")
Packit 8ea169
    CMDARGS.add_argument("-b", "--without-bodhi",
Packit 8ea169
            action="store_true", dest="without_bodhi", default=False,
Packit 8ea169
            help="Run without abrt-bohi")
Packit 8ea169
    CMDARGS.add_argument("-v", "--verbose",
Packit 8ea169
            action="count", dest="verbose", default=0,
Packit 8ea169
            help="Be verbose")
Packit 8ea169
Packit 8ea169
    OPTIONS = CMDARGS.parse_args()
Packit 8ea169
    DIR_PATH = os.path.abspath(OPTIONS.problem_dir)
Packit 8ea169
Packit 8ea169
    verbose = 0
Packit 8ea169
    ABRT_VERBOSE = os.getenv("ABRT_VERBOSE")
Packit 8ea169
    if ABRT_VERBOSE:
Packit 8ea169
        try:
Packit 8ea169
            verbose = int(ABRT_VERBOSE)
Packit 8ea169
        #pylint: disable=bare-except
Packit 8ea169
        except:
Packit 8ea169
            pass
Packit 8ea169
Packit 8ea169
    verbose += OPTIONS.verbose
Packit 8ea169
    set_verbosity(verbose)
Packit 8ea169
    os.environ["ABRT_VERBOSE"] = str(verbose)
Packit 8ea169
Packit 8ea169
    try:
Packit 8ea169
        dump_dir = report.dd_opendir(DIR_PATH, report.DD_OPEN_READONLY)
Packit 8ea169
        if not dump_dir:
Packit 8ea169
            raise ValueError(_("cannot open problem directory '{0}'").format(DIR_PATH))
Packit 8ea169
Packit 8ea169
        dd_load_flag = (report.DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE |
Packit 8ea169
                        report.DD_FAIL_QUIETLY_ENOENT)
Packit 8ea169
Packit 8ea169
        duphash_content = dump_dir.load_text(FILENAME_DUPHASH, dd_load_flag)
Packit 8ea169
        if not duphash_content:
Packit 8ea169
            raise ValueError("problem directory misses '{0}'".format(FILENAME_DUPHASH))
Packit 8ea169
Packit 8ea169
        os_info = dump_dir.load_text(FILENAME_OSINFO, dd_load_flag)
Packit 8ea169
        if not os_info:
Packit 8ea169
            raise ValueError("problem directory misses '{0}'" .format(FILENAME_OSINFO))
Packit 8ea169
Packit 8ea169
    except ValueError as ex:
Packit 8ea169
        error_msg(_("Problem directory error: {0}").format(ex))
Packit 8ea169
        sys.exit(RETURN_FAILURE)
Packit 8ea169
    finally:
Packit 8ea169
        dump_dir.close()
Packit 8ea169
Packit 8ea169
    # get Bugzilla Product and Version from os_info
Packit 8ea169
    product = os.getenv("Bugzilla_Product") or ""
Packit 8ea169
    version = ""
Packit 8ea169
    for line in os_info.split("\n"):
Packit 8ea169
        if (OSINFO_BUGZILLA_PRODUCT in line) and (product == ""):
Packit 8ea169
            product = parse_os_release_line(line)
Packit 8ea169
        if OSINFO_BUGZILLA_PRODUCT_VERSION in line:
Packit 8ea169
            version = parse_os_release_line(line)
Packit 8ea169
Packit 8ea169
    if product == "":
Packit 8ea169
        log1(_("Using product '{0}' from /etc/os-release.").format(OSINFO_BUGZILLA_PRODUCT))
Packit 8ea169
    else:
Packit 8ea169
        log1(_("Using product {0}.").format(product))
Packit 8ea169
    if version != "":
Packit 8ea169
        log1(_("Using product version {0}.").format(version))
Packit 8ea169
Packit 8ea169
    # Find bugzilla bug with abrt_hash: == $duphash_content and product ==
Packit 8ea169
    # $product, if OSINFO_BUGZILLA_PRODUCT from crash's os_info doesn't exist,
Packit 8ea169
    # the OSINFO_BUGZILLA_PRODUCT from /etc/os-release is used
Packit 8ea169
    with Popen(["reporter-bugzilla -h {0} -p{1}".format(duphash_content, product)],
Packit 8ea169
             shell=True,
Packit 8ea169
             stdout=PIPE,
Packit 8ea169
             bufsize=-1) as proc:
Packit 8ea169
        bug_id = proc.stdout.read().rstrip().decode("utf-8", "ignore")
Packit 8ea169
Packit 8ea169
    if bug_id:
Packit 8ea169
        log_warning(_("Duplicate bugzilla bug '#{0}' was found").format(bug_id))
Packit 8ea169
    else:
Packit 8ea169
        log1(_("There is no bugzilla bug with 'abrt_hash:{0}'").format(duphash_content))
Packit 8ea169
        sys.exit(RETURN_OK)
Packit 8ea169
Packit 8ea169
    # abrt-bodhi do not support rawhide, because there are no updates for rawhide in Bodhi
Packit 8ea169
    if "rawhide" in version.lower():
Packit 8ea169
        log1(_("Warning: abrt-bodhi do not support Product version 'Rawhide'"))
Packit 8ea169
        sys.exit(RETURN_OK)
Packit 8ea169
Packit 8ea169
    if not OPTIONS.without_bodhi:
Packit 8ea169
        Popen(["abrt-bodhi -r -b {0} -d {1}".format(bug_id, DIR_PATH)],
Packit 8ea169
                shell=True,
Packit 8ea169
                bufsize=-1).wait()
Packit 8ea169
Packit 8ea169
    sys.exit(RETURN_OK)