#!.tox/bootstrap/bin/python
import re
from itertools import product, chain
from jinja2 import FileSystemLoader, Environment
jinja = Environment(loader=FileSystemLoader('.'), trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True)

########################################################
################# EDIT AFTER THIS LINE #################
pythons = [
    '2.6',
    '2.7',
    '3.3',
    '3.4',
    'pypy',
]
deps = [ # set to [''] if you have no deps
    'Django==1.4.13',
    'Django==1.5.8',
    'Django==1.6.5',
    'https://www.djangoproject.com/download/1.7.b4/tarball/',
]
covers = [True, False]
envs = [''] # could be some env vars that activate certain features
skips = list(chain(
    # skip 1.4 on py3
    product(
        ['3.3', '3.4'],
        [dep for dep in deps if '1.4' in dep],
        covers,
        envs
    ),
    # skip 1.7 on py2.6
    product(
        ['2.6'],
        [dep for dep in deps if '1.7' in dep],
        covers,
        envs
    ),
))
############# DO NOT EDIT BELOW THIS LINE ##############
########################################################

# the list of environments is the product of python versions,
# dependencies, coverage switches (on/off) and
# environment variables

matrix = {}
for python, dep, cover, env in product(pythons, deps, covers, envs):
    if (python, dep, cover, env) not in skips:
        name = '-'.join(filter(None, (  # mangle the python version, deps,
                                        # cover flags and env vars into
                                        # something pretty
            python,
            '-'.join(re.sub(
                r'[A-Za-z/:.>]+[/=]+(.*?)(,.*|/.*)?$',
                r'\1',
                dep
            ).split(' ')), # strip useless characters
            '' if cover else 'nocover',
            env and env.lower().replace('_', ''),
        )))

        matrix[name] = {
            'python': 'python' + python if 'py' not in python else python,
            'deps': dep.split(),
            'cover': cover,
            'env': env,
        }

with open('tox.ini', 'w') as fh:
    fh.write(jinja.get_template('tox.tmpl.ini').render(matrix=matrix))

with open('.travis.yml', 'w') as fh:
    fh.write(jinja.get_template('.travis.tmpl.yml').render(matrix=matrix))

print("DONE.")