#!/usr/bin/env python2

import sys
import os
import commands
import subprocess
import logging


# append path so we can import project modules
PROJECT_ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) + "/../"
PROJECT_SRC_PATH = PROJECT_ROOT_PATH + "src/"
sys.path.append(PROJECT_SRC_PATH)

from lib import constants, file_ops, log, virtual_env


VIRTENV_PATH = PROJECT_ROOT_PATH + constants.path.VIRTUALENV_DIR
logger = logging.getLogger()


def set_up_directories():
    logger.info("Setting up directories.")
    file_ops.create_directory(PROJECT_ROOT_PATH + constants.path.LOGS)
    file_ops.create_directory(VIRTENV_PATH)


def update_virtenv():
    exit_code = subprocess.call(
        ["pip", "install", "-r", constants.path.REQUIREMENTS]
    )

    if exit_code != 0:
        raise EnvironmentError("Problem installing requirements")


def run_tests():
    # todo: add option to run in parallel
    import nose
    nose.main()


if __name__ == "__main__":
    set_up_directories()
    log.set_default_file_handler(
        logger, constants.path.LOGS + constants.log.TEST_RUNNER
    )

    exit_code, result = commands.getstatusoutput(
        "virtualenv %s" % PROJECT_ROOT_PATH + constants.path.VIRTUALENV_DIR
    )

    if exit_code != 0:
        print result
        print "Failed to set up basic virtualenv. Please check the logs."
        raise RuntimeError

    logger.info("Updating virtual environment packages.")

    # doing execfile() on this file will alter the current interpreter's
    # environment so you can import libraries in the virtualenv
    virtual_env.activate(PROJECT_ROOT_PATH)
    update_virtenv()

    run_tests()

@@ -16,16 +16,260 @@
 python2%0A
+# Copyright (C) 2015 Google Inc., authors, and contributors %3Csee AUTHORS file%3E%0A# Licensed under http://www.apache.org/licenses/LICENSE-2.0 %3Csee LICENSE file%3E%0A# Created By: jernej@reciprocitylabs.com%0A# Maintained By: jernej@reciprocitylabs.com%0A%0A
 %0Aimport 
@@ -302,26 +302,8 @@
 nds%0A
-import subprocess%0A
 impo
@@ -568,16 +568,29 @@
 tual_env
+, environment
 %0A%0A%0AVIRTE
@@ -680,557 +680,363 @@
 ger(
-)%0A%0A%0Adef set_up_directories():%0A    logger.info(%22Setting up directories.%22)%0A    file_ops.create_directory(PROJECT_ROOT_PATH + constants.path.LOGS)%0A    file_ops.create_directory(VIRTENV_PATH)%0A%0A%0Adef update_virtenv():%0A    exit_code = subprocess.call(%0A        %5B%22pip%22, %22install%22, %22-r%22, constants.path.REQUIREMENTS%5D%0A    )%0A%0A    if exit_code != 0:%0A        raise EnvironmentError(%22Problem installing requirements%22)%0A%0A%0Adef run_tests():%0A    # todo: add option to run in parallel%0A    import nose%0A    nose.main()%0A%0A%0Aif __name__ == %22__main__%22:%0A    set_up_directories()
+%22selenium.webdriver.remote.remote_connection%22)%0A%0A%0Adef run_tests():%0A    # todo: integrate with pytest-flask for app logs&profiling%0A    import pytest%0A    logger.setLevel(environment.LOGGING_LEVEL)%0A%0A    pytest.main()%0A%0A%0Aif __name__ == %22__main__%22:%0A    file_ops.create_directory(environment.LOG_PATH)%0A    file_ops.delete_directory_contents(environment.LOG_PATH)%0A
 %0A   
@@ -1081,16 +1081,44 @@
  logger,
+%0A        PROJECT_ROOT_PATH +
  constan
@@ -1129,18 +1129,30 @@
 ath.LOGS
+_DIR +%0A      
  
-+
  constan
@@ -1154,19 +1154,20 @@
 nstants.
-log
+path
 .TEST_RU
@@ -1174,24 +1174,149 @@
 NNER%0A    )%0A%0A
+    if os.path.isdir(VIRTENV_PATH):%0A        output_option = open(os.devnull, 'w')%0A    else:%0A        output_option = None%0A    
     exit_cod
@@ -1361,16 +1361,20 @@
         
+    
 %22virtual
@@ -1381,13 +1381,34 @@
 env 
+%25s 
 %25s%22 %25
+ (%0A               
  PRO
@@ -1453,24 +1453,101 @@
 LENV_DIR
+,
 %0A    
-)%0A%0A
+            %22--python=python2 --no-site-packages%22%0A            )%0A        )%0A%0A    
     if e
@@ -1565,24 +1565,28 @@
  0:%0A        
+    
 print result
@@ -1590,24 +1590,28 @@
 ult%0A        
+    
 print %22Faile
@@ -1672,16 +1672,20 @@
         
+    
 raise Ru
@@ -1704,266 +1704,289 @@
     
-logger.info(%22Updating 
 virtual
- environment packages.%22)%0A%0A    # doing execfile() on this file will alter the current interpreter's%0A    # environment so you can import libraries in the virtualenv%0A    virtual_env.activate(PROJECT_ROOT_PATH)%0A    update_virtenv()%0A%0A
+_env.activate(PROJECT_ROOT_PATH)%0A%0A    if constants.test_runner.UPDATE_VENV in sys.argv:%0A        virtual_env.update_virtenv(%0A            output_option,%0A            PROJECT_ROOT_PATH + constants.path.RESOURCES +%0A            constants.path.REQUIREMENTS%0A        )%0A    else:%0A    
     

Update test runner for manual env updating