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

_dname = os.path.dirname

REPO_ROOT = _dname(_dname(_dname(os.path.abspath(__file__))))
os.chdir(REPO_ROOT)


def run(command):
    return check_call(command, shell=True)


try:
    # Has the form "major.minor"
    python_version = os.environ['PYTHON_VERSION']
except KeyError:
    python_version = '.'.join([str(i) for i in sys.version_info[:2]])

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument(
        '-e', '--extras', help='Install extras_require along with normal install'
    )
    args = parser.parse_args()
    run('pip install -r requirements-test.txt')
    run('pip install coverage')
    if os.path.isdir('dist') and os.listdir('dist'):
        shutil.rmtree('dist')
    run('python setup.py bdist_wheel')
    wheel_dist = os.listdir('dist')[0]
    package = os.path.join('dist', wheel_dist)
    if args.extras:
        package = "'%s[%s]'" % (package, args.extras)
    run('pip install %s' % package)
