This repository was archived by the owner on Oct 23, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
211 lines (188 loc) · 7.13 KB
/
Copy pathconfig.py
File metadata and controls
211 lines (188 loc) · 7.13 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
import os
import pytoml
from task_maker.args import CacheMode, UIS, TaskFormat, Arch
from typing import List, Optional, Tuple, Any, Union
class Config:
OPTIONS = {
"generic": [
"solutions", "task_dir", "max_depth", "ui", "cache", "dry_run",
"no_sanity_checks", "clean", "task_info", "format", "fuzz_checker"
],
"remote": [
"server", "no_spawn", "run_server", "run_worker", "stop",
"storedir", "tempdir", "cache_size"
],
"server": [
"server_logfile", "server_pidfile", "server_address",
"server_port", "server_verbose"
],
"worker": [
"worker_logfile", "worker_pidfile", "worker_keep_sandboxes",
"worker_name", "worker_num_cores", "worker_port", "worker_address",
"worker_pending_requests", "worker_verbose"
],
"execution": ["exclusive", "extra_time", "copy_exe"],
"ioi": ["detailed_checker"],
"terry": ["arch", "seed"],
"statement": ["no_statement", "set"],
"help": ["help_colors"],
"bulk": ["contest_dir", "contest_yaml", "make_booklet"]
}
CUSTOM_TYPES = {
"ui": UIS,
"cache": CacheMode,
"format": TaskFormat,
"arch": Arch
}
def __init__(self):
# generic group
self.solutions = [] # type: List[str]
self.cwd = os.getcwd()
# $PWD for tasks inside symlinks
self.task_dir = os.getenv("PWD", os.getcwd())
self.max_depth = 2
self.ui = UIS.CURSES
self.cache = CacheMode.ALL
self.dry_run = False
self.no_sanity_checks = False
self.clean = False
self.task_info = False
self.format = None # type: Optional[TaskFormat]
self.fuzz_checker = None # type: Tuple[str, str]
# remote group
self.server = "127.0.0.1:7070"
self.no_spawn = False
self.run_server = False
self.run_worker = False
self.stop = False
self.storedir = "~/.cache/task-maker/files"
self.tempdir = "~/.cache/task-maker/temp"
self.cache_size = 2048 # in MiB
self._get_host_port()
self._resolve_dir()
# server group
self.server_logfile = "/tmp/task-maker-server.log"
self.server_pidfile = None # type: Optional[str]
self.server_address = None # type: Optional[str]
self.server_port = 7070
self.server_verbose = False
# worker group
self.worker_logfile = "/tmp/task-maker-worker.log"
self.worker_pidfile = None # type: Optional[str]
self.worker_keep_sandboxes = False
self.worker_name = "local"
self.worker_num_cores = None # type: Optional[int]
self.worker_port = 7070
self.worker_address = "127.0.0.1"
self.worker_pending_requests = None # type: Optional[int]
self.worker_verbose = False
# execution group
self.exclusive = False
self.extra_time = 0.0
self.copy_exe = False
# IOI group
self.detailed_checker = False
# terry group
self.arch = Arch.DEFAULT
self.seed = None
# statement group
self.no_statement = False
self.set = []
# help group
self.help_colors = False
# bulk group
self.contest_dir = os.getcwd()
self.contest_yaml = None
self.make_booklet = False
# current index in bulk run and total number of bulk tasks
self.bulk_number = None # type: Optional[int]
self.bulk_total = None # type: Optional[int]
def apply_args(self, args):
"""
Apply all the options from Config.OPTIONS using the argparse's result
"""
for group, options in Config.OPTIONS.items():
for arg in options:
self._apply_arg(arg, args)
self._update_state()
def apply_file(self):
"""
Apply the configuration from the ~/.task-maker.toml file
"""
path = os.path.join(os.path.expanduser("~"), ".task-maker.toml")
try:
with open(path, "r") as f:
config = pytoml.loads(f.read())
except FileNotFoundError:
return
for group, items in config.items():
if group not in Config.OPTIONS:
raise ValueError("Invalid group specified in config file: " +
group)
for item, value in items.items():
if item not in Config.OPTIONS[group]:
raise ValueError(
"Invalid option in config.file: {}.{}".format(
group, item))
setattr(self, item, self._get_value(group, item, value))
self._update_state()
def _apply_arg(self, name, args):
value = getattr(args, name, None)
if value is not None and value is not False:
setattr(self, name, value)
def _update_state(self):
self._get_host_port()
self._resolve_dir()
self._get_contest_dir()
self._absolutize()
def _get_host_port(self):
server_addr = self.server.rsplit(":", 1)
if len(server_addr) == 1:
self.host, self.port = server_addr[0], 7070
elif len(server_addr) == 2:
self.host, self.port = server_addr[0], int(server_addr[1])
else:
raise ValueError("Invalid address for the server")
def _resolve_dir(self):
self.storedir = os.path.expanduser(self.storedir)
self.tempdir = os.path.expanduser(self.tempdir)
def _get_contest_dir(self):
if self.contest_yaml:
self.contest_dir = os.path.dirname(self.contest_yaml)
def _absolutize(self):
def _do_absolutize(attr: Union[str, int], scope: Any = self):
if isinstance(attr, str):
val = getattr(scope, attr, None)
else:
val = scope[attr]
if val is not None:
val = os.path.abspath(val)
if isinstance(attr, str):
setattr(scope, attr, val)
else:
scope[attr] = val
_do_absolutize("task_dir")
_do_absolutize("storedir")
_do_absolutize("tempdir")
_do_absolutize("server_logfile")
_do_absolutize("server_pidfile")
_do_absolutize("worker_logfile")
_do_absolutize("worker_pidfile")
_do_absolutize("contest_dir")
_do_absolutize("contest_yaml")
if self.fuzz_checker:
_do_absolutize(0, self.fuzz_checker)
_do_absolutize(1, self.fuzz_checker)
def _get_value(self, group, item, value):
if item in Config.CUSTOM_TYPES:
try:
return Config.CUSTOM_TYPES[item](value)
except Exception:
valid = [x.name.lower() for x in Config.CUSTOM_TYPES[item]]
raise ValueError("Invalid config value at {}.{}, "
"the valid values are: {}".format(
group, item, ", ".join(valid)))
return value
def __repr__(self):
return str(self.__dict__)