category_seed now getting read or generated

This commit is contained in:
slackish 2016-10-20 13:27:34 -06:00
parent 2b29ffcf3d
commit 962fa791f6
1 changed files with 22 additions and 21 deletions

View File

@ -1,20 +1,20 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import base64 import binascii
import glob import glob
import hashlib import hashlib
import hmac
import io import io
import json import json
import os import os
import markdown import shutil
import random
import sys import sys
import zipfile import zipfile
import puzzles import puzzles
TMPFILE = "%s.tmp"
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
@ -34,7 +34,6 @@ def write_kv_pairs(ziphandle, filename, kv):
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Build a category package') parser = argparse.ArgumentParser(description='Build a category package')
parser.add_argument('seed', help='contest seed')
parser.add_argument('categorydirs', nargs='+', help='Directory of category source') parser.add_argument('categorydirs', nargs='+', help='Directory of category source')
parser.add_argument('outdir', help='Output directory') parser.add_argument('outdir', help='Output directory')
args = parser.parse_args() args = parser.parse_args()
@ -45,22 +44,20 @@ if __name__ == '__main__':
categoryname = os.path.basename(categorydir.strip(os.sep)) categoryname = os.path.basename(categorydir.strip(os.sep))
# build category seed
category_seed = hashlib.new('sha1')
category_seed.update(categoryname.encode('utf-8'))
category_seed.update(args.seed.encode('utf-8'))
category_seed = category_seed.hexdigest()
# create zipfile # create zipfile
zipfilename = os.path.join(args.outdir, "%s.zip" % categoryname) zipfilename = os.path.join(args.outdir, "%s.zip" % categoryname)
if os.path.exists(zipfilename): if os.path.exists(zipfilename):
# assume blank state for now # open and gather some state
zf = zipfile.ZipFile(zipfilename, 'r')
try:
category_seed = zf.open(os.path.join(categoryname, "category_seed.txt")).read().strip()
except:
pass pass
# # open and gather some state zf.close()
# zf = zipfile.ZipFile(zipfilename, 'a')
else: zf = zipfile.ZipFile(TMPFILE % zipfilename, 'w')
# create and read in state if 'category_seed' not in locals():
zf = zipfile.ZipFile(zipfilename, 'w') category_seed = binascii.b2a_hex(os.urandom(20))
# read in category details (will be pflarr in future) # read in category details (will be pflarr in future)
for categorypath in glob.glob(os.path.join(categorydir, "*", "puzzle.moth")): for categorypath in glob.glob(os.path.join(categorydir, "*", "puzzle.moth")):
@ -81,18 +78,18 @@ if __name__ == '__main__':
summary = {} summary = {}
for points in sorted(puzzles_dict): for points in sorted(puzzles_dict):
puzzle = puzzles_dict[points] puzzle = puzzles_dict[points]
hashmap = hashlib.sha1(args.seed.encode('utf-8')) hashmap = hashlib.sha1(category_seed)
hashmap.update(categoryname.encode('utf-8'))
hashmap.update(str(points).encode('utf-8')) hashmap.update(str(points).encode('utf-8'))
mapping[points] = hashmap.hexdigest() mapping[points] = hashmap.hexdigest()
answers[points] = puzzle['answer'] answers[points] = puzzle['answer']
if len(puzzle['summary']) > 0: if len(puzzle['summary']) > 0:
summary[points] = puzzle['summary'] summary[points] = puzzle['summary']
# write mapping, answers, and summary # write mapping, answers, summary, category_seed
write_kv_pairs(zf, os.path.join(categoryname, 'map.txt'), mapping) write_kv_pairs(zf, os.path.join(categoryname, 'map.txt'), mapping)
write_kv_pairs(zf, os.path.join(categoryname, 'answers.txt'), answers) write_kv_pairs(zf, os.path.join(categoryname, 'answers.txt'), answers)
write_kv_pairs(zf, os.path.join(categoryname, 'summary.txt'), summary) write_kv_pairs(zf, os.path.join(categoryname, 'summary.txt'), summary)
zf.writestr(os.path.join(categoryname, "category_seed.txt"), category_seed)
# write out puzzles # write out puzzles
for points in sorted(puzzles_dict): for points in sorted(puzzles_dict):
@ -114,4 +111,8 @@ if __name__ == '__main__':
# non-optimal writing of file-like objects, but it works # non-optimal writing of file-like objects, but it works
zf.writestr(os.path.join(puzzledir, 'puzzle.json'), \ zf.writestr(os.path.join(puzzledir, 'puzzle.json'), \
json.dumps(puzzle.publish())) json.dumps(puzzle.publish()))
# clean up
zf.close()
shutil.move(TMPFILE % zipfilename, zipfilename)
#vim:py #vim:py