-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.py
executable file
·45 lines (34 loc) · 1.81 KB
/
run.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
#!/usr/bin/python2
"""emulate this bash script, in a cross-platform and working-directory-agnostic way:
```bash
src/backend/server.py &
firefox http://localhost:8080/
```
"""
# TODO: silence messages from the webrowser (they are very distracting)
import sys
from os.path import dirname, abspath, join as pathjoin
import os
import threading, webbrowser, time
import subprocess
#'working directory': not the system working directory, but the directory this program is in (so that we can be run from anywhere and$
PROJECT_ROOT = dirname(abspath(__file__)) #run.py should always sit in the project root
def browse():
time.sleep(2) #give time for the server to spin up
#webbrowser.open('http://127.0.0.1:8080') #the URL here is hardcoded and needs to match what server.py spins up on
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
debug = 'debug'
else:
debug = ''
PYTHON = sys.executable # simply use the same python that called the run.py script.
browser = threading.Thread(target=browse)
browser.start() #open a browser to the webapp's page (nonblocking)
# this is glitchy: depending on the mixture of close buttons and ctrl-cs sent
# even though the script we're emulating runs the server in the background
# it is cleaner to run everything else in the background and run the server on the mainthread
# because call() hooks SIGINT (or your local system's favourite shutdown signal)
# and politely but firmly kills the server no matter how run.py ends
subprocess.call([PYTHON, pathjoin(PROJECT_ROOT, "src", "backend", "server.py"), debug])
# TODO(kousu): only browser.start() if the server comes up (requires some kind of pegging; ugh)
# TODO(kousu): figure out why the browser opens twice (sometimes) if you press ctrl-c or the server fails