Skip to content

Commit dce7363

Browse files
committed
fix: fix bugs and refactor code
1 parent af07005 commit dce7363

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

codeforces/cf_run.py

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python
22

33
import argparse
4+
import shlex
45
import subprocess
6+
import tempfile
57
import time
68

79
import colorama
@@ -10,27 +12,42 @@
1012

1113
colorama.init(autoreset=True)
1214

15+
1316
def main(argv=None):
1417
parser = argparse.ArgumentParser()
1518

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+
)
2625

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+
)
3032

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+
)
3451

3552
if argv:
3653
args = parser.parse_args(argv)
@@ -40,8 +57,7 @@ def main(argv=None):
4057
args.timeout = None if args.timeout == -1 else args.timeout
4158

4259
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)
4561

4662
print(title)
4763
print("time limit per test:", time_limit)
@@ -50,15 +66,22 @@ def main(argv=None):
5066
print()
5167

5268
for inp, ans in sample_tests:
69+
tmp = tempfile.TemporaryFile('w')
70+
tmp.write(inp)
71+
tmp.seek(0)
72+
5373
start = time.time()
5474

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
6285

6386
time_used = time.time() - start
6487

@@ -70,7 +93,11 @@ def main(argv=None):
7093
print(inp)
7194

7295
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)
74101

75102
print(colorama.Style.BRIGHT + "Jury's answer")
76103
print(ans)

0 commit comments

Comments
 (0)