Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved logging, fix unicode error, include_dir buildrun #253

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions problemtools/problem2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import logging
import subprocess

import plasTeX.TeX
import plasTeX.Logging

from .ProblemPlasTeX import ProblemRenderer
from .ProblemPlasTeX import ProblemsetMacros
from . import template

def convert(options: argparse.Namespace) -> None:
import plasTeX.TeX
import plasTeX.Logging

from .ProblemPlasTeX import ProblemRenderer
from .ProblemPlasTeX import ProblemsetMacros
from . import template

problem = os.path.realpath(options.problem)

problembase = os.path.splitext(os.path.basename(problem))[0]
Expand Down
11 changes: 8 additions & 3 deletions problemtools/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,18 @@ def get_program(path, language_config=None, work_dir=None, include_dir=None,
files = [path]
else:
build = os.path.join(path, 'build')
if os.path.isfile(build) and os.access(path, os.X_OK):
if os.path.isfile(build) and os.access(build, os.X_OK):
return BuildRun(path, work_dir)
files = rutil.list_files_recursive(path)

if language_config is not None:
lang = language_config.detect_language(files)
if lang is not None:
return SourceCode(path, lang,
work_dir=work_dir, include_dir=include_dir)
if include_dir is not None:
lang_dir = os.path.join(include_dir, lang.lang_id)
build = os.path.join(lang_dir, 'build')
if os.path.isfile(build) and os.access(build, os.X_OK):
return BuildRun(path, work_dir=work_dir, include_dir=lang_dir)

return SourceCode(path, lang, work_dir=work_dir, include_dir=include_dir)
return None
22 changes: 13 additions & 9 deletions problemtools/run/buildrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from .program import Program
from . import rutil

log = logging.getLogger(__file__)


class BuildRun(Program):
"""Class for build/run-script program.
"""

def __init__(self, path, work_dir=None):
def __init__(self, path, work_dir=None, include_dir=None):
"""Instantiate BuildRun object.

Args:
Expand All @@ -28,12 +30,6 @@ def __init__(self, path, work_dir=None):
if not os.path.isdir(path):
raise ProgramError('%s is not a directory' % path)

build = os.path.join(path, 'build')
if not os.path.isfile(build):
raise ProgramError('%s does not have a build script' % path)
if not os.access(build, os.X_OK):
raise ProgramError('%s/build is not executable' % path)

if work_dir is None:
work_dir = tempfile.mkdtemp()

Expand All @@ -47,7 +43,15 @@ def __init__(self, path, work_dir=None):
os.makedirs(self.path)

rutil.add_files(path, self.path)
if include_dir is not None and os.path.isdir(include_dir):
rutil.add_files(include_dir, self.path)

# Check for existence of build script after copying include_dir, since that could contain the script
build = os.path.join(self.path, 'build')
if not os.path.isfile(build):
raise ProgramError('%s does not have a build script' % path)
if not os.access(build, os.X_OK):
raise ProgramError('%s/build is not executable' % path)

def __str__(self):
"""String representation"""
Expand All @@ -65,8 +69,8 @@ def compile(self):
run = os.path.join(self.path, 'run')

if status:
logging.debug('Build script failed (status %d) when compiling %s\n', status, self.name)
self._compile_result = (False, 'build script failed with exit code %d' % (status))
log.debug('Build script failed (status %d) when compiling %s', status, self.name)
self._compile_result = (False, f'build script failed with exit code {status:d}')
elif not os.path.isfile(run) or not os.access(run, os.X_OK):
self._compile_result = (False, 'build script did not produce an executable called "run"')
else:
Expand Down
7 changes: 5 additions & 2 deletions problemtools/run/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

from .errors import ProgramError

log = logging.getLogger(__name__)


class Program(object):
"""Abstract base class for programs.
"""
Expand Down Expand Up @@ -70,7 +73,7 @@ def should_skip_memory_rlimit(self):

@staticmethod
def __run_wait(argv, infile, outfile, errfile, timelim, memlim, working_directory=None):
logging.debug('run "%s < %s > %s 2> %s"',
log.debug('run "%s < %s > %s 2> %s"',
' '.join(argv), infile, outfile, errfile)
pid = os.fork()
if pid == 0: # child
Expand Down Expand Up @@ -111,7 +114,7 @@ def __run_wait(argv, infile, outfile, errfile, timelim, memlim, working_director
print(exc)
os.kill(os.getpid(), signal.SIGTERM)
# Unreachable
logging.error("Unreachable part of run_wait reached")
log.error("Unreachable part of run_wait reached")
os.kill(os.getpid(), signal.SIGTERM)
(pid, status, rusage) = os.wait4(pid, 0)
return status, rusage.ru_utime + rusage.ru_stime
Expand Down
5 changes: 4 additions & 1 deletion problemtools/run/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from .program import Program
from . import rutil

log = logging.getLogger(__name__)


class SourceCode(Program):
"""Class representing a program provided by source code.
"""
Expand Down Expand Up @@ -103,7 +106,7 @@ def compile(self):
if not os.path.isfile(compiler) or not os.access(compiler, os.X_OK):
return (False, '%s does not seem to be installed, expected to find compiler at %s' % (self.language.name, compiler))

logging.debug('compile command: %s', command)
log.debug('compile command: %s', command)

try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
Expand Down
25 changes: 25 additions & 0 deletions problemtools/tests/test_output_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import random
import pathlib
import string
import tempfile

from problemtools.verifyproblem import OutputValidators


def test_output_validator_feedback():
r = random.Random(0)
with tempfile.TemporaryDirectory() as directory:
feedback = pathlib.Path(directory) / "feedback.txt"
text = "".join(r.choices(string.printable))
feedback.write_text(text)
data = OutputValidators._get_feedback(directory)
assert text in data


def test_output_validator_feedback_non_unicode():
r = random.Random(0)
with tempfile.TemporaryDirectory() as directory:
feedback = pathlib.Path(directory) / "feedback.txt"
feedback.write_bytes(r.randbytes(1024))
# Just test that this does not throw an error
OutputValidators._get_feedback(directory)
Loading