#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import imp
import os
import sys
import urllib2


BASE_URL = 'http://src.chromium.org/chrome/trunk/'
DEPS_FILE = 'bootstrap_deps'

SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
# Directory containing src/ in a Chromium checkout.
CHECKOUT_BASE_PATH = os.path.join(SCRIPT_PATH, os.pardir, os.pardir, os.pardir)
# Directory in which to save bootstrap files.
BOOTSTRAP_BASE_PATH = os.path.join(SCRIPT_PATH, 'support', 'bootstrap_files')

PERF_DIR = os.path.join('src', 'tools', 'perf')
TELEMETRY_DIR = os.path.join('src', 'tools', 'telemetry')
TELEMETRY_TOOLS_DIR = os.path.join('src', 'tools', 'telemetry_tools')


def _GetBasePath():
  """Find the location of our Chromium or bootstrap checkout.

  It tries to import Telemetry.  If the import succeeds,
  we assume that's the correct location.

  Returns:
    The path containing the src/ directory, or None if no checkout is found.
  """
  module_name = 'telemetry'
  module_path = TELEMETRY_DIR

  try:
    paths = [os.path.join(CHECKOUT_BASE_PATH, module_path)]
    imp.find_module(module_name, paths)
    return CHECKOUT_BASE_PATH
  except ImportError:
    pass

  try:
    paths = [os.path.join(BOOTSTRAP_BASE_PATH, module_path)]
    imp.find_module(module_name, paths)
    return BOOTSTRAP_BASE_PATH
  except ImportError:
    pass

  return None


def _Bootstrap(bootstrap_deps_url):
  """Grab all the deps listed in the file at bootstrap_deps_url."""
  bootstrap_txt = urllib2.urlopen(
      BASE_URL + 'src/tools/telemetry_tools/telemetry_bootstrap.py').read()
  bootstrap = imp.new_module('bootstrap')
  exec bootstrap_txt in bootstrap.__dict__
  bootstrap.DownloadDeps(BOOTSTRAP_BASE_PATH, bootstrap_deps_url)


def ListBootstrapDeps(base_path):
  """List the deps required for telemetry."""
  sys.path.append(os.path.join(base_path, TELEMETRY_TOOLS_DIR))
  import telemetry_bootstrap

  deps_file = os.path.join(base_path, PERF_DIR, DEPS_FILE)
  return telemetry_bootstrap.ListAllDepsPaths(deps_file)


def Main():
  if not _GetBasePath():
    _Bootstrap(BASE_URL + 'src/tools/perf/' + DEPS_FILE)

  new_base_path = _GetBasePath()
  new_perf_path = os.path.join(new_base_path, PERF_DIR)
  new_telemetry_path = os.path.join(new_base_path, TELEMETRY_DIR)

  if '--print-bootstrap-deps' in sys.argv:
    print ListBootstrapDeps(new_base_path)
    return 0

  sys.path.append(new_perf_path)
  import page_sets
  page_set_filenames = page_sets.GetAllPageSetFilenames()

  old_benchmark_names = {
    "image_decoding_benchmark": "image_decoding",
    "image_decoding_measurement": "image_decoding",
    "loading_benchmark": "loading",
    "loading_measurement": "loading",
    "media_measurement": "media",
    "memory_benchmark": "memory",
    "memory_measurement": "memory",
    "rasterize_and_record_benchmark": "rasterize_and_record",
    "rasterize_and_record_measurement": "rasterize_and_record",
    "robohornetpro": "robohornet_pro",
    "scrolling_benchmark": "smoothness",
    "smoothness_benchmark": "smoothness",
    "smoothness_measurement": "smoothness",
    "startup_benchmark": "startup_warm_blank_page",
    "startup_measurement": "startup",
    "tab_switching_measurement": "tab_switching",
  }

  sys.path.append(new_telemetry_path)
  from telemetry.page import page_measurement_runner
  # There are bots that are hard-coded to run some specific named tests.
  # Convert these to the current naming conventions by overriding them
  # in the runner.
  class MeasurementRunner(page_measurement_runner.PageMeasurementRunner):
    def GetModernizedTestName(self, arg):
      if arg not in old_benchmark_names:
        return arg
      sys.stderr.write(
        'An old name %s was given. Please use %s in the future.\n' % (
          arg,
          old_benchmark_names.get(arg)))
      return old_benchmark_names[arg]

  runner = MeasurementRunner()
  return runner.Run(new_perf_path, page_set_filenames)


if __name__ == '__main__':
  sys.exit(Main())