# Lint as: python2, python3
#!/usr/bin/env python3

"""
This file generates all telemetry_Benchmarks control files from a main list.
"""



from datetime import datetime
import os
import re

# This test list is a subset of telemetry benchmark tests. The full list can be
# obtained by executing
# /build/${BOARD}/usr/local/telemetry/src/tools/perf/list_benchmarks

# PLEASE READ THIS:

# PERF_TESTS: these tests run on each build: tot, tot-1, tot-2 and expensive to
# run.

# PERF_DAILY_RUN_TESTS: these tests run on a nightly build: tot. If you are
# trying to gain confidence for a new test, adding your test in this list is a
# good start.

# For adding a new test to any of these lists, please add rohitbm, lafeenstra,
# haddowk in the change.

PERF_PER_BUILD_TESTS = (
    'loading.desktop',
    'rendering.desktop',
    'speedometer2',
)

PERF_DAILY_RUN_TESTS = (
    'blink_perf.image_decoder',
)

ALL_TESTS = (PERF_PER_BUILD_TESTS + PERF_DAILY_RUN_TESTS)

EXTRA_ARGS_MAP = {
    'loading.desktop': '--story-tag-filter=typical',
    'rendering.desktop': '--story-tag-filter=top_real_world_desktop',
}

DEFAULT_YEAR = str(datetime.now().year)

DEFAULT_AUTHOR = 'ChromeOS Team'

CONTROLFILE_TEMPLATE = (
        """# Copyright {year} The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Do not edit this file! It was created by generate_controlfiles.py.

from autotest_lib.client.common_lib import utils

AUTHOR = '{author}'
NAME = 'telemetry_Benchmarks.{test}'
{attributes}
TIME = 'LONG'
TEST_CATEGORY = 'Benchmark'
TEST_CLASS = 'performance'
TEST_TYPE = 'server'
PY_VERSION = 3

DOC = '''
This server side test suite executes the Telemetry Benchmark:
{test}
This is part of Chrome for ChromeOS performance testing.

Pass local=True to run with local telemetry and no AFE server.
'''

def run_benchmark(machine):
    host = hosts.create_host(machine)
    dargs = utils.args_to_dict(args)
    dargs['extra_args'] = '{extra_args}'.split()
    job.run_test('telemetry_Benchmarks', host=host,
                 benchmark='{test}',
                 tag='{test}',
                 args=dargs)

parallel_simple(run_benchmark, machines)""")


def _get_suite(test):
    if test in PERF_PER_BUILD_TESTS:
        return 'ATTRIBUTES = \'suite:crosbolt_perf_perbuild\''
    elif test in PERF_DAILY_RUN_TESTS:
        return 'ATTRIBUTES = \'suite:crosbolt_perf_nightly\''
    return ''


def get_existing_fields(filename):
    """Returns the existing copyright year and author of the control file."""
    if not os.path.isfile(filename):
        return (DEFAULT_YEAR, DEFAULT_AUTHOR)

    copyright_year = DEFAULT_YEAR
    author = DEFAULT_AUTHOR
    copyright_pattern = re.compile(
            '# Copyright (\d+) The Chromium OS Authors.')
    author_pattern = re.compile("AUTHOR = '(.+)'")
    with open(filename) as f:
        for line in f:
            match_year = copyright_pattern.match(line)
            if match_year:
                copyright_year = match_year.group(1)
            match_author = author_pattern.match(line)
            if match_author:
                author = match_author.group(1)
    return (copyright_year, author)


def generate_control(test):
    """Generates control file from the template."""
    filename = 'control.%s' % test
    copyright_year, author = get_existing_fields(filename)
    extra_args = EXTRA_ARGS_MAP.get(test, '')

    with open(filename, 'w+') as f:
        content = CONTROLFILE_TEMPLATE.format(
                attributes=_get_suite(test),
                author=author,
                extra_args=extra_args,
                test=test,
                year=copyright_year)
        f.write(content)


def check_unmanaged_control_files():
    """Prints warning if there is unmanaged control file."""
    for filename in os.listdir('.'):
        if not filename.startswith('control.'):
            continue
        test = filename[len('control.'):]
        if test not in ALL_TESTS:
            print('warning, unmanaged control file:', test)


def main():
    """The main function."""
    for test in ALL_TESTS:
        generate_control(test)
    check_unmanaged_control_files()


if __name__ == "__main__":
    main()
