forked from Kattis/problemtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_from_old_problemformat.py
62 lines (50 loc) · 2.09 KB
/
update_from_old_problemformat.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
# -*- coding: utf-8 -*-
from optparse import OptionParser
import glob
import os
import yaml
def update(problemdir):
probyaml = os.path.join(problemdir, 'problem.yaml')
if not os.path.isfile(probyaml):
raise Exception('Could not find %s' % probyaml)
config = yaml.safe_load('%s' % open(probyaml, 'r').read())
stmts = glob.glob(os.path.join(problemdir, 'problem_statement/problem.tex'))
stmts.extend(glob.glob(os.path.join(problemdir, 'problem_statement/problem.[a-z][a-z].tex')))
yaml_changed = False
if 'name' in config:
print 'Move problem name "%s" to these problem statement files: %s' % (config['name'], stmts)
for f in stmts:
stmt = open(f, 'r').read()
if stmt.find('\\problemname{') != -1:
print ' Statement %s already has a problemname, skipping' % f
continue
newstmt = '\problemname{%s}\n\n%s' % (config['name'], stmt)
open(f, 'w').write(newstmt)
del config['name']
yaml_changed = True
if 'validator' in config:
validator_flags = config['validator'].split()
validation = 'default'
if validator_flags[0] == 'custom':
validation = 'custom'
validator_flags = validator_flags[1:]
validator_flags = ' '.join(validator_flags)
print 'Old validator option exists, moving to validation: %s, validator_flags: %s' % (validation, validator_flags)
config['validation'] = validation
if validator_flags != '':
config['validator_flags'] = validator_flags
del config['validator']
yaml_changed = True
if yaml_changed:
open(probyaml, 'w').write(yaml.dump(config, default_flow_style = False, allow_unicode = True))
if __name__ == '__main__':
parser = OptionParser(usage="usage: %prog problems")
(options, args) = parser.parse_args()
if not args:
parser.print_help()
for dir in args:
try:
print 'Updating %s' % dir
update(dir)
except Exception as e:
print 'Update FAILED: %s' % e