#!/usr/bin/env python3
# ===----------------------------------------------------------------------===##
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===----------------------------------------------------------------------===##

import argparse
import logging
import pathlib
import subprocess
import sys
import tempfile


def main(argv):
    parser = argparse.ArgumentParser(
        prog='submit-benchmarks',
        description='Submit a LNT JSON report to a LNT server.')
    parser.add_argument('report', type=pathlib.Path,
        help='Path to the LNT JSON report to submit.')
    parser.add_argument('--lnt-url', type=str, required=True,
        help='The URL of the LNT instance to submit results to.')
    parser.add_argument('--test-suite', type=str, required=True,
        help='The name of the test suite for reporting LNT results.')
    parser.add_argument('--dry-run', action='store_true',
        help='Do not actually perform any action. Use with -vv to see what would be executed.')
    parser.add_argument('-v', '--verbose', action='count', default=0,
        help='Verbosity level: passing the option multiple times increases the level.')
    args = parser.parse_args(argv)

    if args.verbose == 0:
        logging.basicConfig(level=logging.INFO)
    elif args.verbose >= 1:
        logging.basicConfig(level=logging.DEBUG)

    def run(command, **kwargs):
        command = [str(c) for c in command]
        logging.debug(f'$ {" ".join(command)}')
        if args.dry_run:
            return
        try:
            if not args.verbose:
                if 'stdout' not in kwargs:
                    kwargs['stdout'] = subprocess.PIPE
                if 'stderr' not in kwargs:
                    kwargs['stderr'] = subprocess.PIPE
            subprocess.run(command, check=True, **kwargs)
        except subprocess.CalledProcessError as e:
            if e.stdout:
                sys.stdout.write(e.stdout.decode())
            if e.stderr:
                sys.stderr.write(e.stderr.decode())
            raise

    with tempfile.TemporaryDirectory() as tmp:
        tmp = pathlib.Path(tmp)

        logging.info('Installing LNT')
        run(['python3', '-m', 'venv', tmp / '.venv'])
        run([tmp / '.venv/bin/pip', 'install', 'llvm-lnt'])

        logging.info(f'Submitting results to {args.lnt_url}')
        submission_url = f'{args.lnt_url}/db_default/v4/{args.test_suite}/submitRun'
        run([tmp / '.venv/bin/lnt', 'submit', '--ignore-regressions', '--merge', 'append',
                                               submission_url, args.report.resolve()])


if __name__ == '__main__':
    main(sys.argv[1:])
