Passed
Push — web-timed ( cfeda2...1d9e3e )
by Matt
01:11
created

tests.launch.run()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
rs 9.9
c 0
b 0
f 0
cc 2
nop 0
1
"""
2
Virtualenv and launcher script rolled into one.
3
4
This is copied in part from https://docs.python.org/3/library/venv.html.
5
"""
6
7
import os.path
8
import sys
9
import venv
10
from subprocess import Popen
11
12
win32 = os.name == 'nt'
13
14
15
class ExtendedEnvBuilder(venv.EnvBuilder):
16
    def post_setup(self, context):
17
        # install the bot-specific packages
18
        if win32:
19
            pip = "./venv/Scripts/pip.exe"
20
        else:
21
            pip = "./venv/bin/pip"
22
        proc = Popen([pip, "install", "-Ue", "../[audio]"])
23
        proc.communicate()
24
25
26
def run():
27
    env = os.environ.copy()
28
    env["PYTHONPATH"] = os.getcwd()
29
    if win32:
30
        path = "./venv/Scripts/python.exe"
31
    else:
32
        path = "./venv/bin/python3"
33
34
    print("\n\nSpawning: {} {}\n\n".format(os.path.abspath(path), sys.argv[1]))
35
    proc = Popen([os.path.abspath(path), "{}".format(sys.argv[1])],
36
                 cwd=os.getcwd(), env=env)
37
38
    proc.communicate()
39
40
41
if __name__ == "__main__":
42
    if len(sys.argv) == 1:
43
        sys.argv.append("home.py")
44
45
    print("Need to create environment:", not os.path.exists("./venv"))
46
    if not os.path.exists("./venv"):
47
        print("Creating a new virtual environment...")
48
        builder = ExtendedEnvBuilder(with_pip=True)
49
        builder.create("./venv")
50
51
    while True:
52
        run()
53