forked from Kitware/tangelo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtangelo.in
executable file
·143 lines (118 loc) · 3.44 KB
/
tangelo.in
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!${PYTHON_EXECUTABLE}
import os
import platform
import signal
import subprocess
import sys
import time
def start():
sys.stderr.write("starting tangelo...")
if os.path.exists(pidfile):
sys.stderr.write("failed (already seems to be running)\n")
return 1
if daemonize:
screen = open(os.devnull)
else:
screen = None
os.chdir(path)
args = [sys.executable, cherry,
"-i", "tangelo",
"-c", "server.conf",
"-P", path]
if daemonize:
args += ["-d", "-p", pidfile]
if not daemonize:
sys.stderr.write("\n")
retval = subprocess.call(args, stdout=screen, stderr=screen)
if daemonize:
f = open(logfile)
f.seek(0, 2)
done = False
while not done:
time.sleep(1)
cur_pos = f.tell()
line = f.readline()
if not line:
f.seek(cur_pos)
else:
if "Bus STARTED" in line:
retval = 0
sys.stderr.write("success (serving on %s)\n" % location)
done = True
elif "Error" in line:
retval = 1
sys.stderr.write("failed (check tangelo.log for reason)\n")
done = True
elif "Serving on" in line:
location = line.split("Serving on")[1].strip()
return retval
def stop():
retval = 0
sys.stderr.write("stopping tangelo...")
if os.path.exists(pidfile):
f = open(pidfile)
try:
pid = int(f.read())
except ValueError:
if report:
sys.stderr.write("failed (tangelo.pid does not contain a valid process id)\n")
return 1
try:
os.kill(pid, signal.SIGKILL)
os.remove(pidfile)
except OSError:
sys.stderr.write("failed (could not kill process %d)\n" % (pid))
retval = 1
if retval == 0:
sys.stderr.write("success\n")
return retval
def restart():
stopval = stop()
if stopval == 0:
return start()
else:
return stopval
def usage():
sys.stderr.write("usage: tangelo [-d|-D] <start|stop|restart>\n")
# Read the action argument.
if len(sys.argv) < 2:
usage()
sys.exit(1)
if len(sys.argv) > 2:
daemonize_flag = sys.argv[1]
action = sys.argv[2]
if daemonize_flag not in ["-d", "-D"]:
usage()
sys.exit(1)
else:
action = sys.argv[1]
daemonize_flag = None
# Configured variables.
cherry = "@CherryPy_EXECUTABLE@"
# Detect operating system (and OSX version, if applicable).
os_name = platform.system()
if os_name == "Darwin":
version = map(int, platform.mac_ver()[0].split("."))
daemonize = daemonize_flag != "-d" and not(os_name == "Windows" or (os_name == "Darwin" and version[1] == 6))
# Determine the path to this script.
path = os.path.dirname(os.path.abspath(sys.argv[0]))
pidfile = path + "/tangelo.pid"
logfile = path + "/tangelo.log"
# Dispatch on action argument.
code = 1
if action == "start":
code = start()
elif action == "stop":
if not daemonize:
sys.stderr.write("error: stop action not supported on this platform\n")
sys.exit(1)
code = stop()
elif action == "restart":
if not daemonize:
sys.stderr.write("error: restart action not supported on this platform\n")
sys.exit(1)
code = restart()
else:
usage()
code = 1
sys.exit(code)