-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacescanner_launcher.py
executable file
·128 lines (100 loc) · 3.41 KB
/
spacescanner_launcher.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
import os, subprocess, threading, webbrowser, time
from sys import path, version, executable
SPACESCANNER_PATH = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "source")
path.append(SPACESCANNER_PATH)
from util import *
isServerStarted = False
glock = threading.Lock()
DEFAULT_PORT = 19000
###########################################
def importsOk():
psutilModuleOK = True
try:
import psutil #@UnusedImport
except ImportError:
psutilModuleOK = False
if not psutilModuleOK:
print("Cannot start SpaceScanner: psutil module not found")
if isWindows():
installStr = "Run:\n\tpip install psutil"
else:
installStr = "Run:\n\tsudo apt-get install python-psutil"
print (installStr)
return False
return True
###########################################
def isProcessRunningPosix(names):
myPid = os.getpid()
output = subprocess.check_output(["ps", "x"])
for line in output.splitlines():
for name in names:
if line.find(name) == -1: continue
try:
pid = int(line.split()[0])
if pid == myPid: continue
except:
continue
return True
return False
def isProcessRunningWindows(names):
# Finding process info on Windows all versions is too complicated
# without Python extension packages. Leave it for now.
return False
def isProcessRunning(names):
if os.name == "posix":
return isProcessRunningPosix(names)
else:
return isProcessRunningWindows(names)
###########################################
def launchBrowser():
# wait for server to start starting...
while True:
with glock:
doBreak = isServerStarted
if doBreak: break
time.sleep(0.1)
# wait for server to finish starting
time.sleep(0.2)
# open the web browser (Firefox in Linux, user's preferred on Windows)
if isWindows():
controller = webbrowser.get('windows-default')
else:
# will return default system's browser if Firefox is not installed
controller = webbrowser.get('Firefox')
url = "http://localhost:{}".format(DEFAULT_PORT)
controller.open_new_tab(url)
#######################################
def main():
global isServerStarted
if not version.startswith("2.7"):
print("You are using Python version {0}".format(version[:5]))
print("For SpaceScanner only version 2.7 is supported")
if not importsOk():
exit(1)
try:
browserThread = threading.Thread(target=launchBrowser)
browserThread.start()
if not isProcessRunning(['spacescanner.py']):
print("Launching SpaceScanner...")
with glock:
isServerStarted = True
if isWindows():
time.sleep(2) # just wait, since fork is not supported
doExec = True
else:
doExec = (os.fork() == 0)
if doExec:
os.execl(executable, executable,
* [os.path.join(SPACESCANNER_PATH, 'spacescanner.py'), 'web'])
else:
with glock:
isServerStarted = True
browserThread.join()
except Exception as e:
print("Something went wrong:")
print(e)
#######################################
if __name__ == '__main__':
main()