-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·113 lines (94 loc) · 3.76 KB
/
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
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
#!/usr/bin/env python3
import sys
import argparse
import os
import platform
import shutil
import hashlib
import json
from datetime import datetime
from logging import Logger, basicConfig, getLogger, INFO
from os import getenv
from pathlib import Path
from subprocess import (DEVNULL, PIPE, STDOUT, CalledProcessError,
TimeoutExpired, call, check_call, check_output, run)
from tempfile import TemporaryDirectory
from typing import Any, Iterator, List, MutableMapping, Union, Optional
from enum import Enum
import toml
from problem import Problem, find_problem_dir
logger: Logger = getLogger(__name__)
def main(args: List[str]):
try:
import colorlog
except ImportError:
basicConfig(
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
level=getenv('LOG_LEVEL', 'INFO'),
)
logger.warn('Please install colorlog: pip3 install colorlog')
else:
handler = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
log_colors={
'DEBUG': 'cyan',
'INFO': 'white',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
})
handler.setFormatter(formatter)
basicConfig(
level=getenv('LOG_LEVEL', 'INFO'),
handlers=[handler]
)
parser = argparse.ArgumentParser(description='Testcase Generator')
parser.add_argument('toml', nargs='*', help='Toml File')
parser.add_argument('-p', '--problem', nargs='*',
help='Generate problem', default=[])
parser.add_argument('--dev', action='store_true', help='Developer Mode')
parser.add_argument('--test', action='store_true', help='CI Mode')
parser.add_argument('--htmldir', help='Generate HTML', default=None)
parser.add_argument('--compile-checker',
action='store_true', help='Deprecated: Compile Checker')
parser.add_argument('--only-html', action='store_true', help='HTML generator Mode')
opts = parser.parse_args(args)
if opts.dev and opts.test:
raise ValueError('only one of --dev and --test can be used')
if opts.compile_checker:
logger.warning(
'--compile-checker is deprecated. Checker is compiled in default')
rootdir: Path = Path(__file__).parent
problems: List[Problem] = list()
for tomlpath in opts.toml:
tomlfile = toml.load(opts.toml)
problems.append(Problem(rootdir, Path(tomlpath).parent))
for problem_name in opts.problem:
problem_dir = find_problem_dir(rootdir, problem_name)
if problem_dir is None:
raise ValueError('Cannot find problem: {}'.format(problem_name))
problems.append(Problem(rootdir, problem_dir))
if len(problems) == 0:
logger.warning('No problems')
if opts.htmldir:
logger.info('Make htmldir')
Path(opts.htmldir).mkdir(exist_ok=True, parents=True)
# suppress the annoying dialog appears when an application crashes on Windows
if platform.uname().system == 'Windows':
import ctypes
SEM_NOGPFAULTERRORBOX = 2 # https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx
ctypes.windll.kernel32.SetErrorMode(SEM_NOGPFAULTERRORBOX)
mode = Problem.Mode.DEFAULT
if opts.dev:
mode = Problem.Mode.DEV
if opts.test:
mode = Problem.Mode.TEST
if opts.only_html:
mode = Problem.Mode.HTML
for problem in problems:
problem.generate(mode, Path(opts.htmldir) if opts.htmldir else None)
if __name__ == '__main__':
main(sys.argv[1:])