Skip to content

Commit

Permalink
Bugfixes to get a working dev env
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGrace2282 committed Sep 10, 2024
1 parent fa88598 commit 5db11b1
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 9 deletions.
36 changes: 32 additions & 4 deletions docs/source/contributing/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
Setting up a development environment
------------------------------------

Basic Setup
~~~~~~~~~~~

First, you will need to install the following:

* ``python``
* ``pipenv``
* ``git``

You will also need a Github account.
You will also need a GitHub account.

First, `fork <https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo#forking-a-repository>`_
tin. Then you can clone tin onto your computer with
Expand All @@ -30,15 +33,40 @@ After that, install dependencies and follow standard django procedures
.. code-block:: bash
pipenv install --dev
python3 manage.py migrate
python3 create_debug_users.py
pipenv run python3 manage.py migrate
pipenv run python3 scripts/create_debug_users.py
Now you're all set! Try running the development server

.. code-block:: bash
python3 manage.py runserver
pipenv run python3 manage.py runserver
Head on over to `http://127.0.0.1:8000 <http://127.0.0.1:8000>`_, and login
as ``admin`` and the password you just entered.

Submissions
~~~~~~~~~~~

In order to actually create a submission, there are some more steps. First,
you'll need to install `redis <https://redis.io/download>`_. This is platform dependent:
for example, on macOS you can use `homebrew <https://brew.sh/>`_ and run ``brew install redis``,
but on archlinux you'll need to run ``pacman -Syu redis``.

You'll also need to run some scripts to emulate the sandboxing process that goes on in production.
Run the following script::

pipenv run python3 scripts/create_wrappers.py

After that, you'll want to start up the development server and create a course,
and an assignment in the course. After saving the assignment, you can hit "Upload grader"
to add a grader - the simplest example of a grader is located in ``scripts/sample_grader.py``.

Finally, before creating a submission, you'll need to start the celery worker. This can be done
by running the following command in a separate terminal::

pipenv run celery -A tin worker --loglevel=info

Now you can try making a submission, and as long as your submission doesn't throw an error you
should get a 100%! Congrats on your brand new 5.0 GPA!
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ extend-ignore-names = [
"FBT",
]

"scripts/*" = [
"INP001",
]

[tool.ruff.format]
docstring-code-format = true
line-ending = "lf"
Expand Down
File renamed without changes.
54 changes: 54 additions & 0 deletions scripts/create_wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/env python3
from __future__ import annotations

import argparse
from pathlib import Path

TIN_ROOT = Path(__file__).parent.parent / "tin"

PYTHON_WRAPPER = """
import subprocess
import sys
submission = "{submission_path}"
subprocess.run([sys.executable, submission])
"""

# TODO
JAVA_WRAPPER = """"""


def create_wrappers(file_name: str, wrapper_text: str) -> None:
"""Create sample Tin wrapper scripts.
These are supposed to be used for sandboxing, but
for debug purposes we can just do nothing!
"""
wrappers = TIN_ROOT / "sandboxing" / "wrappers"
# we need both because in some cases bwrap exists on the parent system
for dir in ["sandboxed", "testing"]:
wrapper = wrappers / dir
wrapper.mkdir(parents=True, exist_ok=True)

path = wrapper.joinpath(f"{file_name}.txt")
# prevent possible overwriting
if path.exists() and not args.force:
print(f"Skipping file {path}")
else:
if args.force:
print(f"Overwriting file {path}")
path.write_text(wrapper_text)


def cli() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Create sample wrapper scripts to run Tin submissions."
)
parser.add_argument("--force", action="store_true", help="overwrite existing wrapper files.")
return parser.parse_args()


if __name__ == "__main__":
args = cli()
create_wrappers("P", PYTHON_WRAPPER)
create_wrappers("J", JAVA_WRAPPER)
18 changes: 18 additions & 0 deletions scripts/sample_grader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""The (second) simplest grader.
This runs the student submission, and without checking the output
gives them a 100%. It also errors if the student submission crashes.
"""

from __future__ import annotations

import subprocess
import sys

# this will error if the student submission errors
process = subprocess.run(
[sys.executable, sys.argv[1]],
check=True,
)

print("Score: 100%")
11 changes: 6 additions & 5 deletions tin/apps/submissions/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ def run_submission(submission_id):
logger.error("Cannot run processes: %s", e)
raise FileNotFoundError from e

python_exe = (
os.path.join(submission.assignment.venv.path, "bin", "python")
if submission.assignment.venv_fully_created
else "/usr/bin/python3.10"
)
if submission.assignment.venv_fully_created:
python_exe = os.path.join(submission.assignment.venv.path, "bin", "python")
elif settings.DEBUG:
python_exe = shutil.which("python") or shutil.which("python3")
else:
python_exe = "/usr/bin/python3.10"

if not settings.DEBUG or shutil.which("bwrap") is not None:
folder_name = "sandboxed"
Expand Down

0 comments on commit 5db11b1

Please sign in to comment.