Skip to content

Commit

Permalink
executors: add basic autodetection from filename
Browse files Browse the repository at this point in the history
Ref #967
  • Loading branch information
Xyene committed Dec 6, 2021
1 parent 2871cb3 commit 1054773
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
8 changes: 1 addition & 7 deletions dmoj/commands/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ def execute(self, line: str) -> None:
raise InvalidCommandException(f"unknown problem '{problem_id}'")
elif not language_id:
if source_file:
filename, dot, ext = source_file.partition('.')
if not ext:
raise InvalidCommandException('invalid file name')
else:
# TODO: this should be a proper lookup elsewhere
ext = ext.upper()
language_id = {'PY': 'PY2', 'CPP': 'CPP11', 'JAVA': 'JAVA8'}.get(ext, ext)
language_id = executors.from_filename(source_file).name
else:
raise InvalidCommandException('no language is selected')
elif language_id not in executors:
Expand Down
22 changes: 22 additions & 0 deletions dmoj/executors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
executors: Dict[str, Any] = {}


def by_ext(ext : str):
ext = ext.lower()

for name, executor in executors.items():
if name.lower() == ext:
return executor

for executor in executors.values():
if executor.ext == ext:
return executor

return KeyError('no executor for extension "%s"' % ext)


def from_filename(filename : str):
_, _, ext = filename.partition('.')
if not ext:
raise KeyError('invalid file name')

return by_ext(ext)


def get_available():
return get_available_modules(
_reexecutor, os.path.dirname(__file__), only_executors, exclude_executors | _unsupported_executors
Expand Down
7 changes: 5 additions & 2 deletions dmoj/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from yaml.parser import ParserError
from yaml.scanner import ScannerError

from dmoj import checkers
from dmoj import checkers, executors
from dmoj.config import ConfigNode, InvalidInitException
from dmoj.judgeenv import env, get_problem_root
from dmoj.utils.helper_files import compile_with_auxiliary_files, parse_helper_file_error
Expand Down Expand Up @@ -272,7 +272,7 @@ def _run_generator(self, gen, args=None):
time_limit = env.generator_time_limit
memory_limit = env.generator_memory_limit
compiler_time_limit = env.generator_compiler_time_limit
lang = None # Default to C/C++
lang = None

base = get_problem_root(self.problem.id)
if isinstance(gen, str):
Expand Down Expand Up @@ -300,6 +300,9 @@ def _run_generator(self, gen, args=None):
if not isinstance(filenames, list):
filenames = [filenames]

if not lang:
lang = executors.from_filename(filenames[0])

filenames = [os.path.abspath(os.path.join(base, name)) for name in filenames]
executor = compile_with_auxiliary_files(filenames, flags, lang, compiler_time_limit)

Expand Down
4 changes: 1 addition & 3 deletions testsuite/generator_python/init.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
generator:
language: PY2
source: gen.py
generator: gen.py
test_cases:
- {generator_args: [1, 2], points: 1}
- {generator_args: [10, 20], points: 1}
Expand Down

0 comments on commit 1054773

Please sign in to comment.