forked from acm-uiuc/chroma-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·96 lines (81 loc) · 2.71 KB
/
server.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
#!./virt_env/bin/python
import os
import cherrypy as http
import subprocess
import signal
import simplejson as json
from mako.template import Template
from mako.lookup import TemplateLookup
lookup = TemplateLookup(directories=['templates'])
p = None
current = "Off"
def sortDictByValueKey(dict, valuekey):
sortedValueKeys = {}
for key in dict.iterkeys():
if(valuekey == "name"):
value = dict[key][valuekey] + "1"
else:
value = dict[key][valuekey] + dict[key]["name"]
while(value in sortedValueKeys.keys()):
if(valuekey == "name"):
value = value + "1"
else:
value = value + dict[key]["name"]
print value
sortedValueKeys[value] = key
sortedKeys = []
for key in sorted(sortedValueKeys.iterkeys()):
sortedKeys.append(sortedValueKeys[key])
return sortedKeys
class Serv:
@http.expose
def index(self):
global current
animations = os.listdir('./animations/')
aData = {}
for a in animations:
json_data=open('animations/%s/manifest.json'%a)
data = json.load(json_data)
aData[a] = data
json_data.close()
sortedKeys = sortDictByValueKey(aData, "name")
tmpl = lookup.get_template("index.html")
return tmpl.render(animations=aData,current=current,animationKeys=sortedKeys)
@http.expose
@http.tools.allow(methods=['POST'])
def set(self, animation):
global p
global current
if p:
os.kill(p.pid, signal.SIGTERM)
if animation == "Off":
current = "Off"
return "Turned off"
animations = os.listdir('./animations/')
if animation in animations:
p = subprocess.Popen(['env','PYTHONPATH=./osc:$PYTHONPATH','python','animations/%s/main.py'%animation])
current = animation
return "Animation %s started"%animation
return "Unknown animation %s"%animation
@http.expose
@http.tools.allow(methods=['POST'])
def pull(self):
p = os.popen('git pull origin master','r')
output = ""
while 1:
line = p.readline()
if not line: break
output += line + '\n'
return output
if __name__ == "__main__":
config = {
'/static':{
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
}
}
http.tree.mount(Serv(), '/', config = config)
http.config.update( {'server.socket_host':"0.0.0.0", 'server.socket_port':8181 } )
http.engine.signal_handler.subscribe()
http.engine.start()
http.engine.block()