Skip to content

Commit 1318e87

Browse files
authoredSep 9, 2024··
Use oicompare as default checker (#261)
1 parent bfbd798 commit 1318e87

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed
 

‎src/sinol_make/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import argcomplete
66

77
from sinol_make import util, sio2jail
8-
from sinol_make.helpers import cache
8+
from sinol_make.helpers import cache, oicompare
99

1010
# Required for side effects
1111
from sinol_make.task_type.normal import NormalTaskType # noqa
1212
from sinol_make.task_type.interactive import InteractiveTaskType # noqa
1313

1414

15-
__version__ = "1.8.2"
15+
__version__ = "1.8.3"
1616

1717

1818
def configure_parsers():
@@ -76,6 +76,7 @@ def main_exn():
7676
parser.print_help()
7777
exit(1)
7878
check_sio2jail()
79+
oicompare.check_and_download()
7980

8081
for curr_args in arguments:
8182
args = parser.parse_args(curr_args)

‎src/sinol_make/helpers/oicompare.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import requests
3+
import subprocess
4+
5+
from sinol_make import util
6+
7+
8+
__OICOMAPRE_VERSION = 'v1.0.2'
9+
10+
11+
def get_path():
12+
return os.path.expanduser('~/.local/bin/oicompare')
13+
14+
15+
def check_installed():
16+
path = get_path()
17+
if not os.path.exists(path):
18+
return False
19+
try:
20+
output = subprocess.run([path, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21+
except PermissionError:
22+
return False
23+
if output.returncode != 0:
24+
return False
25+
if output.stdout.decode().strip() != f'oicompare version {__OICOMAPRE_VERSION[1:]}':
26+
return False
27+
return True
28+
29+
30+
def download_oicomapare():
31+
url = f'https://github.com/sio2project/oicompare/releases/download/{__OICOMAPRE_VERSION}/oicompare'
32+
if util.is_macos_arm():
33+
url += '-arm64'
34+
path = get_path()
35+
os.makedirs(os.path.dirname(path), exist_ok=True)
36+
manual_download_msg = ("You can try downloading it manually from "
37+
f"https://github.com/sio2project/oicompare/releases/tag/{__OICOMAPRE_VERSION}/ and placing "
38+
f"it in ~/.local/bin/oicomapre")
39+
try:
40+
request = requests.get(url)
41+
except requests.exceptions.ConnectionError:
42+
util.exit_with_error("Couldn't download oicompare (couldn't connect). " + manual_download_msg)
43+
if request.status_code != 200:
44+
util.exit_with_error(f"Couldn't download oicompare (returned status code: {request.status_code}). "
45+
+ manual_download_msg)
46+
with open(path, 'wb') as f:
47+
f.write(request.content)
48+
os.chmod(path, 0o755)
49+
50+
51+
def check_and_download():
52+
if check_installed():
53+
return
54+
download_oicomapare()
55+
if not check_installed():
56+
util.exit_with_error("Couldn't download oicompare. Please try again later or download it manually.")

‎src/sinol_make/task_type/__init__.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sinol_make import util
77
from sinol_make.executors.sio2jail import Sio2jailExecutor
88
from sinol_make.executors.time import TimeExecutor
9-
from sinol_make.helpers import package_util, paths, cache
9+
from sinol_make.helpers import package_util, paths, cache, oicompare
1010
from sinol_make.helpers.classinit import RegisteredSubclassesBase
1111
from sinol_make.interfaces.Errors import CheckerException
1212
from sinol_make.structs.status_structs import ExecutionResult
@@ -162,6 +162,23 @@ def _run_diff(self, output_file_path, answer_file_path) -> Tuple[bool, Fraction,
162162
else:
163163
return False, Fraction(0, 1), ""
164164

165+
def _run_oicompare(self, output_file_path, answer_file_path) -> Tuple[bool, Fraction, str]:
166+
path = oicompare.get_path()
167+
proc = subprocess.Popen([path, output_file_path, answer_file_path, 'english_abbreviated'],
168+
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
169+
proc.wait()
170+
output, stderr = proc.communicate()
171+
if proc.returncode == 0:
172+
return True, Fraction(100, 1), ""
173+
elif proc.returncode == 1:
174+
return False, Fraction(0, 1), output.decode('utf-8').strip()
175+
else:
176+
raise CheckerException(f"!!! oicompare failed with code {proc.returncode}. This is a huge bug, please report"
177+
f" it here https://github.com/sio2project/sinol-make/issues/new/choose and provide "
178+
f"these files: {output_file_path}, {answer_file_path}.\n"
179+
f"Output: {output.decode('utf-8').strip()}\n"
180+
f"Stderr: {stderr.decode('utf-8').strip()}")
181+
165182
def check_output(self, input_file_path, output_file_path, answer_file_path) -> Tuple[bool, Fraction, str]:
166183
"""
167184
Runs the checker (or runs diff) and returns a tuple of three values:
@@ -171,6 +188,8 @@ def check_output(self, input_file_path, output_file_path, answer_file_path) -> T
171188
"""
172189
if self.has_checker:
173190
return self._run_checker(input_file_path, output_file_path, answer_file_path)
191+
elif oicompare.check_installed():
192+
return self._run_oicompare(output_file_path, answer_file_path)
174193
else:
175194
return self._run_diff(output_file_path, answer_file_path)
176195

‎tests/conftest.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import multiprocessing as mp
99

1010
from sinol_make import util
11-
from sinol_make.helpers import compile, paths, cache
11+
from sinol_make.helpers import compile, paths, cache, oicompare
1212
from sinol_make.interfaces.Errors import CompilationError
1313

1414

@@ -91,6 +91,8 @@ def pytest_configure(config):
9191
except FileNotFoundError:
9292
pass
9393

94+
oicompare.check_and_download()
95+
9496

9597
def pytest_generate_tests(metafunc):
9698
if "time_tool" in metafunc.fixturenames:

0 commit comments

Comments
 (0)
Please sign in to comment.