#!/usr/bin/env python
import argparse
import subprocess
import os
import sys

PROJECT_NAME = "cla_frontend"
BACKEND_PROJECT_NAME = "cla_backend"

# use python scripts/jenkins/build.py integration

# args
parser = argparse.ArgumentParser(
    description='Build project ready for testing by Jenkins.')
parser.add_argument('envname', metavar='envname', type=str,
                    nargs=1, help='e.g. integration, production, etc.')
parser.add_argument('--backend-dir', metavar='backend_dir', type=str, nargs=1,
                    help='path to backend project')
args = parser.parse_args()

env = args.envname[0]
backend_workspace = args.backend_dir[0]

env_name = "%s-%s" % (PROJECT_NAME, env)
env_path = "/tmp/jenkins/envs/%s" % env_name
bin_path = "%s/bin" % env_path

backend_env_name = "%s-%s" % (BACKEND_PROJECT_NAME, env)
backend_env_path = "/tmp/jenkins/envs/%s" % backend_env_name
backend_bin_path = "%s/bin" % backend_env_path


def run(command, ignore_rc=False, **kwargs):
    defaults = {
        'shell': True
    }
    defaults.update(kwargs)

    return_code = subprocess.call(command, **defaults)
    if return_code:
        if not ignore_rc:
            sys.exit(return_code)


def run_bg(command, **kwargs):
    defaults = {
        'shell': True
    }
    defaults.update(kwargs)

    return subprocess.Popen(command, **defaults)

print 'starting...'
run('pkill -f envs/cla_.*integration', ignore_rc=True)

# setting up virtualenv
if not os.path.isdir(env_path):
    run('virtualenv --no-site-packages %s' % env_path)

run('%s/pip install -r requirements/jenkins.txt' % bin_path)


# Remove .pyc files from the project
run("find . -name '*.pyc' -delete")

# make sure bower really does install the right versions every time
run("rm -rf cla_frontend/assets-src/vendor")

# build js assets
run('%s/python manage.py builddata constants_json' % bin_path)
run('npm install')
run("bower install")
run("gulp build")

# run python tests
run(("%s/python manage.py jenkins --coverage-rcfile=.coveragerc "
     "--settings=cla_frontend.settings.jenkins") % bin_path)

# start backend and frontend dev servers
backend_process = run_bg(
    "cd %s && %s/python manage.py testserver initial_category.json test_provider.json test_auth_clients.json --addrport 8000 --noinput --settings=cla_backend.settings.jenkins" % (backend_workspace.replace(' ', '\ '), backend_bin_path, ))
run("wget http://localhost:8000/admin/ -t 20 --retry-connrefused --waitretry=2 -T 60")

frontend_process = run_bg("%s/python manage.py runserver 0.0.0.0:8001" % bin_path)
run("wget http://localhost:8001/ -t 20 --retry-connrefused --waitretry=2 -T 60")

# run Karma unit tests
run('npm run test-single-run')

# run protractor tests against SauceLabs
run(
    ('node_modules/protractor/bin/protractor '
     '--sauceUser %s --sauceKey %s '
     'cla_frontend/assets-src/javascripts/app/test/protractor-jenkins-conf.js') % (
        os.environ.get('SAUCE_USER'),
        os.environ.get('SAUCE_KEY'))
)

print 'exiting...'
run('pkill -f envs/cla_.*integration', ignore_rc=True)


@@ -2219,24 +2219,48 @@
 ovider.json 
+initial_mattertype.json 
 test_auth_cl

fix test_server jsbuild command