#!/usr/bin/python
#
# Chris Van Hoof -- vanhoof@canonical.com
#  ps-hwe-current-bugs.py
#
# $ python ps-hwe-current-bugs.py -p <lp project> -t <lp tag> [-l (list all tags)]
#   Project: <lp project> [Tag: <lp tag>]
#    CURRENT BUGS:
#        [id :: importance :: status :: title :: owner :: date opened :: date last updated]
#    FIXED BUGS:
#        [...]
#    INVALID BUGS:
#        [...]
#

import os
import sys
import getopt
from launchpadlib.launchpad import Launchpad

APP_NAME = 'ps-hwe-current-bugs.py'
SERVICE_ROOT = 'production'
CACHE_DIR = os.path.expanduser('~/.launchpadlib/cache')

def usage():
    print " USAGE:"
    print "\tps-hwe-current-bugs.py -p <project> -t <tag>"
    print "\tps-hwe-current-bugs.py -p <project> -l"
    return 0

def fetch_lp_object():
    lp_object = Launchpad.login_with(APP_NAME, SERVICE_ROOT, CACHE_DIR)
    return lp_object

def short_date(str):
    return str.split(' ')[0]

def show_valid_tags(pillar_name):
    launchpad = fetch_lp_object()
    pillar = launchpad.projects[pillar_name]
    valid_tags = pillar.official_bug_tags

    print " OFFICAL TAGS:"
    for valid_tag in (valid_tags):
        print "\t%s" % (valid_tag)

    return 0

def show_bug_data(launchpad, pillar_name, tag_id):
    pillar = launchpad.projects[pillar_name]
    
    current_bugs = pillar.searchTasks(
        tags=[tag_id],
        status=[
            "New",
            "Confirmed",
            "In Progress",
            "Incomplete",
            "Triaged"])

    fixed_bugs = pillar.searchTasks(
        tags=[tag_id],
        status=[
            "Fix Committed",
            "Fix Released"])

    invalid_bugs = pillar.searchTasks(
        tags=[tag_id],
        status=[
            "Invalid",
            "Won't Fix"])
    
    bug_type_to_list_dict = {
        'CURRENT BUGS'  : current_bugs,
        'FIXED BUGS'    : fixed_bugs,
        'INVALID BUGS'  : invalid_bugs,
    }

    bug_type_array = ['CURRENT BUGS', 'FIXED BUGS', 'INVALID BUGS']


    print "Project: %s [Tag: %s]" % (pillar.name, tag_id)
    print "\t[id :: importance :: status :: title :: owner :: date opened :: date last updated]\n"

    for bug_type in bug_type_array:
        print " %s:" % (bug_type)
        for a_bug in list(bug_type_to_list_dict[bug_type]):
            a_bug_creation_date = short_date(str(a_bug.bug.date_created))
            a_bug_modified_date = short_date(str(a_bug.bug.date_last_updated))
            print "\t%s :: %s :: %s :: %s :: %s (%s) :: %s :: %s" % (
                a_bug.bug.id, a_bug.importance, a_bug.status,
                a_bug.bug.title, a_bug.bug.owner.display_name,
                a_bug.bug.owner.name, a_bug_creation_date,
                a_bug_modified_date)

    return 0


def main(args):
    if len(args) == 1:
        usage()
        sys.exit(2)

    try:
        opts, args = getopt.getopt(args[1:], "p:t:lh")
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt == "-h":
            usage()
            sys.exit()
        elif opt in "-p":
            pillar_name = arg
        elif opt == "-l":
            show_valid_tags(pillar_name)
            sys.exit()
        elif opt in "-t":
            tag_id = arg

    launchpad = fetch_lp_object()
    show_bug_data(launchpad, pillar_name, tag_id)

    return 0    


if __name__ == '__main__':
    sys.exit(main(sys.argv))
