2009-09-29 15:36:25 -06:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import optparse
|
|
|
|
import config
|
|
|
|
|
|
|
|
p = optparse.OptionParser()
|
|
|
|
p.add_option('-p', '--puzzles', dest='puzzles', default='puzzles',
|
|
|
|
help='Directory containing puzzles')
|
|
|
|
p.add_option('-w', '--htmldir', dest='htmldir', default='puzzler',
|
|
|
|
help='Directory to write HTML puzzle tree')
|
|
|
|
p.add_option('-k', '--keyfile', dest='keyfile', default='puzzler.keys',
|
|
|
|
help='Where to write keys')
|
|
|
|
|
|
|
|
opts, args = p.parse_args()
|
|
|
|
|
|
|
|
keys = []
|
|
|
|
|
|
|
|
for cat in os.listdir(opts.puzzles):
|
|
|
|
dirname = os.path.join(opts.puzzles, cat)
|
|
|
|
for points in os.listdir(dirname):
|
|
|
|
pointsdir = os.path.join(dirname, points)
|
|
|
|
outdir = os.path.join(opts.htmldir, cat, points)
|
2009-10-01 18:33:21 -06:00
|
|
|
try:
|
|
|
|
os.makedirs(outdir)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2009-09-29 15:36:25 -06:00
|
|
|
|
|
|
|
readme = ''
|
|
|
|
files = []
|
|
|
|
for fn in os.listdir(pointsdir):
|
|
|
|
path = os.path.join(pointsdir, fn)
|
|
|
|
if fn == 'key':
|
|
|
|
key = open(path, encoding='utf-8').readline().strip()
|
|
|
|
keys.append((cat, points, key))
|
|
|
|
elif fn == 'index.html':
|
|
|
|
readme = open(path, encoding='utf-8').read()
|
2009-10-01 18:33:21 -06:00
|
|
|
elif fn.endswith('~'):
|
|
|
|
pass
|
2009-09-29 15:36:25 -06:00
|
|
|
else:
|
|
|
|
files.append((fn, path))
|
|
|
|
|
|
|
|
title = '%s for %s points' % (cat, points)
|
|
|
|
f = open(os.path.join(outdir, 'index.html'), 'w', encoding='utf-8')
|
|
|
|
f.write('''<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<!DOCTYPE html PUBLIC
|
|
|
|
"-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
|
|
<head>
|
|
|
|
<title>%(title)s</title>
|
|
|
|
<link rel="stylesheet" href="%(css)s" type="text/css" />
|
2009-10-08 14:32:33 -06:00
|
|
|
<script type="text/javascript">
|
|
|
|
function readCookie(key) {
|
|
|
|
var s = key + '=';
|
|
|
|
var toks = document.cookie.split(';');
|
|
|
|
for (var i = 0; i < toks.length; i++) {
|
|
|
|
var tok = toks[i];
|
|
|
|
while (tok.charAt(0) == ' ') {
|
|
|
|
tok = tok.substring(1, tok.length);
|
|
|
|
}
|
|
|
|
if (tok.indexOf(s) == 0) {
|
|
|
|
return tok.substring(s.length, tok.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTeamInfo() {
|
|
|
|
team = readCookie('team');
|
|
|
|
passwd = readCookie('passwd');
|
|
|
|
if (team != null) {
|
2009-10-08 14:33:43 -06:00
|
|
|
document.getElementById("form").t.value = team;
|
2009-10-08 14:32:33 -06:00
|
|
|
}
|
|
|
|
if (passwd != null) {
|
2009-10-08 14:33:43 -06:00
|
|
|
document.getElementById("form").w.value = passwd;
|
2009-10-08 14:32:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
window.onload = getTeamInfo;
|
|
|
|
</script>
|
2009-09-29 15:36:25 -06:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>%(title)s</h1>
|
|
|
|
''' % {'title': title,
|
|
|
|
'css': config.css})
|
|
|
|
if readme:
|
|
|
|
f.write('<div class="readme">%s</div>\n' % readme)
|
|
|
|
if files:
|
|
|
|
f.write('<ul>\n')
|
|
|
|
for fn, path in files:
|
|
|
|
shutil.copy(path, outdir)
|
2009-10-05 11:03:27 -06:00
|
|
|
if not fn.startswith(','):
|
|
|
|
f.write('<li><a href="%s">%s</a></li>\n' % (fn, fn))
|
2009-09-29 15:36:25 -06:00
|
|
|
f.write('</ul>\n')
|
|
|
|
f.write('''
|
2009-10-08 14:32:33 -06:00
|
|
|
<form id="form" action="%(cgi)s" method="post">
|
2009-09-30 13:58:16 -06:00
|
|
|
<fieldset>
|
|
|
|
<legend>Your answer:</legend>
|
|
|
|
<input type="hidden" name="c" value="%(cat)s" />
|
|
|
|
<input type="hidden" name="p" value="%(points)s" />
|
|
|
|
Team: <input name="t" /><br />
|
|
|
|
Password: <input type="password" name="w" /><br />
|
|
|
|
Key: <input name="k" /><br />
|
|
|
|
<input type="submit" />
|
|
|
|
</fieldset>
|
2009-09-29 15:36:25 -06:00
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
''' % {'cgi': config.get('puzzler', 'cgi_url'),
|
|
|
|
'cat': cat,
|
|
|
|
'points': points})
|
|
|
|
|
|
|
|
f = open(opts.keyfile, 'w', encoding='utf-8')
|
|
|
|
for key in keys:
|
|
|
|
f.write('%s\t%s\t%s\n' % key)
|
|
|
|
|