8
8
#
9
9
10
10
import argparse
11
+ import base64
11
12
import datetime
12
13
import json
13
14
import logging
@@ -299,6 +300,56 @@ Submit multiple files (the problem and language are taken from the first):
299
300
return "\n \n " .join (part for part in epilog_parts if part )
300
301
301
302
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
+
302
353
def do_api_submit ():
303
354
'''Submit to the API with the given data.'''
304
355
@@ -374,6 +425,7 @@ parser.add_argument('-c', '--contest', help='''submit for contest with ID or sho
374
425
Defaults to the value of the
375
426
environment variable 'SUBMITCONTEST'.
376
427
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' )
377
429
parser .add_argument ('-p' , '--problem' , help = 'submit for problem with ID or label PROBLEM' , default = '' )
378
430
parser .add_argument ('-l' , '--language' , help = 'submit in language with ID LANGUAGE' , default = '' )
379
431
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:
530
582
if my_language :
531
583
break
532
584
533
- if not my_language :
585
+ if not my_language and not args . print :
534
586
usage ('No known language specified or detected.' )
535
587
536
588
# Check for problem matching ID or label.
@@ -540,7 +592,7 @@ for problem in problems:
540
592
my_problem = problem
541
593
break
542
594
543
- if not my_problem :
595
+ if not my_problem and not args . print :
544
596
usage ('No known problem specified or detected.' )
545
597
546
598
# Guess entry point if not already specified.
@@ -556,11 +608,16 @@ if not entry_point and my_language['entry_point_required']:
556
608
error ('Entry point required but not specified nor detected.' )
557
609
558
610
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' ]} '" )
560
613
logging .debug (f"language is `{ my_language ['name' ]} '" )
561
614
logging .debug (f"entry_point is `{ entry_point or '<None>' } '" )
562
615
logging .debug (f"url is `{ baseurl } '" )
563
616
617
+ if args .print :
618
+ do_api_print ()
619
+ exit (0 )
620
+
564
621
if not args .assume_yes :
565
622
print ('Submission information:' )
566
623
if len (filenames ) == 1 :
0 commit comments