#!/usr/bin/python

# This script updates bug descriptions to include PCI
# info from the original reporter's lspci file

from arsenal_lib import *

arsenal = Arsenal()
d = arsenal.load_project("ubuntu")

if len(sys.argv) < 2:
    sys.stderr.write("Usage: " + sys.argv[0] + " <source-package>\n")
    exit(1)

source_pkgs = sys.argv[1:]

regex_pci = re.compile('VGA compatible controller', re.IGNORECASE)
regex_png = re.compile('png', re.IGNORECASE)
regex_syslog = re.compile('syslog', re.IGNORECASE)

total_updated = 0
for pkg in source_pkgs:
    try:
        arsenal.reset()
        d = arsenal.load_project("ubuntu")
        print "Processing ", pkg
        num_updated = 0
        for bugtask in d.searchTasks(search_text=pkg):
            print bugtask.bug.id
            if bugtask.bug_target_display_name != pkg + " (Ubuntu)":
                continue

            bug = ArsenalBug(bugtask.bug, arsenal.launchpad)
            print "\n%-8d  %-12s  %s" % (bug.id, bugtask.status, bug.title)

            # Next if bug description already has pci info listed
            if regex_pci.search(bug.description):
                print " --> Already has PCI info listed"
                continue

            # Scan attachments for lspci from the original reporter
            pci = ''
            for a in bug.attachments:
                print a.title
                if regex_png.search(a.title):
                    continue
                if regex_syslog.search(a.title):
                    continue
                # if author != bug original reporter, skip it
                author = a.message.owner.name
                if author != bug.bug.owner.name:
                    continue

                hosted_file = a.data
                hosted_file_buffer = hosted_file.open()
                content = hosted_file_buffer.read()

                # Stop looking at attachments if this one has the PCI info
                if regex_pci.search(content):
                    pci = content
                    exit

            # No pci info seems to be present
            if pci == '':
                print " --> No pci info available"
                continue

            # Parse lspci info in 'pci'
            want_next_subsystem_line = 0
            pci_vga = ''
            for line in pci.split('\n'):
                if want_next_subsystem_line == 1:
                    if "Subsystem" in line:
                        pci_vga += "    " + line + "\n"
                        want_next_subsystem_line = 0

                if "00:00.0" in line:
                    pci_vga += line + "\n"
                    want_next_subsystem_line = 1
                elif "VGA compatible controller" in line:
                    pci_vga += line + "\n"
                    want_next_subsystem_line = 1
                
            # Append pci info to description
            if pci_vga:
                bug.append_description("\n[lspci]\n" + pci_vga)
                try:
                    bug.bug.lp_save()
                except HTTPError, e:
                    print "*** Encountered HTTP error processing ", pkg, " ***"
                    print e.content
                    time.sleep(60)
                    arsenal.reset()
                    d = arsenal.load_project("ubuntu")
                print " --> Appended to description:\n", pci_vga
                num_updated += 1
            else:
                print " --> Need PCI info but not available"

        total_updated += num_updated
        print "Number of bugs updated:  ", num_updated
        time.sleep(10)
    except HTTPError, e:
        print "*** Encountered HTTP error ***"
        print e.content
        print "Total updated this time for ", pkg, ":  ", total_updated
        time.sleep(60)
        arsenal.reset()
        d = arsenal.load_project("ubuntu")

print "Total updated:  ", total_updated
