#!/usr/bin/env python3

# Copyright 2016 The Kubernetes Authors.
#
# 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.




import argparse
import logging
import sys
import yaml

from data import ResultDb


_log = logging.getLogger('main')


def parse_args():
  parser = argparse.ArgumentParser(
      description="""
      Ingest the output of *.out file(s) and insert it into the database.
      """)
  parser.add_argument(
      'outputs', type=str, nargs='+',
      help='output file(s) from test run. These are the *.out files '
           'generated by `run`.')
  parser.add_argument(
      '--db', type=str, required=True,
      help='result database for analysis')
  parser.add_argument('-v', '--verbose', action='store_true')

  return parser.parse_args()


def go(args):
  db = ResultDb(args.db)
  for output in args.outputs:
    _log.info('Processing %s', output)
    results = yaml.safe_load(open(output))
    db.put(results)
  db.commit()


if __name__ == '__main__':
  the_args = parse_args()

  logging.basicConfig(
      level=logging.DEBUG if the_args.verbose else logging.INFO,
      format='%(levelname)s %(asctime)s %(filename)s:%(lineno)d] %(message)s',
      datefmt="%m-%d %H:%M:%S")

  sys.exit(go(the_args))
