8000 Backport changes from upstream by int00h5525 · Pull Request #1 · int00h5525/moth · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Backport changes from upstream #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions tools/mothballer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import argparse
import binascii
import glob
import hashlib
import io
import json
import logging< 8000 /td>
import moth
import os
import shutil
import string
import sys
import tempfile
import zipfile

SEEDFN = "SEED"


def write_kv_pairs(ziphandle, filename, kv):
""" Write out a sorted map to file
:param ziphandle: a zipfile object
Expand All @@ -24,17 +24,19 @@ def write_kv_pairs(ziphandle, filename, kv):
"""
filehandle = io.StringIO()
for key in sorted(kv.keys()):
if type(kv[key]) == type([]):
if isinstance(kv[key], list):
for val in kv[key]:
filehandle.write("%s %s\n" % (key, val))
else:
filehandle.write("%s %s\n" % (key, kv[key]))
filehandle.seek(0)
ziphandle.writestr(filename, filehandle.read())



def escape(s):
return s.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')



def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files):
html_content = io.StringIO()
file_content = io.StringIO()
Expand All @@ -51,7 +53,7 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
</section>
''')
scripts = ['<script src="{}"></script>'.format(s) for s in puzzle.scripts]

html_content.write(
'''<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -84,14 +86,13 @@ def generate_html(ziphandle, puzzle, puzzledir, category, points, authors, files
file_content=file_content.getvalue(),
authors=', '.join(authors),
scripts='\n'.join(scripts),
)
)
)
ziphandle.writestr(os.path.join(puzzledir, 'index.html'), html_content.getvalue())


def build_category(categorydir, outdir):
category_seed = binascii.b2a_hex(os.urandom(20))
puzzles_dict = {}
secrets = {}

categoryname = os.path.basename(categorydir.strip(os.sep))
zipfilename = os.path.join(outdir, "%s.zip" % categoryname)
Expand All @@ -101,14 +102,14 @@ def build_category(categorydir, outdir):
# open and gather some state
existing = zipfile.ZipFile(zipfilename, 'r')
try:
category_seed = existing.open(seedfn).read().strip()
except:
category_seed = existing.open(SEEDFN).read().strip()
except Exception:
pass
existing.close()
logging.debug("Using PRNG seed {}".format(category_seed))

zipfileraw = tempfile.NamedTemporaryFile(delete=False)
mothball = package(categoryname, categorydir, zfraw)
mothball = package(categoryname, categorydir, category_seed)
shutil.copyfileobj(mothball, zipfileraw)
zipfileraw.close()
shutil.move(zipfileraw.name, zipfilename)
Expand All @@ -127,10 +128,10 @@ def package(categoryname, categorydir, seed):
for puzzle in cat:
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'))
puzzlehash = hashmap.hexdigest()

mapping[puzzle.points] = puzzlehash
answers[puzzle.points] = puzzle.answers
summary[puzzle.points] = puzzle.summary
Expand Down Expand Up @@ -161,8 +162,8 @@ def package(categoryname, categorydir, seed):
zf.close()
zfraw.seek(0)
return zfraw


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Build a category package')
parser.add_argument('outdir', help='Output directory')
Expand All @@ -173,4 +174,3 @@ def package(categoryname, categorydir, seed):

for categorydir in args.categorydirs:
build_category(categorydir, args.outdir)

0