-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·99 lines (89 loc) · 3.9 KB
/
main.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
#!/usr/bin/env python3
from re import A
from jinja2 import Template, Environment, FileSystemLoader
import subprocess
import shutil
from pathlib import Path
import toml
import json
import sys
import os
params = {'problems':{}}
env = Environment(loader=FileSystemLoader('./', encoding='utf8'))
version_hash = subprocess.run("git rev-parse HEAD",shell=True,cwd="./library-checker-problems",stdout=subprocess.PIPE).stdout.decode()
is_local = "--local" in sys.argv
updated=10
with open('.cache.json') as f:
hashlist = json.load(f)
def make_testcase(category,name):
global updated
path=category+"/"+name
tmp=path+('.local' if is_local else '.remote')
if ( tmp in hashlist ) and hashlist[tmp].rstrip('\n') == "ignored":
print("{} is ignored.".format(tmp))
return
if ( tmp in hashlist ) and no_diff(hashlist[tmp].rstrip('\n'),version_hash.rstrip('\n'),path) :
print("{} is cached.".format(tmp))
return
if updated==0:
print("{} become secondary.".format(tmp))
return
print("{} will be made.".format(tmp))
hashlist[tmp]=version_hash
if Path('build/{}'.format(path)).exists():
shutil.rmtree('build/{}'.format(path))
subprocess.call("library-checker-problems/generate.py --test -p {0}".format(name),shell=True)
shutil.copytree("library-checker-problems/{0}/in".format(path),"build/{0}/in".format(path))
shutil.copytree("library-checker-problems/{0}/out".format(path),"build/{0}/out".format(path))
for name in Path("build/{0}/in".format(path)).glob('*.in'):
shutil.move("build/{}/in/{}".format(path,name.name),'.'.join("build/{}/in/{}".format(path,name.name).split('.')[:-1])+".txt")
for name in Path("build/{0}/out".format(path)).glob('*.out'):
shutil.move("build/{}/out/{}".format(path,name.name),'.'.join("build/{}/out/{}".format(path,name.name).split('.')[:-1])+".txt")
updated-=1
def make_problem_page(category,name):
params['problems'].setdefault(category,[])
params['problems'][category].append(name)
path=category+"/"+name
problem_params={"dir":"{0}".format(name),"testcases":[]}
tomls=toml.load("library-checker-problems/{0}/info.toml".format(path))
for cases in tomls["tests"]:
casename='.'.join(cases["name"].split('.')[:-1])
for i in range(0,int(cases["number"])):
problem_params["testcases"].append("{:s}_{:02d}".format(casename,i))
tmpl = env.get_template('templates/problem.html')
if not Path("build/{}".format(category)).exists():
os.makedirs("build/{}".format(category))
with open('build/{0}.html'.format(path), 'w') as f:
f.write(tmpl.render(problem_params))
def make_toppage():
tmpl = env.get_template('templates/index.html')
with open('build/index.html', 'w') as f:
f.write(tmpl.render(params))
def dump_hashlist():
with open('.cache.json','w') as f:
json.dump(hashlist, f, indent=4)
def no_diff(preSHA,SHA,path):
res=subprocess.run("git diff {} {} --name-only --relative={}".format(preSHA,SHA,path),shell=True,cwd="./library-checker-problems",stdout=subprocess.PIPE).stdout.decode()
return res==''
def test():
make_testcase("graph","tree_diameter")
make_testcase("datastructure","unionfind")
make_testcase("datastructure","associative_array")
make_problem_page("graph","tree_diameter")
make_problem_page("datastructure","unionfind")
make_problem_page("datastructure","associative_array")
make_toppage()
dump_hashlist()
def main():
tomls = list(filter(lambda p: not p.match('test/**/info.toml'),
Path('.').glob('**/info.toml')))
tomls = sorted(tomls, key=lambda x: x.parent.name)
for x in tomls:
problem=x.parent
changed=make_testcase(problem.parent.name,problem.name)
make_problem_page(problem.parent.name,problem.name)
make_toppage()
dump_hashlist()
if __name__ == '__main__':
if is_local:test()
else:main()