#!/usr/bin/env python
# coding=utf-8

"""

   Copyright 2015 Andreas Würl

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

"""

from __future__ import print_function

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.")

    for job_name in args:
        job_url = jenkins_url + "job/" + job_name
        test_report = report.get_report(job_url)

        if test_report is not None:
            db = shelve.open(job_name + ".db")

            previous_report = db.get('report', None)

            if test_report.is_incremental and previous_report is not None:
                print("incremental update of " + str(previous_report))

                suites_by_module = previous_report.suites_by_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

            print(job_name + ": " + str(test_report))

            db.close()
