2017-10-30 11:25:58 -06:00
|
|
|
#!/usr/bin/python3
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2016-12-01 16:20:04 -07:00
|
|
|
# To pick up any changes to this file without restarting anything:
|
|
|
|
# while true; do ./tools/devel-server.py --once; done
|
|
|
|
# It's kludgy, but it gets the job done.
|
|
|
|
# Feel free to make it suck less, for example using the `tcpserver` program.
|
|
|
|
|
2016-10-15 21:47:50 -06:00
|
|
|
import glob
|
2016-10-22 10:35:55 -06:00
|
|
|
import html
|
2016-10-14 22:26:47 -06:00
|
|
|
import http.server
|
2016-10-22 10:35:55 -06:00
|
|
|
import io
|
2016-10-14 22:26:47 -06:00
|
|
|
import mistune
|
2016-10-20 11:32:21 -06:00
|
|
|
import moth
|
2016-10-16 19:52:09 -06:00
|
|
|
import os
|
2016-10-14 22:26:47 -06:00
|
|
|
import pathlib
|
2016-10-22 10:35:55 -06:00
|
|
|
import shutil
|
2016-10-14 22:26:47 -06:00
|
|
|
import socketserver
|
2016-10-18 09:34:06 -06:00
|
|
|
import sys
|
|
|
|
import traceback
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
try:
|
|
|
|
from http.server import HTTPStatus
|
|
|
|
except ImportError:
|
2016-10-17 15:37:11 -06:00
|
|
|
class HTTPStatus:
|
2016-11-23 14:05:34 -07:00
|
|
|
OK = 200
|
|
|
|
NOT_FOUND = 404
|
|
|
|
INTERNAL_SERVER_ERROR = 500
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2016-11-28 15:17:12 -07:00
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
|
2016-10-17 23:02:05 -06:00
|
|
|
# XXX: This will eventually cause a problem. Do something more clever here.
|
|
|
|
seed = 1
|
|
|
|
|
2017-11-09 14:47:25 -07:00
|
|
|
def page(title, body, baseurl, scripts=[]):
|
2016-10-14 22:26:47 -06:00
|
|
|
return """<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2016-10-24 21:09:04 -06:00
|
|
|
<title>{title}</title>
|
2017-11-09 14:47:25 -07:00
|
|
|
<link rel="stylesheet" href="{baseurl}/files/src/www/res/style.css">
|
2017-10-23 10:01:11 -06:00
|
|
|
{scripts}
|
2016-10-14 22:26:47 -06:00
|
|
|
</head>
|
|
|
|
<body>
|
2016-10-24 21:09:04 -06:00
|
|
|
<h1>{title}</h1>
|
2016-10-16 20:32:00 -06:00
|
|
|
<div id="preview" class="terminal">
|
2016-10-24 21:09:04 -06:00
|
|
|
{body}
|
2016-10-14 22:26:47 -06:00
|
|
|
</div>
|
|
|
|
</body>
|
2017-10-23 10:01:11 -06:00
|
|
|
</html>""".format(
|
|
|
|
title=title,
|
|
|
|
body=body,
|
2017-11-09 14:47:25 -07:00
|
|
|
baseurl=baseurl,
|
2017-10-23 10:01:11 -06:00
|
|
|
scripts="\n".join('<script src="{}"></script>'.format(s) for s in scripts),
|
|
|
|
)
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
|
2016-10-14 22:26:47 -06:00
|
|
|
|
|
|
|
|
2017-02-02 11:17:03 -07:00
|
|
|
# XXX: What horrors did we unleash with our chdir shenanigans that
|
|
|
|
# makes this serve 404 and 500 when we mix in ThreadingMixIn?
|
|
|
|
class ThreadingServer(socketserver.ForkingMixIn, http.server.HTTPServer):
|
2016-10-14 22:26:47 -06:00
|
|
|
pass
|
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
|
2016-10-18 09:34:06 -06:00
|
|
|
class MothHandler(http.server.SimpleHTTPRequestHandler):
|
2016-12-01 16:20:04 -07:00
|
|
|
puzzles_dir = "puzzles"
|
2017-11-09 14:47:25 -07:00
|
|
|
base_url = ""
|
|
|
|
|
|
|
|
def mdpage(self, body, scripts=[]):
|
|
|
|
try:
|
|
|
|
title, _ = body.split('\n', 1)
|
|
|
|
except ValueError:
|
|
|
|
title = "Result"
|
|
|
|
title = title.lstrip("#")
|
|
|
|
title = title.strip()
|
|
|
|
return page(title, mistune.markdown(body, escape=False), self.base_url, scripts=scripts)
|
|
|
|
|
2016-12-01 16:20:04 -07:00
|
|
|
|
2016-10-18 09:34:06 -06:00
|
|
|
def handle_one_request(self):
|
|
|
|
try:
|
|
|
|
super().handle_one_request()
|
|
|
|
except:
|
|
|
|
tbtype, value, tb = sys.exc_info()
|
|
|
|
tblist = traceback.format_tb(tb, None) + traceback.format_exception_only(tbtype, value)
|
2016-10-22 10:35:55 -06:00
|
|
|
payload = ("Traceback (most recent call last)\n" +
|
|
|
|
"".join(tblist[:-1]) +
|
|
|
|
tblist[-1]).encode('utf-8')
|
|
|
|
self.send_response(HTTPStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
self.send_header("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
self.send_header("Content-Length", payload)
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(payload)
|
2016-10-18 09:34:06 -06:00
|
|
|
|
2016-10-14 22:26:47 -06:00
|
|
|
def do_GET(self):
|
|
|
|
if self.path == "/":
|
|
|
|
self.serve_front()
|
2017-11-09 14:47:25 -07:00
|
|
|
elif self.path.startswith("/puzzles/"):
|
|
|
|
self.serve_puzzles(self.path)
|
|
|
|
elif self.path.startswith("/files/"):
|
2016-10-22 10:35:55 -06:00
|
|
|
self.serve_file(self.translate_path(self.path))
|
2016-10-14 22:26:47 -06:00
|
|
|
else:
|
|
|
|
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
|
|
|
|
|
|
|
|
def translate_path(self, path):
|
|
|
|
if path.startswith('/files'):
|
|
|
|
path = path[7:]
|
|
|
|
return super().translate_path(path)
|
|
|
|
|
|
|
|
def serve_front(self):
|
2016-10-22 10:35:55 -06:00
|
|
|
body = """
|
2016-10-14 22:26:47 -06:00
|
|
|
MOTH Development Server Front Page
|
|
|
|
====================
|
|
|
|
|
|
|
|
Yo, it's the front page.
|
|
|
|
There's stuff you can do here:
|
|
|
|
|
2017-11-09 14:47:25 -07:00
|
|
|
* [Available puzzles](puzzles/)
|
|
|
|
* [Raw filesystem view](files/)
|
|
|
|
* [Documentation](files/docs/)
|
|
|
|
* [Instructions](files/docs/devel-server.md) for using this server
|
2016-10-14 22:26:47 -06:00
|
|
|
|
|
|
|
If you use this development server to run a contest,
|
|
|
|
you are a fool.
|
|
|
|
"""
|
2017-11-09 14:47:25 -07:00
|
|
|
payload = self.mdpage(body).encode('utf-8')
|
2016-10-22 10:35:55 -06:00
|
|
|
self.send_response(HTTPStatus.OK)
|
|
|
|
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
|
|
self.send_header("Content-Length", len(payload))
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(payload)
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2017-11-09 14:47:25 -07:00
|
|
|
def serve_puzzles(self, path):
|
2016-10-22 10:35:55 -06:00
|
|
|
body = io.StringIO()
|
2017-11-09 14:47:25 -07:00
|
|
|
path = path.rstrip('/')
|
2016-10-16 20:32:00 -06:00
|
|
|
parts = path.split("/")
|
2017-10-23 10:01:11 -06:00
|
|
|
scripts = []
|
2016-10-22 10:35:55 -06:00
|
|
|
title = None
|
2016-10-23 14:55:35 -06:00
|
|
|
fpath = None
|
|
|
|
points = None
|
2016-10-22 10:35:55 -06:00
|
|
|
cat = None
|
2016-10-23 14:55:35 -06:00
|
|
|
puzzle = None
|
2016-10-22 10:35:55 -06:00
|
|
|
|
|
|
|
try:
|
2016-12-01 16:20:04 -07:00
|
|
|
fpath = os.path.join(self.puzzles_dir, parts[2])
|
2016-10-23 14:55:35 -06:00
|
|
|
points = int(parts[3])
|
2016-10-22 10:35:55 -06:00
|
|
|
except:
|
|
|
|
pass
|
2016-10-18 20:11:20 -06:00
|
|
|
|
2016-10-23 14:55:35 -06:00
|
|
|
if fpath:
|
|
|
|
cat = moth.Category(fpath, seed)
|
|
|
|
if points:
|
2016-11-17 14:24:01 -07:00
|
|
|
puzzle = cat.puzzle(points)
|
2016-10-23 14:55:35 -06:00
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
if not cat:
|
|
|
|
title = "Puzzle Categories"
|
|
|
|
body.write("<ul>")
|
2016-12-01 16:20:04 -07:00
|
|
|
for i in sorted(glob.glob(os.path.join(self.puzzles_dir, "*", ""))):
|
|
|
|
bn = os.path.basename(i.strip('/\\'))
|
2017-11-09 14:47:25 -07:00
|
|
|
body.write('<li><a href="{}/">puzzles/{}/</a></li>'.format(bn, bn))
|
2016-10-22 10:35:55 -06:00
|
|
|
body.write("</ul>")
|
|
|
|
elif not puzzle:
|
2016-10-16 19:52:09 -06:00
|
|
|
# List all point values in a category
|
2016-10-22 10:35:55 -06:00
|
|
|
title = "Puzzles in category `{}`".format(parts[2])
|
|
|
|
body.write("<ul>")
|
2017-02-02 11:34:57 -07:00
|
|
|
for points in cat.pointvals():
|
2017-11-09 14:47:25 -07:00
|
|
|
body.write('<li><a href="{points}/">puzzles/{cat}/{points}/</a></li>'.format(cat=parts[2], points=points))
|
2016-10-22 10:35:55 -06:00
|
|
|
body.write("</ul>")
|
2016-10-23 14:55:35 -06:00
|
|
|
elif len(parts) == 4:
|
2016-10-22 10:35:55 -06:00
|
|
|
# Serve up a puzzle
|
2017-10-23 10:01:11 -06:00
|
|
|
scripts = puzzle.scripts
|
2016-10-22 10:35:55 -06:00
|
|
|
title = "{} puzzle {}".format(parts[2], parts[3])
|
|
|
|
body.write("<h2>Body</h2>")
|
2017-10-30 12:16:23 -06:00
|
|
|
body.write("<div id='body' style='border: solid 1px silver;'>")
|
2016-10-22 10:35:55 -06:00
|
|
|
body.write(puzzle.html_body())
|
2017-10-30 12:16:23 -06:00
|
|
|
body.write("</div>")
|
2016-10-23 14:55:35 -06:00
|
|
|
body.write("<h2>Files</h2>")
|
|
|
|
body.write("<ul>")
|
2017-01-25 17:59:29 -07:00
|
|
|
for name,puzzlefile in sorted(puzzle.files.items()):
|
|
|
|
if puzzlefile.visible:
|
2017-10-03 16:06:44 -06:00
|
|
|
visibility = ''
|
2017-01-25 17:59:29 -07:00
|
|
|
else:
|
2017-10-03 16:06:44 -06:00
|
|
|
visibility = '(unlisted)'
|
2017-11-09 14:47:25 -07:00
|
|
|
body.write('<li><a href="{filename}">{filename}</a> {visibility}</li>'
|
2017-01-25 17:59:29 -07:00
|
|
|
.format(cat=parts[2],
|
|
|
|
points=puzzle.points,
|
|
|
|
filename=name,
|
|
|
|
visibility=visibility))
|
2016-10-23 14:55:35 -06:00
|
|
|
body.write("</ul>")
|
2016-10-22 10:35:55 -06:00
|
|
|
body.write("<h2>Answers</h2>")
|
2017-10-30 12:16:23 -06:00
|
|
|
body.write("<p>Input box (for scripts): <input id='answer' name='a'>")
|
2016-10-22 10:35:55 -06:00
|
|
|
body.write("<ul>")
|
2016-10-24 21:09:04 -06:00
|
|
|
assert puzzle.answers, 'No answers defined'
|
2016-10-22 10:35:55 -06:00
|
|
|
for a in puzzle.answers:
|
|
|
|
body.write("<li><code>{}</code></li>".format(html.escape(a)))
|
|
|
|
body.write("</ul>")
|
2017-01-30 12:13:02 -07:00
|
|
|
body.write("<h2>Authors</h2><p>{}</p>".format(', '.join(puzzle.get_authors())))
|
2016-10-23 14:55:35 -06:00
|
|
|
body.write("<h2>Summary</h2><p>{}</p>".format(puzzle.summary))
|
2016-11-28 15:17:12 -07:00
|
|
|
if puzzle.logs:
|
|
|
|
body.write("<h2>Debug Log</h2>")
|
|
|
|
body.write('<ul class="log">')
|
|
|
|
for l in puzzle.logs:
|
|
|
|
body.write("<li>{}</li>".format(html.escape(l)))
|
|
|
|
body.write("</ul>")
|
2016-10-18 20:11:20 -06:00
|
|
|
elif len(parts) == 5:
|
2016-10-22 10:35:55 -06:00
|
|
|
# Serve up a puzzle file
|
|
|
|
try:
|
|
|
|
pfile = puzzle.files[parts[4]]
|
|
|
|
except KeyError:
|
2016-12-01 16:20:04 -07:00
|
|
|
self.send_error(HTTPStatus.NOT_FOUND, "File not found. Did you add it to the Files: header or puzzle.add_stream?")
|
2016-10-22 10:35:55 -06:00
|
|
|
return
|
|
|
|
ctype = self.guess_type(pfile.name)
|
|
|
|
self.send_response(HTTPStatus.OK)
|
|
|
|
self.send_header("Content-Type", ctype)
|
|
|
|
self.end_headers()
|
|
|
|
shutil.copyfileobj(pfile.stream, self.wfile)
|
|
|
|
return
|
2016-10-18 20:11:20 -06:00
|
|
|
|
2017-11-09 14:47:25 -07:00
|
|
|
payload = page(title, body.getvalue(), self.base_url, scripts=scripts).encode('utf-8')
|
2016-10-19 15:02:38 -06:00
|
|
|
self.send_response(HTTPStatus.OK)
|
2016-10-22 10:35:55 -06:00
|
|
|
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
|
|
self.send_header("Content-Length", len(payload))
|
2016-10-19 15:02:38 -06:00
|
|
|
self.end_headers()
|
2016-10-22 10:35:55 -06:00
|
|
|
self.wfile.write(payload)
|
2016-10-19 15:02:38 -06:00
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
def serve_file(self, path):
|
|
|
|
lastmod = None
|
|
|
|
fspath = pathlib.Path(path)
|
2016-10-18 20:11:20 -06:00
|
|
|
|
2016-10-22 10:35:55 -06:00
|
|
|
if fspath.is_dir():
|
|
|
|
ctype = "text/html; charset=utf-8"
|
|
|
|
payload = self.list_directory(path)
|
|
|
|
# it sends headers but not body
|
|
|
|
shutil.copyfileobj(payload, self.wfile)
|
2016-10-14 22:26:47 -06:00
|
|
|
else:
|
2016-10-22 10:35:55 -06:00
|
|
|
ctype = self.guess_type(path)
|
2016-10-14 22:26:47 -06:00
|
|
|
try:
|
2016-10-22 10:35:55 -06:00
|
|
|
payload = fspath.read_bytes()
|
2016-10-14 22:26:47 -06:00
|
|
|
except OSError:
|
|
|
|
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
|
2016-10-22 10:35:55 -06:00
|
|
|
return
|
2016-10-23 14:55:35 -06:00
|
|
|
if path.endswith(".md"):
|
2016-10-22 10:35:55 -06:00
|
|
|
ctype = "text/html; charset=utf-8"
|
2017-11-09 14:47:25 -07:00
|
|
|
content = self.mdpage(payload.decode('utf-8'))
|
2016-10-22 10:35:55 -06:00
|
|
|
payload = content.encode('utf-8')
|
|
|
|
try:
|
|
|
|
fs = fspath.stat()
|
|
|
|
lastmod = self.date_time_string(fs.st_mtime)
|
|
|
|
except:
|
|
|
|
pass
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2016-10-17 15:37:11 -06:00
|
|
|
self.send_response(HTTPStatus.OK)
|
2016-10-22 10:35:55 -06:00
|
|
|
self.send_header("Content-Type", ctype)
|
|
|
|
self.send_header("Content-Length", len(payload))
|
|
|
|
if lastmod:
|
|
|
|
self.send_header("Last-Modified", lastmod)
|
2016-10-14 22:26:47 -06:00
|
|
|
self.end_headers()
|
2016-10-22 10:35:55 -06:00
|
|
|
self.wfile.write(payload)
|
2016-10-14 22:26:47 -06:00
|
|
|
|
2016-10-17 19:58:51 -06:00
|
|
|
|
2017-11-09 14:47:25 -07:00
|
|
|
def run(address=('127.0.0.1', 8080), once=False):
|
2016-10-14 22:26:47 -06:00
|
|
|
httpd = ThreadingServer(address, MothHandler)
|
2016-10-17 23:20:48 -06:00
|
|
|
print("=== Listening on http://{}:{}/".format(address[0], address[1]))
|
2016-12-01 16:20:04 -07:00
|
|
|
if once:
|
|
|
|
httpd.handle_request()
|
|
|
|
else:
|
|
|
|
httpd.serve_forever()
|
2016-10-14 22:26:47 -06:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-12-01 16:20:04 -07:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="MOTH puzzle development server")
|
2017-11-09 14:47:25 -07:00
|
|
|
parser.add_argument(
|
|
|
|
'--puzzles', default='puzzles',
|
|
|
|
help="Directory containing your puzzles"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--once', default=False, action='store_true',
|
|
|
|
help="Serve one page, then exit. For debugging the server."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--bind', default="127.0.0.1:8080",
|
|
|
|
help="Bind to ip:port"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--base', default="",
|
|
|
|
help="Base URL to this server, for reverse proxy setup"
|
|
|
|
)
|
2016-12-01 16:20:04 -07:00
|
|
|
args = parser.parse_args()
|
2017-11-09 14:47:25 -07:00
|
|
|
addr, port = args.bind.split(":")
|
|
|
|
port = int(port)
|
2016-12-01 16:20:04 -07:00
|
|
|
MothHandler.puzzles_dir = args.puzzles
|
2017-11-09 14:47:25 -07:00
|
|
|
MothHandler.base_url = args.base
|
|
|
|
run(address=(addr, port), once=args.once)
|