Skip to content

Commit 2156c09

Browse files
committed
Make output of import contest more readable.
Print the output of `zip` line by line, overwriting the last line and removing all output of it on success. Also pretty-print the JSON response on problem output. That makes it much easier to see warnings or errors. Fixes #2342.
1 parent 3db6aae commit 2156c09

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

misc-tools/import-contest.in

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ Part of the DOMjudge Programming Contest Jury System and licensed
1515
under the GNU GPL. See README and COPYING for details.
1616
'''
1717

18-
import json
1918
from os import listdir
19+
from typing import List
20+
import json
2021
import os.path
2122
import re
23+
import subprocess
2224
import sys
23-
from typing import List
2425
import yaml
2526

2627
sys.path.append('@domserver_libdir@')
@@ -213,19 +214,46 @@ if os.path.exists('problems.yaml') or os.path.exists('problems.json') or os.path
213214

214215
confirmIndividually = dj_utils.confirm("Confirm individually for every problem", False)
215216
for problem in problems:
216-
print(f'Preparing problem \'{problem}\'.')
217+
print(f'\nPreparing problem \'{problem}\'.')
217218
if os.path.exists(f'{problem}.zip'):
218219
os.unlink(f'{problem}.zip')
219220
if not os.path.isdir(problem) or not os.path.isfile(f'{problem}/problem.yaml'):
220221
print('Problem directory not found or doesn\'t contain a problem.yaml.')
221222
exit(3)
222-
os.system(f'cd {problem} && zip -r \'../{problem}\' -- .timelimit *')
223+
zip_command = f"zip -r '../{problem}' -- .timelimit *"
224+
process = subprocess.Popen(zip_command, cwd=problem, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, shell=True)
225+
226+
lastLine = None
227+
for line in process.stdout:
228+
if lastLine:
229+
sys.stdout.write("\r" + " " * len(lastLine))
230+
sys.stdout.write(f"\r{line.strip()}")
231+
sys.stdout.flush()
232+
lastLine = line
233+
234+
exit_code = process.wait()
235+
if exit_code == 0:
236+
if lastLine:
237+
sys.stdout.write("\r" + " " * len(lastLine) + "\r")
238+
else:
239+
print(f"\nZipping problem failed with exit code: {exit_code}")
223240

224241
if ((not confirmIndividually) or dj_utils.confirm(f'Ready to import problem \'{problem}\' to problem={problem}. Continue?', True)):
225242
print(f'Uploading problem \'{problem}\', please be patient, this may take a while.')
226243
response = dj_utils.upload_file(
227244
f'contests/{cid}/problems', 'zip', f'{problem}.zip', {'problem': problem})
228-
print(json.dumps(response, indent=4))
245+
if response and 'problem_id' in response:
246+
print(f'Problem imported with ID {response["problem_id"]}:')
247+
if 'messages' in response:
248+
messages = response['messages']
249+
types = {'info': '🛈 ', 'warning': '⚠️ ', 'danger': '🚨'}
250+
for t,e in types.items():
251+
if t in messages and messages[t]:
252+
print(f' {e} {t.capitalize()}:')
253+
for message in messages[t]:
254+
print(f' - {message}')
255+
else:
256+
print(json.dumps(response, indent=4))
229257
else:
230258
print('Skipping contest import.')
231259
else:

0 commit comments

Comments
 (0)