2016-10-18 09:36:24 -06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
2016-10-20 13:27:34 -06:00
|
|
|
import binascii
|
2016-10-18 09:36:24 -06:00
|
|
|
import glob
|
|
|
|
import hashlib
|
|
|
|
import io
|
|
|
|
import json
|
|
|
|
import os
|
2016-10-20 13:27:34 -06:00
|
|
|
import shutil
|
2016-10-18 15:43:14 -06:00
|
|
|
import sys
|
2016-10-18 09:36:24 -06:00
|
|
|
import zipfile
|
|
|
|
|
2016-10-18 15:42:42 -06:00
|
|
|
import puzzles
|
2016-10-18 09:36:24 -06:00
|
|
|
|
2016-10-20 13:27:34 -06:00
|
|
|
TMPFILE = "%s.tmp"
|
|
|
|
|
2016-10-18 09:36:24 -06:00
|
|
|
def write_kv_pairs(ziphandle, filename, kv):
|
2016-10-18 15:42:42 -06:00
|
|
|
""" 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:
|
|
|
|
"""
|
2016-10-18 09:36:24 -06:00
|
|
|
filehandle = io.StringIO()
|
|
|
|
for key in sorted(kv.keys()):
|
|
|
|
if type(kv[key]) == type([]):
|
|
|
|
for val in kv[key]:
|
|
|
|
filehandle.write("%s: %s%s" % (key, val, os.linesep))
|
|
|
|
else:
|
|
|
|
filehandle.write("%s: %s%s" % (key, kv[key], os.linesep))
|
|
|
|
filehandle.seek(0)
|
|
|
|
ziphandle.writestr(filename, filehandle.read())
|
2016-10-18 15:42:42 -06:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-10-18 15:43:14 -06:00
|
|
|
parser = argparse.ArgumentParser(description='Build a category package')
|
|
|
|
parser.add_argument('categorydirs', nargs='+', help='Directory of category source')
|
2016-10-18 15:42:42 -06:00
|
|
|
parser.add_argument('outdir', help='Output directory')
|
|
|
|
args = parser.parse_args()
|
2016-10-18 09:36:24 -06:00
|
|
|
|
2016-10-18 15:43:14 -06:00
|
|
|
for categorydir in args.categorydirs:
|
|
|
|
puzzles_dict = {}
|
2016-10-18 15:42:42 -06:00
|
|
|
secrets = {}
|
2016-10-18 09:36:24 -06:00
|
|
|
|
2016-10-18 15:43:14 -06:00
|
|
|
categoryname = os.path.basename(categorydir.strip(os.sep))
|
2016-10-18 09:36:24 -06:00
|
|
|
|
2016-10-18 15:42:42 -06:00
|
|
|
# create zipfile
|
|
|
|
zipfilename = os.path.join(args.outdir, "%s.zip" % categoryname)
|
|
|
|
if os.path.exists(zipfilename):
|
2016-10-20 13:27:34 -06:00
|
|
|
# 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
|
|
|
|
zf.close()
|
|
|
|
|
|
|
|
zf = zipfile.ZipFile(TMPFILE % zipfilename, 'w')
|
|
|
|
if 'category_seed' not in locals():
|
|
|
|
category_seed = binascii.b2a_hex(os.urandom(20))
|
2016-10-18 15:42:42 -06:00
|
|
|
|
2016-10-18 15:43:14 -06:00
|
|
|
# read in category details (will be pflarr in future)
|
|
|
|
for categorypath in glob.glob(os.path.join(categorydir, "*", "puzzle.moth")):
|
|
|
|
points = categorypath.split(os.sep)[-2] # directory before '/puzzle.moth'
|
|
|
|
categorypath = os.path.dirname(categorypath)
|
2016-10-18 16:06:11 -06:00
|
|
|
print(categorypath)
|
2016-10-18 15:43:14 -06:00
|
|
|
try:
|
|
|
|
points = int(points)
|
|
|
|
except:
|
|
|
|
print("Failed to identify points on: %s" % categorypath, file=sys.stderr)
|
|
|
|
continue
|
2016-10-18 16:06:11 -06:00
|
|
|
puzzle = puzzles.Puzzle(category_seed, categorypath)
|
2016-10-18 15:43:14 -06:00
|
|
|
puzzles_dict[points] = puzzle
|
2016-10-18 15:42:42 -06:00
|
|
|
|
|
|
|
# build mapping, answers, and summary
|
|
|
|
mapping = {}
|
|
|
|
answers = {}
|
|
|
|
summary = {}
|
2016-10-18 15:43:14 -06:00
|
|
|
for points in sorted(puzzles_dict):
|
|
|
|
puzzle = puzzles_dict[points]
|
2016-10-20 13:27:34 -06:00
|
|
|
hashmap = hashlib.sha1(category_seed)
|
2016-10-18 15:42:42 -06:00
|
|
|
hashmap.update(str(points).encode('utf-8'))
|
|
|
|
mapping[points] = hashmap.hexdigest()
|
2016-10-18 17:03:42 -06:00
|
|
|
answers[points] = puzzle['answer']
|
|
|
|
if len(puzzle['summary']) > 0:
|
|
|
|
summary[points] = puzzle['summary']
|
2016-10-18 15:42:42 -06:00
|
|
|
|
2016-10-20 13:27:34 -06:00
|
|
|
# write mapping, answers, summary, category_seed
|
2016-10-18 15:42:42 -06:00
|
|
|
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, 'summary.txt'), summary)
|
2016-10-20 13:27:34 -06:00
|
|
|
zf.writestr(os.path.join(categoryname, "category_seed.txt"), category_seed)
|
2016-10-18 15:42:42 -06:00
|
|
|
|
|
|
|
# write out puzzles
|
2016-10-18 15:43:14 -06:00
|
|
|
for points in sorted(puzzles_dict):
|
|
|
|
puzzle = puzzles_dict[points]
|
2016-10-18 15:42:42 -06:00
|
|
|
puzzledir = os.path.join(categoryname, 'content', mapping[points])
|
2016-10-20 12:48:48 -06:00
|
|
|
puzzlejson = puzzle.publish()
|
2016-10-18 17:03:42 -06:00
|
|
|
|
2016-10-18 15:42:42 -06:00
|
|
|
# write associated files
|
2016-10-18 17:03:42 -06:00
|
|
|
assoc_files = []
|
2016-10-18 15:43:14 -06:00
|
|
|
for fobj in puzzle['files']:
|
2016-10-18 17:03:42 -06:00
|
|
|
if fobj.visible == True:
|
|
|
|
assoc_files.append(fobj.name)
|
2016-10-18 15:43:14 -06:00
|
|
|
zf.writestr(os.path.join(puzzledir, fobj.name), \
|
|
|
|
fobj.handle.read())
|
2016-10-18 17:03:42 -06:00
|
|
|
|
|
|
|
if len(assoc_files) > 0:
|
2016-10-20 12:48:48 -06:00
|
|
|
puzzlejson["associated_files"] = assoc_files
|
2016-10-18 17:03:42 -06:00
|
|
|
|
|
|
|
# non-optimal writing of file-like objects, but it works
|
|
|
|
zf.writestr(os.path.join(puzzledir, 'puzzle.json'), \
|
2016-10-21 16:11:09 -06:00
|
|
|
json.dumps(puzzlejson))
|
2016-10-20 13:27:34 -06:00
|
|
|
|
|
|
|
# clean up
|
|
|
|
zf.close()
|
|
|
|
shutil.move(TMPFILE % zipfilename, zipfilename)
|
2016-10-18 09:36:24 -06:00
|
|
|
#vim:py
|