Пример #1
0
    def output(cls, *files, **kwargs):
        arg_pattern = ('std',
                       ('grader', DEFAULT_GRADER),
                       ('max_workers', -1),
                       ('job_pool', None),
                       ('stop_on_incorrect', None))
        kwargs = unpack_kwargs('output', kwargs, arg_pattern)

        std = kwargs['std']
        grader = kwargs['grader']
        job_pool = kwargs['job_pool']
        if kwargs['stop_on_incorrect'] is not None:
            log.warn("parameter stop_on_incorrect "
                     "is deprecated and has no effect.")

        cls._thread_pool_executor(cls.output, files, kwargs)

        def get_std():
            return cls._process_file(std)[1]

        std = cls._do_tasks(job_pool, get_std)

        def do(file):
            (file_name, content) = cls._process_file(file)
            cls._compare_two(file_name, content, std, grader)

        cls._do_tasks(job_pool, do, *files)
Пример #2
0
    def program(cls, *programs, **kwargs):
        arg_pattern = ('input',
                       ('std', None),
                       ('std_program', None),
                       ('grader', DEFAULT_GRADER),
                       ('max_workers', -1),
                       ('job_pool', None),
                       ('stop_on_incorrect', None))
        kwargs = unpack_kwargs('program', kwargs, arg_pattern)

        input = kwargs['input']
        std = kwargs['std']
        std_program = kwargs['std_program']
        grader = kwargs['grader']
        job_pool = kwargs['job_pool']
        if kwargs['stop_on_incorrect'] is not None:
            log.warn("parameter stop_on_incorrect is "
                     "deprecated and has no effect.")

        cls._thread_pool_executor(cls.program, programs, kwargs)

        if not isinstance(input, IO):
            raise TypeError("expect {}, got {}".format(
                        type(IO).__name__, type(input).__name__))

        input.flush_buffer()
        input.input_file.seek(0)

        if std_program is not None:
            def get_std():
                return cls._do_execute(std_program, input)
            std = cls._do_tasks(job_pool, get_std)

        elif std is not None:
            def get_std():
                return cls._process_file(std)[1]
            std = cls._do_tasks(job_pool, get_std)

        else:
            raise TypeError("program() missing 1 required non-None"
                            " keyword-only argument: `std` or `std_program`")

        def do(program_name):
            timeout = None
            if (list_like(program_name) and
                len(program_name) == 2 and
                int_like(program_name[-1])):
                program_name, timeout = program_name

            content = cls._do_execute(program_name, input, timeout=timeout)

            cls._compare_two(program_name, content, std, grader)

        cls._do_tasks(job_pool, do, *programs)
Пример #3
0
    def output(cls, *files, **kwargs):
        kwargs = unpack_kwargs('output', kwargs,
                               ('std', ('grader', DEFAULT_GRADER),
                                ('max_workers', -1), ('job_pool', None),
                                ('stop_on_incorrect', None)))
        std = kwargs['std']
        grader = kwargs['grader']
        max_workers = kwargs['max_workers']
        job_pool = kwargs['job_pool']
        if kwargs['stop_on_incorrect'] is not None:
            log.warn(
                "parameter stop_on_incorrect is deprecated and has no effect.")

        if (max_workers is None or max_workers >= 0) and job_pool is None:
            max_workers = cls.__normal_max_workers(max_workers)
            try:
                from concurrent.futures import ThreadPoolExecutor
                with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
                    return cls.output(*files,
                                      std=std,
                                      grader=grader,
                                      max_workers=max_workers,
                                      job_pool=job_pool)
            except ImportError:
                pass

        def get_std():
            return cls.__process_file(std)[1]

        if job_pool is not None:
            std = job_pool.submit(get_std).result()
        else:
            std = get_std()

        def do(file):
            (file_name, content) = cls.__process_file(file)
            cls.__compare_two(file_name, content, std, grader)

        if job_pool is not None:
            job_pool.map(do, files)
        else:
            [x for x in map(do, files)]
Пример #4
0
    def program(cls, *programs, **kwargs):
        kwargs = unpack_kwargs('program', kwargs,
                               ('input', ('std', None), ('std_program', None),
                                ('grader', DEFAULT_GRADER),
                                ('max_workers', -1), ('job_pool', None),
                                ('stop_on_incorrect', None)))
        input = kwargs['input']
        std = kwargs['std']
        std_program = kwargs['std_program']
        grader = kwargs['grader']
        max_workers = kwargs['max_workers']
        job_pool = kwargs['job_pool']
        if kwargs['stop_on_incorrect'] is not None:
            log.warn(
                "parameter stop_on_incorrect is deprecated and has no effect.")

        if (max_workers is None or max_workers >= 0) and job_pool is None:
            max_workers = cls.__normal_max_workers(max_workers)
            try:
                from concurrent.futures import ThreadPoolExecutor
                with ThreadPoolExecutor(max_workers=max_workers) as job_pool:
                    return cls.program(*programs,
                                       input=input,
                                       std=std,
                                       std_program=std_program,
                                       grader=grader,
                                       max_workers=max_workers,
                                       job_pool=job_pool)
            except ImportError:
                pass

        if not isinstance(input, IO):
            raise TypeError("expect {}, got {}".format(
                type(IO).__name__,
                type(input).__name__))
        input.flush_buffer()
        input.input_file.seek(0)

        if std_program is not None:

            def get_std():
                return make_unicode(
                    subprocess.check_output(std_program,
                                            shell=(not list_like(std_program)),
                                            stdin=input.input_file,
                                            universal_newlines=True))

            if job_pool is not None:
                std = job_pool.submit(get_std).result()
            else:
                std = get_std()
        elif std is not None:

            def get_std():
                return cls.__process_file(std)[1]

            if job_pool is not None:
                std = job_pool.submit(get_std).result()
            else:
                std = get_std()
        else:
            raise TypeError(
                'program() missing 1 required non-None keyword-only argument: \'std\' or \'std_program\''
            )

        def do(program_name):
            timeout = None
            if list_like(program_name) and len(program_name) == 2 and int_like(
                    program_name[-1]):
                program_name, timeout = program_name
            with open(os.dup(input.input_file.fileno()), 'r',
                      newline='\n') as input_file:
                if timeout is None:
                    content = make_unicode(
                        subprocess.check_output(
                            program_name,
                            shell=(not list_like(program_name)),
                            stdin=input_file,
                            universal_newlines=True))
                else:
                    content = make_unicode(
                        subprocess.check_output(
                            program_name,
                            shell=(not list_like(program_name)),
                            stdin=input_file,
                            universal_newlines=True,
                            timeout=timeout))
            cls.__compare_two(program_name, content, std, grader)

        if job_pool is not None:
            job_pool.map(do, programs)
        else:
            [x for x in map(do, programs)]