#!/usr/bin/python
# Massive Bug Filer
# Copyright (C) 2009, Canonical, Ltd.
# License: GPLv3
# Author: Colin Watson <colin.watson@canonical.com>
# Author: Kees Cook <kees@ubuntu.com>
#
# Reports source packages per month
#
import sys
import lpl_common

lp = lpl_common.connect()
ubuntu = lp.distributions['ubuntu']

series_list = ['dapper', 'hardy', 'intrepid', 'jaunty']
year_list = [2005, 2006, 2007, 2008, 2009]

if len(sys.argv)>1:
    series_list = sys.argv[1].split(',')
if len(sys.argv)>2:
    year_list = [int(x) for x in sys.argv[2].split(',')]

seen = dict()
for series_name in series_list:
    seen.setdefault(series_name, dict())
    series = ubuntu.getSeries(name_or_version=series_name)
    for pocket in ('Security', 'Updates'):
        total = {}
        for spph in ubuntu.main_archive.getPublishedSources(distro_series=series, pocket=pocket, published_since_date='%d-01-01' % (sorted(year_list)[0])):

            year = spph.date_published.year
            if year not in year_list:
                continue
            month = spph.date_published.month

            total.setdefault(year, {})
            total[year].setdefault(month, [0, 0])

            seen[series_name].setdefault(spph.source_package_name, set())
            if spph.source_package_version not in seen[series_name][spph.source_package_name]:
                seen[series_name][spph.source_package_name].add(spph.source_package_version)
                total[year][month][0] += 1
                if spph.source_package_name.startswith('language-pack-'):
                    total[year][month][1] += 1

        print '%s-%s:' % (series_name, pocket.lower())
        for year in sorted(total):
            for month in sorted(total[year]):
                if total[year][month][1]:
                    print '  %d-%02d: %d (+ %d langpacks)' % (year, month, total[year][month][0]-total[year][month][1], total[year][month][1])
                else:
                    print '  %d-%02d: %d' % (year, month, total[year][month][0])
