Jiri Moskovcak 935a95
#! /usr/bin/python
Jiri Moskovcak 935a95
#-*- coding: utf-8 -*-
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
import os
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
# abrt_v4 TABLE columns:
Jiri Moskovcak 935a95
UUID         = 0
Jiri Moskovcak 935a95
UID          = 1
Jiri Moskovcak 935a95
INFORMALL    = 2
Jiri Moskovcak 935a95
DUMPDIR_PATH = 3
Jiri Moskovcak 935a95
COUNT        = 4
Jiri Moskovcak 935a95
REPORTED     = 5
Jiri Moskovcak 935a95
TIME         = 6
Jiri Moskovcak 935a95
MESSAGE      = 7
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
# abrt_v4_reportresult columns:
Jiri Moskovcak 935a95
#UUID     = 0
Jiri Moskovcak 935a95
#UID      = 1
Jiri Moskovcak 935a95
REPORTER = 2
Jiri Moskovcak 935a95
RESULT_MESSAGE  = 3
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
def get_db_path():
Jiri Moskovcak 935a95
    path = "/var/spool/abrt/abrt-db"
Jiri Moskovcak 935a95
    try:
Jiri Moskovcak 935a95
        with open("/etc/abrt/plugins/SQLite3.conf") as f:
Jiri Moskovcak 935a95
            for line in f:
Jiri Moskovcak 935a95
                line = line.split('=', 1)
Jiri Moskovcak 935a95
                if len(line) == 2 and line[0].strip() == "DBPath":
Jiri Moskovcak 935a95
                    path = line[1].strip()
Jiri Moskovcak 935a95
    except Exception, ex:
Jiri Moskovcak 935a95
        pass
Jiri Moskovcak 935a95
    return path
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
def get_url_from_text(text):
Jiri Moskovcak 935a95
    url_marks = ["http://", "https://", "ftp://", "ftps://", "file://"]
Jiri Moskovcak 935a95
    lines = text.split('\n')
Jiri Moskovcak 935a95
    url = ""
Jiri Moskovcak 935a95
    for mark in url_marks:
Jiri Moskovcak 935a95
        for line in lines:
Jiri Moskovcak 935a95
            last_mark = line.find(mark)
Jiri Moskovcak 935a95
            if last_mark != -1:
Jiri Moskovcak 935a95
                url_end = line.find(' ',last_mark)
Jiri Moskovcak 935a95
                if url_end == -1:
Jiri Moskovcak 935a95
                    url_end = len(line)
Jiri Moskovcak 935a95
                url = "URL=" + line[last_mark:url_end]
Jiri Moskovcak 935a95
    return url
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
def format_reported_to(reported_to):
Jiri Moskovcak 935a95
    reporter = reported_to[REPORTER]
Jiri Moskovcak 935a95
    url = get_url_from_text(reported_to[RESULT_MESSAGE])
Jiri Moskovcak 935a95
    if not url:
Jiri Moskovcak 935a95
        url = reported_to[RESULT_MESSAGE]
Jiri Moskovcak 935a95
    return reporter + ": " + url
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
if __name__ == "__main__":
Jiri Moskovcak 935a95
    try:
Jiri Moskovcak 935a95
        from sqlite3 import dbapi2 as sqlite
Jiri Moskovcak 935a95
        db = sqlite.connect(get_db_path())
Jiri Moskovcak 935a95
        crashes = db.execute("SELECT * FROM abrt_v4")
Jiri Moskovcak 935a95
        # abrt_v4 TABLE columns:
Jiri Moskovcak 935a95
        # UUID | UID | INFORMALL | DUMPDIR_PATH | COUNT | REPORTED | TIME | MESSAGE
Jiri Moskovcak 935a95
        for crash in crashes:
Jiri Moskovcak 935a95
            # abrt_v4_reportresult columns:
Jiri Moskovcak 935a95
            # UUID | UID | REPORTER | MESSAGE
Jiri Moskovcak 935a95
            report_results = db.execute("SELECT * FROM abrt_v4_reportresult WHERE UUID='%s'" % crash[UUID])
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
            # save count from db to file
Jiri Moskovcak 935a95
            count_file = "%s/count" % crash[DUMPDIR_PATH]
Jiri Moskovcak 935a95
            # don't overwrite
Jiri Moskovcak 935a95
            if not os.path.exists(count_file):
Jiri Moskovcak 935a95
                try:
Jiri Moskovcak 935a95
                    fout = open(count_file, "w")
Jiri Moskovcak 935a95
                    fout.write(str(crash[COUNT]))
Jiri Moskovcak 935a95
                    fout.close()
Jiri Moskovcak 935a95
                except Exception, ex:
Jiri Moskovcak 935a95
                    # silently ignore errors -> probably stalled db, but we can't
Jiri Moskovcak 935a95
                    # do much about it, so it's better to not polute the rpm output
Jiri Moskovcak 935a95
                    pass
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
            # save uuid from db to file
Jiri Moskovcak 935a95
            uuid_file = "%s/uuid" % crash[DUMPDIR_PATH]
Jiri Moskovcak 935a95
            # don't overwrite
Jiri Moskovcak 935a95
            if not os.path.exists(uuid_file):
Jiri Moskovcak 935a95
                try:
Jiri Moskovcak 935a95
                    fout = open(uuid_file, "w")
Jiri Moskovcak 935a95
                    fout.write(str(crash[UUID]))
Jiri Moskovcak 935a95
                    fout.close()
Jiri Moskovcak 935a95
                except Exception, ex:
Jiri Moskovcak 935a95
                    # silently ignore errors -> probably stalled db, but we can't
Jiri Moskovcak 935a95
                    # do much about it, so it's better to not polute the rpm output
Jiri Moskovcak 935a95
                    pass
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
            results = report_results.fetchall()
Jiri Moskovcak 935a95
            if results:
Jiri Moskovcak 935a95
                # save report results from db to file
Jiri Moskovcak 935a95
                reported_to_file = "%s/reported_to" % crash[DUMPDIR_PATH]
Jiri Moskovcak 935a95
                if not os.path.exists(reported_to_file):
Jiri Moskovcak 935a95
                    try:
Jiri Moskovcak 935a95
                        fout = open(reported_to_file, "w")
Jiri Moskovcak 935a95
                    except Exception, ex:
Jiri Moskovcak 935a95
                        # silently ignore errors -> probably stalled db, but we can't
Jiri Moskovcak 935a95
                        # do much about it, so it's better to not polute the rpm output
Jiri Moskovcak 935a95
                        continue
Jiri Moskovcak 935a95
Jiri Moskovcak 935a95
                    for report_result in results:
Jiri Moskovcak 935a95
                        # print "\t", format_reported_to(report_result)
Jiri Moskovcak 935a95
                        # I know, it adds a '\n' to the end, but it's not a problem
Jiri Moskovcak 935a95
                        fout.write("%s\n" % format_reported_to(report_result))
Jiri Moskovcak 935a95
                    fout.close()
Jiri Moskovcak 935a95
        db.close()
Jiri Moskovcak 935a95
    except Exception, ex:
Jiri Moskovcak 935a95
        # in case of any unhandled error, just ignore it, the worst, what
Jiri Moskovcak 935a95
        # can happen is that the old reports are marked as unreported
Jiri Moskovcak 935a95
        #print ex
Jiri Moskovcak 935a95
        pass