mirror of https://github.com/dirtbags/moth.git
Merge pull request #1 from dirtbags/master
Backport changes from upstream
This commit is contained in:
commit
2b97c4fb7e
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import binascii
|
import binascii
|
||||||
import glob
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
|
@ -10,11 +9,12 @@ import logging
|
||||||
import moth
|
import moth
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import string
|
|
||||||
import sys
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
|
SEEDFN = "SEED"
|
||||||
|
|
||||||
|
|
||||||
def write_kv_pairs(ziphandle, filename, kv):
|
def write_kv_pairs(ziphandle, filename, kv):
|
||||||
""" Write out a sorted map to file
|
""" Write out a sorted map to file
|
||||||
:param ziphandle: a zipfile object
|
:param ziphandle: a zipfile object
|
||||||
|
@ -24,7 +24,7 @@ def write_kv_pairs(ziphandle, filename, kv):
|
||||||
"""
|
"""
|
||||||
filehandle = io.StringIO()
|
filehandle = io.StringIO()
|
||||||
for key in sorted(kv.keys()):
|
for key in sorted(kv.keys()):
|
||||||
if type(kv[key]) == type([]):
|
if isinstance(kv[key], list):
|
||||||
for val in kv[key]:
|
for val in kv[key]:
|
||||||
filehandle.write("%s %s\n" % (key, val))
|
filehandle.write("%s %s\n" % (key, val))
|
||||||
else:
|
else:
|
||||||
|
@ -32,9 +32,11 @@ def write_kv_pairs(ziphandle, filename, kv):
|
||||||
filehandle.seek(0)
|
filehandle.seek(0)
|
||||||
ziphandle.writestr(filename, filehandle.read())
|
ziphandle.writestr(filename, filehandle.read())
|
||||||
|
|
||||||
|
|
||||||
def escape(s):
|
def escape(s):
|
||||||
return s.replace('&', '&').replace('<', '<').replace('>', '>')
|
return s.replace('&', '&').replace('<', '<').replace('>', '>')
|
||||||
|
|
||||||
|
|
||||||
def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files):
|
def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files):
|
||||||
html_content = io.StringIO()
|
html_content = io.StringIO()
|
||||||
file_content = io.StringIO()
|
file_content = io.StringIO()
|
||||||
|
@ -88,10 +90,9 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
|
||||||
)
|
)
|
||||||
ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue())
|
ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue())
|
||||||
|
|
||||||
|
|
||||||
def build_category(categorydir, outdir):
|
def build_category(categorydir, outdir):
|
||||||
category_seed = binascii.b2a_hex(os.urandom(20))
|
category_seed = binascii.b2a_hex(os.urandom(20))
|
||||||
puzzles_dict = {}
|
|
||||||
secrets = {}
|
|
||||||
|
|
||||||
categoryname = os.path.basename(categorydir.strip(os.sep))
|
categoryname = os.path.basename(categorydir.strip(os.sep))
|
||||||
zipfilename = os.path.join(outdir, "%s.zip" % categoryname)
|
zipfilename = os.path.join(outdir, "%s.zip" % categoryname)
|
||||||
|
@ -101,14 +102,14 @@ def build_category(categorydir, outdir):
|
||||||
# open and gather some state
|
# open and gather some state
|
||||||
existing = zipfile.ZipFile(zipfilename, 'r')
|
existing = zipfile.ZipFile(zipfilename, 'r')
|
||||||
try:
|
try:
|
||||||
category_seed = existing.open(seedfn).read().strip()
|
category_seed = existing.open(SEEDFN).read().strip()
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
existing.close()
|
existing.close()
|
||||||
logging.debug("Using PRNG seed {}".format(category_seed))
|
logging.debug("Using PRNG seed {}".format(category_seed))
|
||||||
|
|
||||||
zipfileraw = tempfile.NamedTemporaryFile(delete=False)
|
zipfileraw = tempfile.NamedTemporaryFile(delete=False)
|
||||||
mothball = package(categoryname, categorydir, zfraw)
|
mothball = package(categoryname, categorydir, category_seed)
|
||||||
shutil.copyfileobj(mothball, zipfileraw)
|
shutil.copyfileobj(mothball, zipfileraw)
|
||||||
zipfileraw.close()
|
zipfileraw.close()
|
||||||
shutil.move(zipfileraw.name, zipfilename)
|
shutil.move(zipfileraw.name, zipfilename)
|
||||||
|
@ -127,7 +128,7 @@ def package(categoryname, categorydir, seed):
|
||||||
for puzzle in cat:
|
for puzzle in cat:
|
||||||
logging.info("Processing point value {}".format(puzzle.points))
|
logging.info("Processing point value {}".format(puzzle.points))
|
||||||
|
|
||||||
hashmap = hashlib.sha1(seed.encode('utf-8'))
|
hashmap = hashlib.sha1(seed)
|
||||||
hashmap.update(str(puzzle.points).encode('utf-8'))
|
hashmap.update(str(puzzle.points).encode('utf-8'))
|
||||||
puzzlehash = hashmap.hexdigest()
|
puzzlehash = hashmap.hexdigest()
|
||||||
|
|
||||||
|
@ -173,4 +174,3 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
for categorydir in args.categorydirs:
|
for categorydir in args.categorydirs:
|
||||||
build_category(categorydir, args.outdir)
|
build_category(categorydir, args.outdir)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue