import os
import subprocess

from dmoj.error import InternalError
from dmoj.judgeenv import env, get_problem_root
from dmoj.result import CheckerResult
from dmoj.utils.aux_files import compile_with_auxiliary_files, mktemp
from dmoj.utils.unicode import utf8text

executor = None


def get_executor(files, lang, compiler_time_limit, problem_id):
    global executor

    if executor is None:
        filenames = [os.path.join(get_problem_root(problem_id), f) for f in files]
        executor = compile_with_auxiliary_files(filenames, lang, compiler_time_limit)

    return executor


def check(process_output, judge_output, judge_input, problem_id,
          files, lang, time_limit=env['generator_time_limit'], memory_limit=env['generator_memory_limit'],
          compiler_time_limit=env['generator_compiler_limit'], feedback=True,
          point_value=None, **kwargs) -> CheckerResult:
    executor = get_executor(files, lang, compiler_time_limit, problem_id)

    with mktemp(judge_input) as input_file, mktemp(process_output) as output_file, mktemp(judge_output) as judge_file:
        process = executor.launch(input_file.name, output_file.name, judge_file.name, stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE, memory=memory_limit, time=time_limit)

        proc_output, error = map(utf8text, process.communicate())

        # We use the testlib.h return codes
        AC = 0
        WA = 1
        PE = 2
        IE = 3

        if process.returncode == AC:
            if feedback:
                return CheckerResult(True, point_value, feedback=proc_output)
            else:
                return CheckerResult(True, point_value)
        elif process.returncode in (WA, PE):
            if feedback:
                return CheckerResult(False, 0, feedback=proc_output)
            else:
                return CheckerResult(False, 0, feedback='Presentation Error' if process.returncode == PE else '')
        else:
            if process.returncode == IE:
                error = 'checker failed assertion with message %s' % proc_output
            else:
                error = 'checker returned unexpected return code %d with stderr %s' % (process.returncode, error)
            raise InternalError(error)

@@ -384,16 +384,84 @@
 s None:%0A
+        if not isinstance(files, list):%0A            files = %5Bfiles%5D%0A
         

Allow `bridged` checker to accept a string for `files` (#601)