Skip to content

Commit 0414353

Browse files
committed
Allow printing from the submit client
To allow easy deployments, have printing and submitting in the same python file such that we can share code without need to import library files. For convenience of teams, you probably want to alias `printfile` to the submit client with predefined `--print` argument to prevent any accidental submission.
1 parent 68c297e commit 0414353

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

submit/submit

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#
99

1010
import argparse
11+
import base64
1112
import datetime
1213
import json
1314
import logging
@@ -299,6 +300,56 @@ Submit multiple files (the problem and language are taken from the first):
299300
return "\n\n".join(part for part in epilog_parts if part)
300301

301302

303+
def do_api_print():
304+
'''Submit to the API for printing with the given data.'''
305+
306+
if len(filenames) != 1:
307+
error('You can only print a single file')
308+
filename = filenames[0]
309+
310+
with open(filename, 'rb') as file:
311+
data = {
312+
'original_name': filename,
313+
'file_contents': base64.b64encode(file.read()),
314+
}
315+
if my_language:
316+
data['language'] = my_language['name']
317+
if entry_point:
318+
data['entry_point'] = entry_point
319+
320+
url = f"{baseurl}api/{api_version}printing/team"
321+
logging.info(f'connecting to {url}')
322+
323+
response = requests.post(url, data=data, headers=headers)
324+
325+
logging.debug(f"API call 'printing' returned:\n{response.text}")
326+
327+
# The connection worked, but we may have received an HTTP error
328+
if response.status_code >= 300:
329+
print(response.text)
330+
if response.status_code == 401:
331+
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
332+
else:
333+
raise RuntimeError(f'Printing failed (code {response.status_code})')
334+
335+
# We got a successful HTTP response. It worked.
336+
# But check that we indeed received a success response.
337+
338+
try:
339+
result = json.loads(response.text)
340+
except json.decoder.JSONDecodeError as e:
341+
error(f'Parsing DOMjudge\'s API output failed: {e}')
342+
343+
if not isinstance(result, dict) or 'success' not in result:
344+
error('DOMjudge\'s API returned unexpected JSON data.')
345+
346+
if result['success']:
347+
print("DOMjudge reported a successful print job.")
348+
else:
349+
# Should not happen, as the status code should've been >= 300
350+
print(f"DOMjudge reported a printing error: {result['output']}")
351+
352+
302353
def do_api_submit():
303354
'''Submit to the API with the given data.'''
304355

@@ -374,6 +425,7 @@ parser.add_argument('-c', '--contest', help='''submit for contest with ID or sho
374425
Defaults to the value of the
375426
environment variable 'SUBMITCONTEST'.
376427
Mandatory when more than one contest is active.''')
428+
parser.add_argument('-P', '--print', help='submit the file for printing instead of submission', action='store_true')
377429
parser.add_argument('-p', '--problem', help='submit for problem with ID or label PROBLEM', default='')
378430
parser.add_argument('-l', '--language', help='submit in language with ID LANGUAGE', default='')
379431
parser.add_argument('-e', '--entry_point', help='set an explicit entry_point, e.g. the java main class')
@@ -530,7 +582,7 @@ for language in languages:
530582
if my_language:
531583
break
532584

533-
if not my_language:
585+
if not my_language and not args.print:
534586
usage('No known language specified or detected.')
535587

536588
# Check for problem matching ID or label.
@@ -540,7 +592,7 @@ for problem in problems:
540592
my_problem = problem
541593
break
542594

543-
if not my_problem:
595+
if not my_problem and not args.print:
544596
usage('No known problem specified or detected.')
545597

546598
# Guess entry point if not already specified.
@@ -556,11 +608,16 @@ if not entry_point and my_language['entry_point_required']:
556608
error('Entry point required but not specified nor detected.')
557609

558610
logging.debug(f"contest is `{my_contest['shortname']}'")
559-
logging.debug(f"problem is `{my_problem['label']}'")
611+
if not args.print:
612+
logging.debug(f"problem is `{my_problem['label']}'")
560613
logging.debug(f"language is `{my_language['name']}'")
561614
logging.debug(f"entry_point is `{entry_point or '<None>'}'")
562615
logging.debug(f"url is `{baseurl}'")
563616

617+
if args.print:
618+
do_api_print()
619+
exit(0)
620+
564621
if not args.assume_yes:
565622
print('Submission information:')
566623
if len(filenames) == 1:

0 commit comments

Comments
 (0)