-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci_generate.py
executable file
·69 lines (56 loc) · 2.2 KB
/
ci_generate.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
#!/usr/bin/env python3
import os
import argparse
from pathlib import Path
import toml
import generate
from problem import Problem
import json
from logging import Logger, basicConfig, getLogger
from os import getenv
from typing import Dict
logger = getLogger(__name__) # type: Logger
def main():
parser = argparse.ArgumentParser(
description='Testcase generator')
parser.add_argument('--print-version', action='store_true', help='Print version')
parser.add_argument('--htmldir', help='Generate HTML', default=None)
args = parser.parse_args()
basicConfig(
level=getenv('LOG_LEVEL', 'INFO'),
format="%(asctime)s [%(levelname)s] %(message)s"
)
tomls = list(filter(lambda p: not p.match('test/**/info.toml'),
Path('.').glob('**/info.toml')))
tomls = sorted(tomls, key=lambda x: x.parent.name)
versions = dict() # type: Dict[str, str]
for x in tomls:
problem = generate.Problem(Path.cwd(), x.parent)
problem_name = problem.basedir.name
problem_version = problem.problem_version()
versions[problem_name] = problem_version
if args.print_version:
print(json.dumps(versions))
return
cache_dir = Path('cache')
if not cache_dir.exists():
cache_dir.mkdir()
cache = cache_dir / 'generated.json'
generated = json.load(open(str(cache))) if cache.exists() else dict()
if args.htmldir:
Path(args.htmldir).mkdir(exist_ok=True, parents=True)
for x in tomls:
problem = Problem(Path.cwd(), x.parent)
problem_name = problem.basedir.name
problem_version = problem.problem_version()
if problem_name in generated and generated[problem_name] == problem_version:
logger.info('Problem {} is already generated, skip'.format(problem_name))
else:
logger.info('Generate {}, new version: {}'.format(problem_name, problem_version))
problem.generate(mode=Problem.Mode.TEST, html_dir=Path(args.htmldir) if args.htmldir else None)
if problem_name not in generated:
generated[problem_name] = dict()
with open(str(cache), 'w') as f:
json.dump(versions, f)
if __name__ == '__main__':
main()