Skip to content

Adjust execution of server code to generate better error messages #109

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

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 26 additions & 11 deletions src/problem_bank_scripts/problem_bank_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import problem_bank_helpers as pbh
import pandas as pd
import warnings
import tempfile
import traceback

## Parse Markdown
import markdown_it
Expand Down Expand Up @@ -1144,13 +1146,20 @@ def str_presenter(dumper, data2):
# Run the python code; this improved way was suggested by Phil Austin of UBC EOAS

server_py = assemble_server_py(parsed_q, "local")
server = {}
data2 = pbh.create_data2()

spec = importlib.util.spec_from_loader("server", loader=None)
server = importlib.util.module_from_spec(spec)
exec(server_py, server.__dict__)
with tempfile.TemporaryDirectory(suffix=f"_{output_path.stem}", ignore_cleanup_errors=True) as dirpath:
file = pathlib.Path(dirpath).joinpath("server.py")
file.write_text(server_py, encoding="utf8")
try:
code = compile(server_py, file.as_posix(), "exec")
exec(code, server)
server["generate"](data2)
except Exception as e:
msg = f"Error in running the server code, please review the below traceback: \n\n{traceback.format_exc()}"
raise type(e)(msg) from None

data2 = pbh.create_data2()
server.generate(data2)
#################################################################################

# Remove the solutions from the server section
Expand Down Expand Up @@ -1357,14 +1366,20 @@ def process_question_pl(source_filepath, output_path=None, dev=False):
# Run the python code; this improved way was suggested by Phil Austin of UBC EOAS

server_py = assemble_server_py(parsed_q, "local")

spec = importlib.util.spec_from_loader("server", loader=None)
server = importlib.util.module_from_spec(spec)
exec(server_py, server.__dict__)

server = {}
data2 = pbh.create_data2()

server.generate(data2)
with tempfile.TemporaryDirectory(suffix=f"_{output_path.stem}", ignore_cleanup_errors=True) as dirpath:
file = pathlib.Path(dirpath).joinpath("server.py")
file.write_text(server_py, encoding="utf8")
try:
code = compile(server_py, file.as_posix(), "exec")
exec(code, server)
server["generate"](data2)
except Exception as e:
msg = f"Error in running the server code, please review the below traceback: \n\n{traceback.format_exc()}"
raise type(e)(msg) from None

#################################################################################

if dev:
Expand Down
9 changes: 7 additions & 2 deletions src/problem_bank_scripts/scripts/check_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import argparse
import pathlib

from ..problem_bank_scripts import process_question_pl
from problem_bank_scripts import process_question_pl

from . import check_server_ast


def main():
Expand Down Expand Up @@ -39,6 +41,9 @@ def main():
try:
print(f"Processing question: {question}")

if check_server_ast.main([question.as_posix()]) != 0:
return -1

process_question_pl(question.as_posix(), output_path=output_dir)

print(f"\t Moved file to location: {output_dir.parent}")
Expand All @@ -48,7 +53,7 @@ def main():

except Exception as e:
print(f"There is an error in this problem: \n\t- File path: {question}\n\t- Error: {e}")
raise e
return -1


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions src/problem_bank_scripts/scripts/check_server_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
import argparse
import ast
import platform
import sys
import traceback
from collections.abc import Sequence
from pathlib import Path

from ..problem_bank_scripts import assemble_server_py, read_md_problem
from problem_bank_scripts import assemble_server_py, read_md_problem


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
impl = platform.python_implementation()
version = sys.version.split()[0]
version = platform.python_version()

retval = 0
for filename in args.filenames:
Expand Down
2 changes: 1 addition & 1 deletion src/problem_bank_scripts/scripts/lint_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from black import format_str
from black.mode import Mode, TargetVersion

from ..problem_bank_scripts import read_md_problem
from problem_bank_scripts import read_md_problem


def main(argv: Sequence[str] | None = None) -> int:
Expand Down
2 changes: 1 addition & 1 deletion src/problem_bank_scripts/scripts/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import shutil
import sys

from ..problem_bank_scripts import process_question_md, process_question_pl
from problem_bank_scripts import process_question_md, process_question_pl


def _bool(v: str | bool):
Expand Down
4 changes: 2 additions & 2 deletions src/problem_bank_scripts/scripts/process_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import git

from ..problem_bank_scripts import process_question_md, process_question_pl
from problem_bank_scripts import process_question_md, process_question_pl


def _bool(v: str | bool):
Expand Down Expand Up @@ -99,7 +99,7 @@ def main():

except Exception as e:
print(f"There is an error in this problem: \n\t- File path: {md_file}\n\t- Error: {e}")
raise e
raise


if __name__ == "__main__":
Expand Down
Loading