#!/usr/bin/env python
from optparse import OptionParser
import os
import shelve

from citools.data import TestReport
from citools.jenkins.report import Report

if __name__ == '__main__':

    parser = OptionParser()

    (options, args) = parser.parse_args()

    report = Report()

    jenkins_url = os.environ['JENKINS_URL']

    if jenkins_url is None:
        raise RuntimeError("Jenkins Url should be set.")

    job_name = args[0]
    job_url = jenkins_url + "/job/" + job_name
    test_report = report.get_report(job_url)

    db = shelve.open(job_name + ".db")

    previous_report = db['report']

    if test_report.is_incremental() and previous_report is not None:
        suites_by_module = previous_report.suites_per_module

        suites_by_module.update(test_report.suites_by_module)

        test_report = TestReport(test_report.name, suites_by_module, test_report.build_number, false)

    db['report'] = test_report

    db.close()
