-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlaunch.py
60 lines (47 loc) · 1.48 KB
/
launch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Virtualenv and launcher script rolled into one.
This is copied in part from https://docs.python.org/3/library/venv.html.
"""
import os.path
import sys
import time
import venv
from subprocess import Popen
win32 = os.name == 'nt'
class ExtendedEnvBuilder(venv.EnvBuilder):
def post_setup(self, context):
# install the bot-specific packages
if win32:
pip = "./venv/Scripts/pip.exe"
else:
pip = "./venv/bin/pip"
proc = Popen([pip, "install", "-U", "-r", "requirements.txt"])
proc.communicate()
def run():
env = os.environ.copy()
env["PYTHONPATH"] = os.getcwd()
if win32:
path = "./venv/Scripts/python.exe"
else:
path = "venv/bin/python3"
print("\n\nSpawning (w/ autoreboot): {} {}\n\n".format(os.path.abspath(path), "jetbot/bot.py"))
proc = Popen([os.path.abspath(path), "jetbot/bot.py", *sys.argv[1:]],
cwd=os.getcwd(), env=env)
result = proc.communicate()
print("\n\nBot died, restarting.")
time.sleep(1)
if __name__ == "__main__":
print("Need to create environment:", not os.path.exists("./venv"))
if not os.path.exists("./venv"):
print("Creating a new virtual environment...")
builder = ExtendedEnvBuilder(with_pip=True)
builder.create("./venv")
if win32:
while True:
try:
run()
except (KeyboardInterrupt, EOFError):
break
else:
run()
print("Bye!")