bootstrap   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 13

3 Functions

Rating   Name   Duplication   Size   Complexity  
B exec_in_env() 0 26 6
A check_call() 0 3 1
B main() 0 32 6
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 sys
9
from os.path import abspath
10
from os.path import dirname
11
from os.path import exists
12
from os.path import join
13
14
base_path = dirname(dirname(abspath(__file__)))
15
16
17
def check_call(args):
18
    print("+", *args)
19
    subprocess.check_call(args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable subprocess does not seem to be defined.
Loading history...
20
21
22
def exec_in_env():
23
    env_path = join(base_path, ".tox", "bootstrap")
24
    if sys.platform == "win32":
25
        bin_path = join(env_path, "Scripts")
26
    else:
27
        bin_path = join(env_path, "bin")
28
    if not exists(env_path):
29
        import subprocess
30
31
        print("Making bootstrap env in: {0} ...".format(env_path))
32
        try:
33
            check_call([sys.executable, "-m", "venv", env_path])
34
        except subprocess.CalledProcessError:
35
            try:
36
                check_call([sys.executable, "-m", "virtualenv", env_path])
37
            except subprocess.CalledProcessError:
38
                check_call(["virtualenv", env_path])
39
        print("Installing `jinja2` into bootstrap environment...")
40
        check_call([join(bin_path, "pip"), "install", "jinja2", "tox", "matrix"])
41
    python_executable = join(bin_path, "python")
42
    if not os.path.exists(python_executable):
43
        python_executable += ".exe"
44
45
    print("Re-executing with: {0}".format(python_executable))
46
    print("+ exec", python_executable, __file__, "--no-env")
47
    os.execv(python_executable, [python_executable, __file__, "--no-env"])
48
49
50
def main():
51
    import jinja2
52
    import matrix
53
54
    print("Project path: {0}".format(base_path))
55
56
    jinja = jinja2.Environment(
57
        loader=jinja2.FileSystemLoader(join(base_path, "ci", "templates")),
58
        trim_blocks=True,
59
        lstrip_blocks=True,
60
        keep_trailing_newline=True,
61
    )
62
63
    tox_environments = {}
64
    for (alias, conf) in matrix.from_file(join(base_path, "setup.cfg")).items():
65
        python = conf["python_versions"]
66
        deps = conf["dependencies"]
67
        tox_environments[alias] = {
68
            "deps": deps.split(),
69
        }
70
        if "coverage_flags" in conf:
71
            cover = {"false": False, "true": True}[conf["coverage_flags"].lower()]
72
            tox_environments[alias].update(cover=cover)
73
        if "environment_variables" in conf:
74
            env_vars = conf["environment_variables"]
75
            tox_environments[alias].update(env_vars=env_vars.split())
76
77
    for name in os.listdir(join("ci", "templates")):
78
        with open(join(base_path, name), "w") as fh:
79
            fh.write(jinja.get_template(name).render(tox_environments=tox_environments))
80
        print("Wrote {}".format(name))
81
    print("DONE.")
82
83
84
if __name__ == "__main__":
85
    args = sys.argv[1:]
86
    if args == ["--no-env"]:
87
        main()
88
    elif not args:
89
        exec_in_env()
90
    else:
91
        print("Unexpected arguments {0}".format(args), file=sys.stderr)
92
        sys.exit(1)
93