Skip to content

Commit 9e04151

Browse files
authored
Fix python3.10 issues with gen command (#171)
* Split gen_util.py into ingen_ and outgen_util * Fix code * Bump version
1 parent 3446333 commit 9e04151

File tree

9 files changed

+112
-109
lines changed

9 files changed

+112
-109
lines changed

src/sinol_make/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sinol_make import util, oiejq
1010

1111

12-
__version__ = "1.5.18"
12+
__version__ = "1.5.19"
1313

1414

1515
def configure_parsers():

src/sinol_make/commands/export/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import yaml
88

99
from sinol_make import util
10+
from sinol_make.commands.ingen.ingen_util import get_ingen, compile_ingen, run_ingen, ingen_exists
1011
from sinol_make.helpers import package_util, parsers, paths
11-
from sinol_make.commands.gen import gen_util
1212
from sinol_make.interfaces.BaseCommand import BaseCommand
1313

1414

@@ -32,17 +32,17 @@ def get_generated_tests(self):
3232
Returns list of generated tests.
3333
Executes ingen to check what tests are generated.
3434
"""
35-
if not gen_util.ingen_exists(self.task_id):
35+
if not ingen_exists(self.task_id):
3636
return []
3737

3838
working_dir = paths.get_cache_path('export', 'tests')
3939
if os.path.exists(working_dir):
4040
shutil.rmtree(working_dir)
4141
os.makedirs(working_dir)
4242

43-
ingen_path = gen_util.get_ingen(self.task_id)
44-
ingen_exe = gen_util.compile_ingen(ingen_path, self.args, self.args.weak_compilation_flags)
45-
if not gen_util.run_ingen(ingen_exe, working_dir):
43+
ingen_path = get_ingen(self.task_id)
44+
ingen_exe = compile_ingen(ingen_path, self.args, self.args.weak_compilation_flags)
45+
if not run_ingen(ingen_exe, working_dir):
4646
util.exit_with_error('Failed to run ingen.')
4747

4848
tests = glob.glob(os.path.join(working_dir, f'{self.task_id}*.in'))

src/sinol_make/commands/gen/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from sinol_make import util
44
from sinol_make.commands.ingen import Command as IngenCommand
55
from sinol_make.commands.outgen import Command as OutgenCommand
6-
from sinol_make.commands.gen import gen_util
76
from sinol_make.helpers import parsers
87
from sinol_make.interfaces.BaseCommand import BaseCommand
98

src/sinol_make/commands/ingen/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33

44
from sinol_make import util
5-
from sinol_make.commands.gen import gen_util
5+
from sinol_make.commands.ingen.ingen_util import get_ingen, compile_ingen, run_ingen
66
from sinol_make.helpers import parsers, package_util
77
from sinol_make.interfaces.BaseCommand import BaseCommand
88

@@ -37,11 +37,11 @@ def run(self, args: argparse.Namespace):
3737
self.task_id = package_util.get_task_id()
3838
package_util.validate_test_names(self.task_id)
3939
util.change_stack_size_to_unlimited()
40-
self.ingen = gen_util.get_ingen(self.task_id, args.ingen_path)
40+
self.ingen = get_ingen(self.task_id, args.ingen_path)
4141
print(util.info(f'Using ingen file {os.path.basename(self.ingen)}'))
42-
self.ingen_exe = gen_util.compile_ingen(self.ingen, self.args, self.args.weak_compilation_flags)
42+
self.ingen_exe = compile_ingen(self.ingen, self.args, self.args.weak_compilation_flags)
4343

44-
if gen_util.run_ingen(self.ingen_exe):
44+
if run_ingen(self.ingen_exe):
4545
print(util.info('Successfully generated input files.'))
4646
else:
4747
util.exit_with_error('Failed to generate input files.')

src/sinol_make/commands/gen/gen_util.py renamed to src/sinol_make/commands/ingen/ingen_util.py

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import glob
2-
import os
31
import stat
4-
import argparse
5-
import signal
62
import subprocess
7-
import sys
3+
4+
import argparse
5+
import os
86

97
from sinol_make import util
10-
from sinol_make.helpers import compiler, package_util, compile
8+
from sinol_make.helpers import package_util, compiler, compile
119

1210

1311
def ingen_exists(task_id):
@@ -69,34 +67,6 @@ def compile_ingen(ingen_path: str, args: argparse.Namespace, weak_compilation_fl
6967
return ingen_exe
7068

7169

72-
def get_correct_solution(task_id):
73-
"""
74-
Returns path to correct solution for given task.
75-
:param task_id: task id, for example abc
76-
:return: path to correct solution or None if not found
77-
"""
78-
correct_solution = package_util.get_files_matching_pattern(task_id, f'{task_id}.*')
79-
if len(correct_solution) == 0:
80-
util.exit_with_error(f'Correct solution for task {task_id} does not exist.')
81-
return correct_solution[0]
82-
83-
84-
def compile_correct_solution(solution_path: str, args: argparse.Namespace, weak_compilation_flags=False):
85-
"""
86-
Compiles correct solution and returns path to compiled executable.
87-
"""
88-
compilers = compiler.verify_compilers(args, [solution_path])
89-
correct_solution_exe, compile_log_path = compile.compile_file(solution_path, package_util.get_executable(solution_path), compilers,
90-
weak_compilation_flags)
91-
if correct_solution_exe is None:
92-
util.exit_with_error('Failed compilation of correct solution.',
93-
lambda: compile.print_compile_log(compile_log_path))
94-
else:
95-
print(util.info('Successfully compiled correct solution.'))
96-
97-
return correct_solution_exe
98-
99-
10070
def run_ingen(ingen_exe, working_dir=None):
10171
"""
10272
Runs ingen and generates all input files.
@@ -121,35 +91,3 @@ def run_ingen(ingen_exe, working_dir=None):
12191
exit_code = process.returncode
12292

12393
return exit_code == 0
124-
125-
126-
def generate_output(arguments):
127-
"""
128-
Generates output file for given input file.
129-
:param arguments: arguments for output generation (type OutputGenerationArguments)
130-
:return: True if the output was successfully generated, False otherwise
131-
"""
132-
input_test = arguments.input_test
133-
output_test = arguments.output_test
134-
correct_solution_exe = arguments.correct_solution_exe
135-
136-
input_file = open(input_test, 'r')
137-
output_file = open(output_test, 'w')
138-
process = subprocess.Popen([correct_solution_exe], stdin=input_file, stdout=output_file, preexec_fn=os.setsid)
139-
previous_sigint_handler = signal.getsignal(signal.SIGINT)
140-
141-
def sigint_handler(signum, frame):
142-
try:
143-
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
144-
except ProcessLookupError:
145-
pass
146-
sys.exit(1)
147-
signal.signal(signal.SIGINT, sigint_handler)
148-
149-
process.wait()
150-
signal.signal(signal.SIGINT, previous_sigint_handler)
151-
exit_code = process.returncode
152-
input_file.close()
153-
output_file.close()
154-
155-
return exit_code == 0

src/sinol_make/commands/outgen/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import multiprocessing as mp
77

88
from sinol_make import util
9-
from sinol_make.commands.gen import gen_util
9+
from sinol_make.commands.outgen.outgen_util import get_correct_solution, compile_correct_solution, generate_output
1010
from sinol_make.structs.gen_structs import OutputGenerationArguments
1111
from sinol_make.helpers import parsers, package_util, cache
1212
from sinol_make.interfaces.BaseCommand import BaseCommand
@@ -43,7 +43,7 @@ def generate_outputs(self, outputs_to_generate):
4343

4444
with mp.Pool(self.args.cpus) as pool:
4545
results = []
46-
for i, result in enumerate(pool.imap(gen_util.generate_output, arguments)):
46+
for i, result in enumerate(pool.imap(generate_output, arguments)):
4747
results.append(result)
4848
if result:
4949
print(util.info(f'Successfully generated output file {os.path.basename(arguments[i].output_test)}'))
@@ -93,14 +93,14 @@ def run(self, args: argparse.Namespace):
9393
package_util.validate_test_names(self.task_id)
9494
util.change_stack_size_to_unlimited()
9595
cache.check_correct_solution(self.task_id)
96-
self.correct_solution = gen_util.get_correct_solution(self.task_id)
96+
self.correct_solution = get_correct_solution(self.task_id)
9797

9898
md5_sums, outputs_to_generate = self.calculate_md5_sums()
9999
if len(outputs_to_generate) == 0:
100100
print(util.info('All output files are up to date.'))
101101
else:
102-
self.correct_solution_exe = gen_util.compile_correct_solution(self.correct_solution, self.args,
103-
self.args.weak_compilation_flags)
102+
self.correct_solution_exe = compile_correct_solution(self.correct_solution, self.args,
103+
self.args.weak_compilation_flags)
104104
self.generate_outputs(outputs_to_generate)
105105
with open(os.path.join(os.getcwd(), 'in', '.md5sums'), 'w') as f:
106106
yaml.dump(md5_sums, f)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import signal
3+
import subprocess
4+
import sys
5+
6+
import argparse
7+
8+
from sinol_make import util
9+
from sinol_make.helpers import package_util, compiler, compile
10+
11+
12+
def get_correct_solution(task_id):
13+
"""
14+
Returns path to correct solution for given task.
15+
:param task_id: task id, for example abc
16+
:return: path to correct solution or None if not found
17+
"""
18+
correct_solution = package_util.get_files_matching_pattern(task_id, f'{task_id}.*')
19+
if len(correct_solution) == 0:
20+
util.exit_with_error(f'Correct solution for task {task_id} does not exist.')
21+
return correct_solution[0]
22+
23+
24+
def compile_correct_solution(solution_path: str, args: argparse.Namespace, weak_compilation_flags=False):
25+
"""
26+
Compiles correct solution and returns path to compiled executable.
27+
"""
28+
compilers = compiler.verify_compilers(args, [solution_path])
29+
correct_solution_exe, compile_log_path = compile.compile_file(solution_path, package_util.get_executable(solution_path), compilers,
30+
weak_compilation_flags)
31+
if correct_solution_exe is None:
32+
util.exit_with_error('Failed compilation of correct solution.',
33+
lambda: compile.print_compile_log(compile_log_path))
34+
else:
35+
print(util.info('Successfully compiled correct solution.'))
36+
37+
return correct_solution_exe
38+
39+
40+
def generate_output(arguments):
41+
"""
42+
Generates output file for given input file.
43+
:param arguments: arguments for output generation (type OutputGenerationArguments)
44+
:return: True if the output was successfully generated, False otherwise
45+
"""
46+
input_test = arguments.input_test
47+
output_test = arguments.output_test
48+
correct_solution_exe = arguments.correct_solution_exe
49+
50+
input_file = open(input_test, 'r')
51+
output_file = open(output_test, 'w')
52+
process = subprocess.Popen([correct_solution_exe], stdin=input_file, stdout=output_file, preexec_fn=os.setsid)
53+
previous_sigint_handler = signal.getsignal(signal.SIGINT)
54+
55+
def sigint_handler(signum, frame):
56+
try:
57+
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
58+
except ProcessLookupError:
59+
pass
60+
sys.exit(1)
61+
signal.signal(signal.SIGINT, sigint_handler)
62+
63+
process.wait()
64+
signal.signal(signal.SIGINT, previous_sigint_handler)
65+
exit_code = process.returncode
66+
input_file.close()
67+
output_file.close()
68+
69+
return exit_code == 0

tests/commands/gen/test_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from sinol_make import util as sm_util
88
from sinol_make.commands.gen import Command
99
from sinol_make.commands.ingen import Command as IngenCommand
10+
from sinol_make.commands.ingen.ingen_util import get_ingen
1011
from sinol_make.commands.outgen import Command as OutgenCommand
11-
from sinol_make.commands.gen import gen_util
1212
from sinol_make.helpers import package_util, paths, cache
1313
from tests.fixtures import *
1414
from tests import util
@@ -108,7 +108,7 @@ def test_shell_ingen_unchanged(create_package):
108108
"""
109109
package_path = create_package
110110
task_id = package_util.get_task_id()
111-
shell_ingen_path = gen_util.get_ingen(task_id)
111+
shell_ingen_path = get_ingen(task_id)
112112
assert os.path.splitext(shell_ingen_path)[1] == ".sh"
113113
edited_time = os.path.getmtime(shell_ingen_path)
114114
simple_run()

0 commit comments

Comments
 (0)