#!/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 urllib

# Directory path in which to save bootstrap files.
BOOTSTRAPPED_FILES_DIR = 'support/bootstrap_files'
PERF_DIR = 'src/tools/perf'
DEPS_FILE = 'bootstrap_deps'

def BootstrapIfNeeded(module_name, module_path, module_deps_url):
  """Ensures that the given module_name is available, grab from URL if not."""
  try:
    imp.find_module(module_name)
    return
  except ImportError:
    sys.path.append(os.path.join(os.path.dirname(__file__),
                                 BOOTSTRAPPED_FILES_DIR,
                                 module_path))
    try:
      imp.find_module(module_name)
      return
    except ImportError:
      bootstrap_txt = urllib.urlopen('http://src.chromium.org/viewvc/chrome/' +
                                     'trunk/src/tools/telemetry_tools/' +
                                     'telemetry_bootstrap.py').read()
      bootstrap = imp.new_module('bootstrap')
      exec bootstrap_txt in bootstrap.__dict__
      bootstrap.DownloadDepsURL(os.path.join(os.path.dirname(__file__),
                                             BOOTSTRAPPED_FILES_DIR),
                                module_deps_url)
      return

def ListBootstrapDeps():
  """List the deps required for telemetry.

  Returns: a list of telemetry deps.
  """
  import telemetry_bootstrap
  deps_file = os.path.join(os.path.dirname(perf_tools.__file__),
                           DEPS_FILE)
  return telemetry_bootstrap.ListAllDepsPaths(open(deps_file).read())

if __name__ == '__main__':
  BootstrapIfNeeded('perf_tools', PERF_DIR,
                    'http://src.chromium.org/viewvc/chrome/trunk/src/tools'
                    '/perf/perf_tools/' + DEPS_FILE)
  import perf_tools
  if '--print-bootstrap-deps' in sys.argv:
    print ListBootstrapDeps()
    sys.exit(0)

  from telemetry.page import page_benchmark_runner
  benchmark_dir = os.path.dirname(perf_tools.__file__)
  sys.exit(page_benchmark_runner.Main(benchmark_dir))
