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

import os
import sys
import logging


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


def Main():
  catapult_path = os.path.abspath(
      os.path.join(os.path.dirname(__file__), '..', '..'))

  _AddToPathIfNeeded(os.path.join(catapult_path, 'dashboard'))
  import dashboard
  paths = dashboard.PathsForDeployment()

  _AddToPathIfNeeded(catapult_path)
  from catapult_build import appengine_dev_server, temp_deployment_dir

  # Build the necessary files in the directory.
  from dashboard_build import preprocess
  with temp_deployment_dir.TempDeploymentDir(paths) as tmp_dir:
    preprocess.PackPinpoint(catapult_path, tmp_dir, paths)

    # The bundled dir is already copied, so don't have the deployment logic
    # copy it again.
    paths.remove(os.path.join(tmp_dir, 'bundled'))
    appengine_dev_server.DevAppserver(paths, sys.argv[1:], reuse_path=tmp_dir)


if __name__ == '__main__':
  logging.basicConfig(
      stream=sys.stdout,
      level=logging.INFO,
      format='[%(asctime)s - %(levelname)s]: \t%(message)s')
  Main()
