bootstrap.exec_in_env()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 28
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nop 0
dl 0
loc 28
rs 8.3706
c 0
b 0
f 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 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(
41
            [join(bin_path, "pip"), "install", "jinja2", "tox", "matrix"]
42
        )
43
    python_executable = join(bin_path, "python")
44
    if not os.path.exists(python_executable):
45
        python_executable += ".exe"
46
47
    print("Re-executing with: {0}".format(python_executable))
48
    print("+ exec", python_executable, __file__, "--no-env")
49
    os.execv(python_executable, [python_executable, __file__, "--no-env"])
50
51
52
def main():
53
    import jinja2
54
    import matrix
55
56
    print("Project path: {0}".format(base_path))
57
58
    jinja = jinja2.Environment(
59
        loader=jinja2.FileSystemLoader(join(base_path, "ci", "templates")),
60
        trim_blocks=True,
61
        lstrip_blocks=True,
62
        keep_trailing_newline=True,
63
    )
64
65
    tox_environments = {}
66
    for (alias, conf) in matrix.from_file(
67
        join(base_path, "setup.cfg")
68
    ).items():
69
        python = conf["python_versions"]
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