bootstrap.main()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 39
rs 8.2266
c 0
b 0
f 0
cc 6
nop 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
from __future__ import absolute_import
4
from __future__ import print_function
5
from __future__ import unicode_literals
6
7
import os
8
import subprocess
9
import sys
10
from os.path import abspath
11
from os.path import dirname
12
from os.path import exists
13
from os.path import join
14
15
base_path = dirname(dirname(abspath(__file__)))
16
17
18
def check_call(args):
19
    print("+", *args)
20
    subprocess.check_call(args)
21
22
23
def exec_in_env():
24
    env_path = join(base_path, ".tox", "bootstrap")
25
    if sys.platform == "win32":
26
        bin_path = join(env_path, "Scripts")
27
    else:
28
        bin_path = join(env_path, "bin")
29
    if not exists(env_path):
30
        import subprocess
31
32
        print("Making bootstrap env in: {0} ...".format(env_path))
33
        try:
34
            check_call([sys.executable, "-m", "venv", env_path])
35
        except subprocess.CalledProcessError:
36
            try:
37
                check_call([sys.executable, "-m", "virtualenv", env_path])
38
            except subprocess.CalledProcessError:
39
                check_call(["virtualenv", env_path])
40
        print("Installing `jinja2` into bootstrap environment...")
41
        check_call(
42
            [join(bin_path, "pip"), "install", "jinja2", "tox", "matrix"]
43
        )
44
    python_executable = join(bin_path, "python")
45
    if not os.path.exists(python_executable):
46
        python_executable += ".exe"
47
48
    print("Re-executing with: {0}".format(python_executable))
49
    print("+ exec", python_executable, __file__, "--no-env")
50
    os.execv(python_executable, [python_executable, __file__, "--no-env"])
51
52
53
def main():
54
    import jinja2
55
    import matrix
56
57
    print("Project path: {0}".format(base_path))
58
59
    jinja = jinja2.Environment(
60
        loader=jinja2.FileSystemLoader(join(base_path, "ci", "templates")),
61
        trim_blocks=True,
62
        lstrip_blocks=True,
63
        keep_trailing_newline=True,
64
    )
65
66
    tox_environments = {}
67
    for (alias, conf) in matrix.from_file(
68
        join(base_path, "setup.cfg")
69
    ).items():
70
        deps = conf["dependencies"]
71
        tox_environments[alias] = {
72
            "deps": deps.split(),
73
        }
74
        if "coverage_flags" in conf:
75
            cover = {"false": False, "true": True}[
76
                conf["coverage_flags"].lower()
77
            ]
78
            tox_environments[alias].update(cover=cover)
79
        if "environment_variables" in conf:
80
            env_vars = conf["environment_variables"]
81
            tox_environments[alias].update(env_vars=env_vars.split())
82
83
    for name in os.listdir(join("ci", "templates")):
84
        with open(join(base_path, name), "w") as fh:
85
            fh.write(
86
                jinja.get_template(name).render(
87
                    tox_environments=tox_environments
88
                )
89
            )
90
        print("Wrote {}".format(name))
91
    print("DONE.")
92
93
94
if __name__ == "__main__":
95
    args = sys.argv[1:]
96
    if args == ["--no-env"]:
97
        main()
98
    elif not args:
99
        exec_in_env()
100
    else:
101
        print("Unexpected arguments {0}".format(args), file=sys.stderr)
102
        sys.exit(1)
103