1
1
#!/usr/bin/env python
2
2
3
3
import argparse
4
+ import shlex
4
5
import subprocess
6
+ import tempfile
5
7
import time
6
8
7
9
import colorama
10
12
11
13
colorama .init (autoreset = True )
12
14
15
+
13
16
def main (argv = None ):
14
17
parser = argparse .ArgumentParser ()
15
18
16
- parser .add_argument ('contestId' , type = int ,
17
- help = ("Id of the contest. It is not the round number. "
18
- "It can be seen in contest URL." ))
19
-
20
- parser .add_argument ('index' , type = str ,
21
- help = ("A letter or a letter followed by a digit, that "
22
- "represent a problem index in a contest." ))
23
-
24
- parser .add_argument ('program' , type = str ,
25
- help = "Path to executable that needs to be tested" )
19
+ parser .add_argument (
20
+ 'contestId' ,
21
+ type = int ,
22
+ help =
23
+ "Id of the contest. It is not the round number. It can be seen in contest URL."
24
+ )
26
25
27
- parser .add_argument ('-t' , '--timeout' , type = int , default = 10 ,
28
- help = ("Timeout for program in seconds, -1 for no time "
29
- "limit (default: 10)" ))
26
+ parser .add_argument (
27
+ 'index' ,
28
+ type = str ,
29
+ help =
30
+ "A letter or a letter followed by a digit, that represent a problem index in a contest."
31
+ )
30
32
31
- parser .add_argument ('-g' , '--gym' , action = 'store_true' ,
32
- help = ("If true open gym contest instead of regular "
33
- "contest. (default: false)" ))
33
+ parser .add_argument (
34
+ 'program' , type = str , help = "Path to executable that needs to be tested" )
35
+
36
+ parser .add_argument (
37
+ '-t' ,
38
+ '--timeout' ,
39
+ type = int ,
40
+ default = 10 ,
41
+ help =
42
+ "Timeout for program in seconds, -1 for no time limit (default: 10)" )
43
+
44
+ parser .add_argument (
45
+ '-g' ,
46
+ '--gym' ,
47
+ action = 'store_true' ,
48
+ help =
49
+ "If true open gym contest instead of regular contest. (default: false)"
50
+ )
34
51
35
52
if argv :
36
53
args = parser .parse_args (argv )
@@ -40,8 +57,7 @@ def main(argv=None):
40
57
args .timeout = None if args .timeout == - 1 else args .timeout
41
58
42
59
title , time_limit , memory_limit , sample_tests = problem .get_info (
43
- args .contestId , args .index , gym = args .gym
44
- )
60
+ args .contestId , args .index , gym = args .gym )
45
61
46
62
print (title )
47
63
print ("time limit per test:" , time_limit )
@@ -50,15 +66,22 @@ def main(argv=None):
50
66
print ()
51
67
52
68
for inp , ans in sample_tests :
69
+ tmp = tempfile .TemporaryFile ('w' )
70
+ tmp .write (inp )
71
+ tmp .seek (0 )
72
+
53
73
start = time .time ()
54
74
55
- out = subprocess .run (
56
- args .program ,
57
- shell = True ,
58
- input = inp .encode ('utf-8' ),
59
- capture_output = True ,
60
- timeout = args .timeout
61
- ).stdout .decode ('utf-8' )
75
+ proc = subprocess .run (
76
+ shlex .split (args .program ),
77
+ stdin = tmp ,
78
+ stdout = subprocess .PIPE ,
79
+ stderr = subprocess .PIPE ,
80
+ timeout = args .timeout ,
81
+ universal_newlines = True )
82
+
83
+ stdout = proc .stdout
84
+ stderr = proc .stderr
62
85
63
86
time_used = time .time () - start
64
87
@@ -70,7 +93,11 @@ def main(argv=None):
70
93
print (inp )
71
94
72
95
print (colorama .Style .BRIGHT + "Participant's output" )
73
- print (out )
96
+ print (stdout )
97
+
98
+ if stderr :
99
+ print (colorama .Style .BRIGHT + "stderr" )
100
+ print (stderr )
74
101
75
102
print (colorama .Style .BRIGHT + "Jury's answer" )
76
103
print (ans )
0 commit comments