bootstrap   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 10

3 Functions

Rating   Name   Duplication   Size   Complexity  
B exec_in_env() 0 26 6
A check_call() 0 3 1
A main() 0 28 3
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([join(bin_path, "pip"), "install", "jinja2", "tox"])
42
    python_executable = join(bin_path, "python")
43
    if not os.path.exists(python_executable):
44
        python_executable += '.exe'
45
46
    print("Re-executing with: {0}".format(python_executable))
47
    print("+ exec", python_executable, __file__, "--no-env")
48
    os.execv(python_executable, [python_executable, __file__, "--no-env"])
49
50
def main():
51
    import jinja2
52
53
    print("Project path: {0}".format(base_path))
54
55
    jinja = jinja2.Environment(
56
        loader=jinja2.FileSystemLoader(join(base_path, "ci", "templates")),
57
        trim_blocks=True,
58
        lstrip_blocks=True,
59
        keep_trailing_newline=True
60
    )
61
62
    tox_environments = [
63
        line.strip()
64
        # 'tox' need not be installed globally, but must be importable
65
        # by the Python that is running this script.
66
        # This uses sys.executable the same way that the call in
67
        # cookiecutter-pylibrary/hooks/post_gen_project.py
68
        # invokes this bootstrap.py itself.
69
        for line in subprocess.check_output([sys.executable, '-m', 'tox', '--listenvs'], universal_newlines=True).splitlines()
70
    ]
71
    tox_environments = [line for line in tox_environments if line.startswith('py')]
72
73
    for name in os.listdir(join("ci", "templates")):
74
        with open(join(base_path, name), "w") as fh:
75
            fh.write(jinja.get_template(name).render(tox_environments=tox_environments))
76
        print("Wrote {}".format(name))
77
    print("DONE.")
78
79
80
if __name__ == "__main__":
81
    args = sys.argv[1:]
82
    if args == ["--no-env"]:
83
        main()
84
    elif not args:
85
        exec_in_env()
86
    else:
87
        print("Unexpected arguments {0}".format(args), file=sys.stderr)
88
        sys.exit(1)
89
90