forked from karpathy/ulogme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ulogme_serve.py
132 lines (115 loc) · 4.14 KB
/
ulogme_serve.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
129
130
131
132
# vim:set ff=unix tabstop=4 shiftwidth=4 expandtab:
# Python 2 compatibility
from __future__ import print_function
from __future__ import absolute_import
import six.moves.socketserver
import six.moves.SimpleHTTPServer
import sys
import cgi
import os
import logging
from export_events import updateEvents
from rewind7am import rewindTime
# Port settings
IP = "127.0.0.1"
if len(sys.argv) > 1:
PORT = int(sys.argv[1])
else:
PORT = 8124
# serve render/ folder, not current folder
rootdir = os.getcwd()
os.chdir('render')
def get_status_output(cmd):
"""Return (status, output) of executing cmd in a shell."""
pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
text = pipe.read()
sts = pipe.close()
if sts is None:
sts = 0
if text and text[-1:] == '\n':
text = text[:-1]
return sts, text
# Custom handler
class CustomHandler(six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# default behavior
try:
six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
except Exception as e:
self.send_response(500)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(e)
def do_POST(self):
form = cgi.FieldStorage(
fp = self.rfile,
headers = self.headers,
environ = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type']}
)
result = 'NOT_UNDERSTOOD'
if self.path == '/refresh':
try:
# recompute jsons. We have to pop out to root from render directory
# temporarily. It's a little ugly
refresh_time = form.getvalue('time')
os.chdir(rootdir) # pop out
updateEvents() # defined in export_events.py
except Exception as e:
# TODO: Should we make the errors visible?
logging.error(e)
finally:
os.chdir('render') # go back to render
result = 'OK'
if self.path == '/addnote':
try:
# add note at specified time and refresh
note = form.getvalue('note')
note_time = form.getvalue('time')
os.chdir(rootdir) # pop out
cmd_status, cmd_stdout = get_status_output('echo %s | ./note.sh %s' % (note, note_time))
updateEvents() # defined in export_events.py
except Exception as e:
# TODO: Should we make the errors visible?
logging.error(e)
finally:
os.chdir('render') # go back to render
result = 'OK'
if self.path == '/blog':
try:
# add note at specified time and refresh
post = form.getvalue('post')
if post is None:
post = ''
post_time = int(form.getvalue('time'))
os.chdir(rootdir) # pop out
trev = rewindTime(post_time)
with open('logs/blog_%d.txt' % (post_time, ), 'w') as bf:
bf.write(post)
updateEvents() # defined in export_events.py
except Exception as e:
# TODO: Should we make the errors visible?
logging.error(e)
finally:
os.chdir('render') # go back to render
result = 'OK'
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(result.encode("utf8"))
six.moves.socketserver.ThreadingTCPServer.allow_reuse_address = True
httpd = None
logging.info("Serving ulogme, see it on http://localhost:%d" % PORT)
try:
httpd = six.moves.socketserver.ThreadingTCPServer((IP, PORT), CustomHandler)
httpd.serve_forever()
except Exception as e:
logging.error(e)
raise
finally:
if httpd:
try:
logging.info("Closing the HTTP server.")
httpd.server_close()
except Exception as e:
logging.error(e)
raise