#!/usr/bin/python
# Copyright 2015 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.

"""Runs the Perf Dashboard Python unit test suite."""

import os
import sys

_CATAPULT = os.path.abspath(
    os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir))


def _ExtraPaths():
  """Returns a list of paths to add to sys.path when running dashboard tests."""
  try:
    import dev_appserver
  except ImportError:
    # TODO(qyearsley): Put the App Engine SDK in the path with the
    # binary dependency manager.
    print 'This script requires the App Engine SDK to be in PYTHONPATH.'
    sys.exit(1)
  extra_paths = dev_appserver.EXTRA_PATHS
  dashboard_path = os.path.join(_CATAPULT, 'dashboard')
  extra_paths.append(dashboard_path)
  _AddToPathIfNeeded(dashboard_path)
  import dashboard
  for library in dashboard.THIRD_PARTY_LIBRARIES:
    extra_paths.append(os.path.join(_CATAPULT, 'third_party', library))
  return extra_paths


def _AddToPathIfNeeded(path):
  if path not in sys.path:
    sys.path.insert(0, path)


if __name__ == '__main__':
  _AddToPathIfNeeded(_CATAPULT)

  from hooks import install
  if '--no-install-hooks' in sys.argv:
    sys.argv.remove('--no-install-hooks')
  else:
    install.InstallHooks()

  from catapult_build import run_with_typ
  sys.exit(run_with_typ.Run(
      os.path.join(_CATAPULT, 'dashboard', 'dashboard'),
      path=_ExtraPaths()))
