8
8
from time import sleep
9
9
from typing import Tuple , Optional
10
10
11
- from atcodertools .codegen .code_gen_config import CodeGenConfig
12
11
from atcodertools .codegen .cpp_code_generator import CppCodeGenerator
13
12
from atcodertools .codegen .java_code_generator import JavaCodeGenerator
13
+ from atcodertools .config .config import Config
14
+ from atcodertools .fileutils .create_contest_file import create_examples , create_code_from
14
15
from atcodertools .constprediction .constants_prediction import predict_constants
15
16
from atcodertools .fileutils .create_contest_file import create_examples , \
16
17
create_code_from
@@ -41,16 +42,25 @@ def extension(lang: str):
41
42
OUT_EXAMPLE_FORMAT = "out_{}.txt"
42
43
43
44
45
+ def output_splitter ():
46
+ # for readability
47
+ print ("=================================================" , file = sys .stderr )
48
+
49
+
50
+ def _message_on_execution (cwd : str , cmd : str ):
51
+ return "Executing the following command in `{}`: {}" .format (cwd , cmd )
52
+
53
+
44
54
def prepare_procedure (atcoder_client : AtCoderClient ,
45
55
problem : Problem ,
46
56
workspace_root_path : str ,
47
57
template_code_path : str ,
48
58
replacement_code_path : str ,
49
59
lang : str ,
50
- config : CodeGenConfig ,
60
+ config : Config ,
51
61
):
52
62
pid = problem .get_alphabet ()
53
- workspace_dir_path = os .path .join (
63
+ problem_dir_path = os .path .join (
54
64
workspace_root_path ,
55
65
problem .get_contest ().get_id (),
56
66
pid )
@@ -78,13 +88,13 @@ def emit_info(text):
78
88
if len (content .get_samples ()) == 0 :
79
89
emit_info ("No samples." )
80
90
else :
81
- os .makedirs (workspace_dir_path , exist_ok = True )
82
- create_examples (content .get_samples (), workspace_dir_path ,
91
+ os .makedirs (problem_dir_path , exist_ok = True )
92
+ create_examples (content .get_samples (), problem_dir_path ,
83
93
IN_EXAMPLE_FORMAT , OUT_EXAMPLE_FORMAT )
84
94
emit_info ("Created examples." )
85
95
86
96
code_file_path = os .path .join (
87
- workspace_dir_path ,
97
+ problem_dir_path ,
88
98
"main.{}" .format (extension (lang )))
89
99
90
100
# If there is an existing code, just create backup
@@ -119,7 +129,7 @@ def emit_info(text):
119
129
create_code_from (
120
130
result ,
121
131
constants ,
122
- gen_class (template , config ),
132
+ gen_class (template , config . code_style_config ),
123
133
code_file_path )
124
134
emit_info (
125
135
"Prediction succeeded -- Saved auto-generated code to '{}'" .format (code_file_path ))
@@ -138,7 +148,7 @@ def emit_info(text):
138
148
code_file_path ))
139
149
140
150
# Save metadata
141
- metadata_path = os .path .join (workspace_dir_path , "metadata.json" )
151
+ metadata_path = os .path .join (problem_dir_path , "metadata.json" )
142
152
Metadata (problem ,
143
153
os .path .basename (code_file_path ),
144
154
IN_EXAMPLE_FORMAT .replace ("{}" , "*" ),
@@ -147,23 +157,31 @@ def emit_info(text):
147
157
).save_to (metadata_path )
148
158
emit_info ("Saved metadata to {}" .format (metadata_path ))
149
159
160
+ if config .postprocess_config .exec_cmd_on_problem_dir is not None :
161
+ emit_info (_message_on_execution (problem_dir_path ,
162
+ config .postprocess_config .exec_cmd_on_problem_dir ))
163
+ config .postprocess_config .execute_on_problem_dir (
164
+ problem_dir_path )
150
165
151
- def func (argv : Tuple [AtCoderClient , Problem , str , str , str , str , CodeGenConfig ]):
166
+ output_splitter ()
167
+
168
+
169
+ def func (argv : Tuple [AtCoderClient , Problem , str , str , str , str , Config ]):
152
170
atcoder_client , problem , workspace_root_path , template_code_path , replacement_code_path , lang , config = argv
153
171
prepare_procedure (
154
172
atcoder_client , problem , workspace_root_path , template_code_path ,
155
173
replacement_code_path , lang , config )
156
174
157
175
158
- def prepare_workspace (atcoder_client : AtCoderClient ,
159
- contest_id : str ,
160
- workspace_root_path : str ,
161
- template_code_path : str ,
162
- replacement_code_path : str ,
163
- lang : str ,
164
- parallel : bool ,
165
- config : CodeGenConfig ,
166
- ):
176
+ def prepare_contest (atcoder_client : AtCoderClient ,
177
+ contest_id : str ,
178
+ workspace_root_path : str ,
179
+ template_code_path : str ,
180
+ replacement_code_path : str ,
181
+ lang : str ,
182
+ parallel : bool ,
183
+ config : Config ,
184
+ ):
167
185
retry_duration = 1.5
168
186
while True :
169
187
problem_list = atcoder_client .download_problem_list (
@@ -176,13 +194,23 @@ def prepare_workspace(atcoder_client: AtCoderClient,
176
194
177
195
tasks = [(atcoder_client , problem , workspace_root_path , template_code_path , replacement_code_path , lang , config ) for
178
196
problem in problem_list ]
197
+
198
+ output_splitter ()
199
+
179
200
if parallel :
180
201
thread_pool = Pool (processes = cpu_count ())
181
202
thread_pool .map (func , tasks )
182
203
else :
183
204
for argv in tasks :
184
205
func (argv )
185
206
207
+ if config .postprocess_config .exec_cmd_on_contest_dir is not None :
208
+ contest_dir_path = os .path .join (workspace_root_path , contest_id )
209
+ logging .info (_message_on_execution (contest_dir_path ,
210
+ config .postprocess_config .exec_cmd_on_contest_dir ))
211
+ config .postprocess_config .execute_on_contest_dir (
212
+ contest_dir_path )
213
+
186
214
187
215
DEFAULT_WORKSPACE_DIR_PATH = os .path .join (
188
216
expanduser ("~" ), "atcoder-workspace" )
@@ -217,11 +245,11 @@ def check_lang(lang: str):
217
245
os .path .join (script_dir_path , "./atcodertools-default.toml" ))
218
246
219
247
220
- def get_code_gen_config (config_path : Optional [str ] = None ):
221
- def _load (path : str ):
248
+ def get_config (config_path : Optional [str ] = None ) -> Config :
249
+ def _load (path : str ) -> Config :
222
250
logging .info ("Going to load {} as config" .format (path ))
223
251
with open (path , 'r' ) as f :
224
- return CodeGenConfig .load (f )
252
+ return Config .load (f )
225
253
226
254
if config_path :
227
255
return _load (config_path )
@@ -285,7 +313,7 @@ def main(prog, args):
285
313
help = "File path to your config file\n {0}{1}" .format ("[Default (Primary)] {}\n " .format (
286
314
USER_CONFIG_PATH ),
287
315
"[Default (Secondary)] {}\n " .format (
288
- DEFAULT_CONFIG_PATH ))
316
+ DEFAULT_CONFIG_PATH ))
289
317
)
290
318
291
319
args = parser .parse_args (args )
@@ -309,17 +337,17 @@ def main(prog, args):
309
337
else :
310
338
logging .info ("Downloading data without login." )
311
339
312
- prepare_workspace (client ,
313
- args .contest_id ,
314
- args .workspace ,
315
- args .template if args .template is not None else get_default_template_path (
316
- args .lang ),
317
- args .replacement if args .replacement is not None else get_default_replacement_path (
318
- args .lang ),
319
- args .lang ,
320
- args .parallel ,
321
- get_code_gen_config (args .config )
322
- )
340
+ prepare_contest (client ,
341
+ args .contest_id ,
342
+ args .workspace ,
343
+ args .template if args .template is not None else get_default_template_path (
344
+ args .lang ),
345
+ args .replacement if args .replacement is not None else get_default_replacement_path (
346
+ args .lang ),
347
+ args .lang ,
348
+ args .parallel ,
349
+ get_config (args .config )
350
+ )
323
351
324
352
325
353
if __name__ == "__main__" :
0 commit comments