2017-01-05 16:50:41 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import binascii
|
|
|
|
import hashlib
|
|
|
|
import io
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import moth
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import zipfile
|
|
|
|
|
2018-07-13 16:35:35 -06:00
|
|
|
SEEDFN = "SEED"
|
|
|
|
|
|
|
|
|
2017-01-05 16:50:41 -07:00
|
|
|
def write_kv_pairs(ziphandle, filename, kv):
|
|
|
|
""" Write out a sorted map to file
|
|
|
|
:param ziphandle: a zipfile object
|
|
|
|
:param filename: The filename to write within the zipfile object
|
|
|
|
:param kv: the map to write out
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
filehandle = io.StringIO()
|
|
|
|
for key in sorted(kv.keys()):
|
2018-07-13 16:51:49 -06:00
|
|
|
if isinstance(kv[key], list):
|
2017-01-05 16:50:41 -07:00
|
|
|
for val in kv[key]:
|
2017-01-23 09:35:38 -07:00
|
|
|
filehandle.write("%s %s\n" % (key, val))
|
2017-01-05 16:50:41 -07:00
|
|
|
else:
|
2017-01-23 09:35:38 -07:00
|
|
|
filehandle.write("%s %s\n" % (key, kv[key]))
|
2017-01-05 16:50:41 -07:00
|
|
|
filehandle.seek(0)
|
|
|
|
ziphandle.writestr(filename, filehandle.read())
|
2018-07-13 16:35:35 -06:00
|
|
|
|
|
|
|
|
2017-01-19 16:50:21 -07:00
|
|
|
def escape(s):
|
|
|
|
return s.replace('&', '&').replace('<', '<').replace('>', '>')
|
2018-07-13 16:35:35 -06:00
|
|
|
|
|
|
|
|
2017-01-30 12:13:02 -07:00
|
|
|
def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files):
|
2017-01-19 16:50:21 -07:00
|
|
|
html_content = io.StringIO()
|
|
|
|
file_content = io.StringIO()
|
|
|
|
if files:
|
|
|
|
file_content.write(
|
|
|
|
''' <section id="files">
|
|
|
|
<h2>Associated files:</h2>
|
|
|
|
<ul>
|
|
|
|
''')
|
|
|
|
for fn in files:
|
|
|
|
file_content.write(' <li><a href="{fn}">{efn}</a></li>\n'.format(fn=fn, efn=escape(fn)))
|
|
|
|
file_content.write(
|
|
|
|
''' </ul>
|
|
|
|
</section>
|
|
|
|
''')
|
2017-10-23 10:01:11 -06:00
|
|
|
scripts = ['<script src="{}"></script>'.format(s) for s in puzzle.scripts]
|
2018-07-13 16:35:35 -06:00
|
|
|
|
2017-01-19 16:50:21 -07:00
|
|
|
html_content.write(
|
|
|
|
'''<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width">
|
|
|
|
<title>{category} {points}</title>
|
|
|
|
<link rel="stylesheet" href="../../style.css">
|
2017-10-23 10:01:11 -06:00
|
|
|
{scripts}
|
2017-01-19 16:50:21 -07:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>{category} for {points} points</h1>
|
|
|
|
<section id="readme">
|
|
|
|
{body} </section>
|
|
|
|
{file_content} <section id="form">
|
|
|
|
<form id="puzzler" action="../../cgi-bin/puzzler.cgi" method="get" accept-charset="utf-8" autocomplete="off">
|
|
|
|
<input type="hidden" name="c" value="{category}">
|
|
|
|
<input type="hidden" name="p" value="{points}">
|
|
|
|
<div>Team hash:<input name="t" size="8"></div>
|
2017-10-30 12:16:23 -06:00
|
|
|
<div>Answer:<input name="a" id="answer" size="20"></div>
|
2017-01-19 16:50:21 -07:00
|
|
|
<input type="submit" value="submit">
|
|
|
|
</form>
|
|
|
|
</section>
|
2017-01-30 12:13:02 -07:00
|
|
|
<address>Puzzle by <span class="authors" data-handle="{authors}">{authors}</span></address>
|
2017-01-19 16:50:21 -07:00
|
|
|
</body>
|
2017-10-23 10:01:11 -06:00
|
|
|
</html>'''.format(
|
2017-10-25 12:41:16 -06:00
|
|
|
category=category,
|
|
|
|
points=points,
|
|
|
|
body=puzzle.html_body(),
|
|
|
|
file_content=file_content.getvalue(),
|
|
|
|
authors=', '.join(authors),
|
|
|
|
scripts='\n'.join(scripts),
|
2018-07-13 16:35:35 -06:00
|
|
|
)
|
2017-10-23 10:01:11 -06:00
|
|
|
)
|
2017-01-19 16:50:21 -07:00
|
|
|
ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue())
|
2017-01-05 16:50:41 -07:00
|
|
|
|
2018-07-13 16:35:35 -06:00
|
|
|
|
2017-01-05 16:50:41 -07:00
|
|
|
def build_category(categorydir, outdir):
|
|
|
|
category_seed = binascii.b2a_hex(os.urandom(20))
|
|
|
|
|
|
|
|
categoryname = os.path.basename(categorydir.strip(os.sep))
|
|
|
|
zipfilename = os.path.join(outdir, "%s.zip" % categoryname)
|
|
|
|
logging.info("Building {} from {}".format(zipfilename, categorydir))
|
|
|
|
|
|
|
|
if os.path.exists(zipfilename):
|
|
|
|
# open and gather some state
|
|
|
|
existing = zipfile.ZipFile(zipfilename, 'r')
|
|
|
|
try:
|
2018-07-13 16:35:35 -06:00
|
|
|
category_seed = existing.open(SEEDFN).read().strip()
|
|
|
|
except Exception:
|
2017-01-05 16:50:41 -07:00
|
|
|
pass
|
|
|
|
existing.close()
|
|
|
|
logging.debug("Using PRNG seed {}".format(category_seed))
|
|
|
|
|
2018-05-11 17:00:55 -06:00
|
|
|
zipfileraw = tempfile.NamedTemporaryFile(delete=False)
|
2018-07-13 16:35:35 -06:00
|
|
|
mothball = package(categoryname, categorydir, category_seed)
|
2018-05-11 17:00:55 -06:00
|
|
|
shutil.copyfileobj(mothball, zipfileraw)
|
|
|
|
zipfileraw.close()
|
|
|
|
shutil.move(zipfileraw.name, zipfilename)
|
|
|
|
|
|
|
|
|
|
|
|
# Returns a file-like object containing the contents of the new zip file
|
|
|
|
def package(categoryname, categorydir, seed):
|
|
|
|
zfraw = io.BytesIO()
|
|
|
|
zf = zipfile.ZipFile(zfraw, 'x')
|
|
|
|
zf.writestr("category_seed.txt", seed)
|
2017-01-05 16:50:41 -07:00
|
|
|
|
2018-05-11 17:00:55 -06:00
|
|
|
cat = moth.Category(categorydir, seed)
|
2017-01-05 16:50:41 -07:00
|
|
|
mapping = {}
|
|
|
|
answers = {}
|
|
|
|
summary = {}
|
|
|
|
for puzzle in cat:
|
|
|
|
logging.info("Processing point value {}".format(puzzle.points))
|
|
|
|
|
2018-07-13 16:35:35 -06:00
|
|
|
hashmap = hashlib.sha1(seed)
|
2017-01-05 16:50:41 -07:00
|
|
|
hashmap.update(str(puzzle.points).encode('utf-8'))
|
|
|
|
puzzlehash = hashmap.hexdigest()
|
2018-07-13 16:35:35 -06:00
|
|
|
|
2017-01-05 16:50:41 -07:00
|
|
|
mapping[puzzle.points] = puzzlehash
|
|
|
|
answers[puzzle.points] = puzzle.answers
|
|
|
|
summary[puzzle.points] = puzzle.summary
|
|
|
|
|
|
|
|
puzzledir = os.path.join('content', puzzlehash)
|
|
|
|
files = []
|
|
|
|
for fn, f in puzzle.files.items():
|
|
|
|
if f.visible:
|
|
|
|
files.append(fn)
|
|
|
|
payload = f.stream.read()
|
|
|
|
zf.writestr(os.path.join(puzzledir, fn), payload)
|
|
|
|
|
|
|
|
puzzledict = {
|
2017-01-30 12:13:02 -07:00
|
|
|
'authors': puzzle.authors,
|
2017-01-05 16:50:41 -07:00
|
|
|
'hashes': puzzle.hashes(),
|
|
|
|
'files': files,
|
|
|
|
'body': puzzle.html_body(),
|
|
|
|
}
|
|
|
|
puzzlejson = json.dumps(puzzledict)
|
|
|
|
zf.writestr(os.path.join(puzzledir, 'puzzle.json'), puzzlejson)
|
2017-01-30 12:13:02 -07:00
|
|
|
generate_html(zf, puzzle, puzzledir, categoryname, puzzle.points, puzzle.get_authors(), files)
|
2017-01-05 16:50:41 -07:00
|
|
|
|
|
|
|
write_kv_pairs(zf, 'map.txt', mapping)
|
|
|
|
write_kv_pairs(zf, 'answers.txt', answers)
|
|
|
|
write_kv_pairs(zf, 'summaries.txt', summary)
|
|
|
|
|
|
|
|
# clean up
|
|
|
|
zf.close()
|
2018-05-11 17:00:55 -06:00
|
|
|
zfraw.seek(0)
|
|
|
|
return zfraw
|
2018-07-13 16:35:35 -06:00
|
|
|
|
|
|
|
|
2017-10-23 10:01:11 -06:00
|
|
|
if __name__ == '__main__':
|
2017-01-05 16:50:41 -07:00
|
|
|
parser = argparse.ArgumentParser(description='Build a category package')
|
|
|
|
parser.add_argument('outdir', help='Output directory')
|
2017-09-11 09:42:55 -06:00
|
|
|
parser.add_argument('categorydirs', nargs='+', help='Directory of category source')
|
2017-01-05 16:50:41 -07:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
|
for categorydir in args.categorydirs:
|
|
|
|
build_category(categorydir, args.outdir)
|