#! /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) if not os.path.isdir(pointsdir): continue outdir = os.path.join(opts.htmldir, cat, points) try: os.makedirs(outdir) except OSError: pass 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() elif fn.endswith('~'): pass 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(''' %(title)s

%(title)s

''' % {'title': title, 'css': config.css}) if readme: f.write('
%s
\n' % readme) if files: f.write('\n') f.write('''
Your answer: Team:
Password:
Key:
''' % {'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)