-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_bulk.py
executable file
·132 lines (109 loc) · 3.53 KB
/
gen_bulk.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import argparse
import csv
import datetime
import json
import logging
import multiprocessing.dummy as mp
import os
import random
import shutil
import tempfile
import progressbar
import compilation
import utils
progressbar.streams.wrap_stderr()
logger = logging.getLogger("gen_bulk")
def process_user(user, args, work_dir):
contest_dir = args.contest_dir
rnd = random.Random(int(user["seed"]))
tex, sol, order = utils.render_contest(contest_dir, rnd, context=user)
user["solutions"] = ":".join(sol)
user["questions_order"] = ":".join(map(str, order))
filename = user["filename"]
password = user["pdf_password"]
target = os.path.join(args.output_pdf, filename)
if os.path.exists(target):
logger.warning("File %s already present, skipping...", target)
return user
with tempfile.NamedTemporaryFile(prefix=filename) as f:
compilation.compile(tex, f.name, work_dir)
if args.no_enc:
shutil.move(f.name, target)
else:
logger.info("Encrypting PDF %s -> %s", f.name, target)
utils.encrypt_pdf(f.name, target, password)
return user
def generate(args, work_dir, users):
contest_dir = args.contest_dir
compilation.setup(contest_dir, work_dir)
os.makedirs(args.output_pdf, exist_ok=True)
def process(user):
return process_user(user, args, work_dir)
result = []
widgets = [
"[",
progressbar.SimpleProgress(),
" / ",
progressbar.Percentage(),
"] ",
progressbar.Bar(),
" ",
progressbar.Timer(),
" | ",
progressbar.AdaptiveETA(samples=datetime.timedelta(seconds=10)),
]
with mp.Pool(args.num_cores) as pool:
for res in progressbar.progressbar(
pool.imap_unordered(process, users),
max_value=len(users),
redirect_stdout=True,
widgets=widgets,
):
if res:
result.append(res)
headers = list(result[0].keys())
with open(args.output_csv, "w") as f:
writer = csv.DictWriter(f, headers)
writer.writeheader()
writer.writerows(result)
def main(args):
with open(args.users_csv) as f:
reader = csv.DictReader(f)
users = list(reader)
if args.work_dir:
generate(args, args.work_dir, users)
else:
with tempfile.TemporaryDirectory() as work_dir:
generate(args, work_dir, users)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--work-dir",
"-w",
help="Working directory for the compilation",
)
parser.add_argument(
"--num-cores",
"-j",
help="Number of parallel compilations",
type=int,
)
parser.add_argument("--verbose", "-v", help="Verbose output", action="store_true")
parser.add_argument("--no-enc", help="Do not encrypt the pdfs", action="store_true")
parser.add_argument("contest_dir", help="Directory with the contest")
parser.add_argument("users_csv", help="Path to the csv file with the students data")
parser.add_argument(
"output_pdf",
help="Directory of where to save the compiled pdf files",
)
parser.add_argument(
"output_csv",
help="Path where to save the CSV with the solutions",
)
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
)
main(args)