-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_problem_format.py
executable file
·103 lines (97 loc) · 3.34 KB
/
generate_problem_format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/python
import os, glob, json, shutil, sys
PROBLEM_FILE = "problem.txt"
HINT_FILE = "hint.txt"
CATEGORY_FILE = "category.txt"
GRADER_FILE = "grader.py"
WEIGHTMAP_FILE = "weightmap.json"
RELEASE_FOLDER = "release"
SERVER_FOLDER = "STUYCTF_SERVER/"
template = """
{{
"name": "{name}",
"score": {score},
"category": "{category}",
"grader": "{grader}",
"description": "{description}",
"threshold": {threshold},
"weightmap": {weightmap},
"hint": "{hint}"
}}
"""
# Remove old server folder and create a new one
shutil.rmtree(SERVER_FOLDER, ignore_errors=True)
os.mkdir(SERVER_FOLDER)
EXCLUDE_DIRS = ["./sample", "./STUYCTF_SERVER"]
# Get list of all folders in current directory
folders = [f+"/" for f in glob.glob("./*") if not os.path.isfile(f)\
and f not in EXCLUDE_DIRS and "INCOMPLETE" not in f\
and "TEST" not in f]
problems = []
for folder in folders:
# Problem name format: PROBLEM-NAME_SCORE
problem_name = folder[2:folder.find("_")]
problem_score = 0 if folder.find("_") == -1 else folder[folder.find("_")+1:-1]
problem_category = ""
problem_description = ""
problem_hint = ""
problem_threshold = 0
problem_weightmap = {}
problem_grader = folder[:folder.find("_")] + '/' + GRADER_FILE
try:
with open(folder + PROBLEM_FILE, "r") as f:
problem_description = f.read()
except IOError, e:
print "Missing problem file...", e
try:
with open(folder + HINT_FILE, "r") as f:
problem_hint = f.read()
except IOError, e:
#print "Missing hint file...", e
pass
try:
with open(folder + CATEGORY_FILE, "r") as f:
problem_category = f.read().strip()
except IOError, e:
print "Missing category file...", e
try:
with open(folder + WEIGHTMAP_FILE, "r") as f:
weightmaps = json.loads(f.read())
problem_threshold = weightmaps["threshold"]
# Convert dashes "-" to spaces " " for later pid (problem id) hashing
for key, value in weightmaps["weightmap"].iteritems():
problem_weightmap[key.replace('-', ' ')] = value
except IOError, e:
#print "Missing weightmap file...", e
pass
problem = template.format(
name = problem_name.replace("-"," "),
score = problem_score,
category = problem_category,
grader = problem_grader,
description = problem_description.replace("\n","<br>").replace('"','\\"'),
hint = problem_hint.replace("\n","<br>").replace('"', '\\"'),
threshold = problem_threshold,
weightmap = json.dumps(problem_weightmap)
)
problems.append(problem)
try:
os.mkdir(SERVER_FOLDER + problem_name)
with open(SERVER_FOLDER + problem_name + "/problem.json", "w") as f:
f.write(problem)
except OSError, e:
print "Error writing problem.json...", e
try:
shutil.copytree(folder + RELEASE_FOLDER, SERVER_FOLDER + problem_name + "/static/")
except OSError, e:
#print "Error copying release folder...", e
pass
try:
os.mkdir(SERVER_FOLDER + problem_name + "/grader")
shutil.copy(folder + GRADER_FILE, SERVER_FOLDER + problem_name + "/grader")
except IOError, e:
print "Error copying grader.py...", e
'''
for problem in problems:
print problem
'''