This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into frontend/feature/student-project-view
- Loading branch information
Showing
39 changed files
with
764 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
FROM docker:dind as builder | ||
|
||
FROM python:3.9 | ||
|
||
COPY --from=builder /usr/local/bin/docker /usr/local/bin/docker | ||
|
||
RUN mkdir /app | ||
WORKDIR /app | ||
|
||
ADD ./project /app/ | ||
|
||
COPY requirements.txt /app/requirements.txt | ||
|
||
RUN pip3 install -r requirements.txt | ||
|
||
COPY . /app | ||
|
||
ENTRYPOINT ["python"] | ||
CMD ["__main__.py"] | ||
|
||
CMD ["__main__.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
FROM python:3.9-slim | ||
FROM docker:dind | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
RUN apk add --no-cache \ | ||
python3 \ | ||
py3-pip \ | ||
tzdata | ||
|
||
ENV TZ=UTC | ||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone | ||
|
||
|
||
|
||
RUN python3 -m venv /venv | ||
ENV PATH="/venv/bin:$PATH" | ||
|
||
# Copy the application code into the container | ||
COPY . /app | ||
|
||
# Install dependencies | ||
RUN apt-get update | ||
RUN apt-get install -y --no-install-recommends python3-pip | ||
WORKDIR /app | ||
|
||
RUN pip3 install --no-cache-dir -r requirements.txt -r dev-requirements.txt | ||
|
||
RUN chmod +x /app/entry-point.sh | ||
|
||
ENTRYPOINT ["/app/entry-point.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/sh | ||
# Start the Docker daemon in the background | ||
dockerd-entrypoint.sh & | ||
|
||
# Wait for the Docker daemon to start | ||
until docker info; do | ||
echo "Waiting for Docker daemon to start..." | ||
sleep 1 | ||
done | ||
|
||
# Execute the command passed to the docker run command | ||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
""" | ||
This module is responsible for evaluating the submission. | ||
It uses docker to run the evaluator in a container. | ||
The image used for the container is determined by the evaluator argument. | ||
If the evaluator is not found in the | ||
DOCKER_IMAGE_MAPPER, the project test path is used as the image. | ||
The evaluator is run in the container and the | ||
exit code is returned. The output of the evaluator is written to a log file | ||
in the submission output folder. | ||
""" | ||
from os import path | ||
import docker | ||
from project.models.submission import Submission | ||
|
||
DOCKER_IMAGE_MAPPER = { | ||
"python": path.join(path.dirname(__file__), "evaluators", "python"), | ||
} | ||
|
||
|
||
def evaluate(submission: Submission, project_path: str, evaluator: str) -> int: | ||
""" | ||
Evaluate a submission using the evaluator. | ||
Args: | ||
submission (Submissions): The submission to evaluate. | ||
project_path (str): The path to the project. | ||
evaluator (str): The evaluator to use. | ||
Returns: | ||
int: The exit code of the evaluator. | ||
Raises: | ||
ValueError: If the evaluator is not found in the DOCKER_IMAGE_MAPPER | ||
and the project test path does not exist. | ||
""" | ||
|
||
docker_image = DOCKER_IMAGE_MAPPER.get(evaluator, None) | ||
if docker_image is None: | ||
docker_image = project_path | ||
if not path.exists(docker_image): | ||
raise ValueError(f"Test path: {docker_image},\ | ||
not found and the provided evaluator:\ | ||
{evaluator} is not associated with any image.") | ||
|
||
submission_path = submission.submission_path | ||
submission_solution_path = path.join(submission_path, "submission") | ||
|
||
container = create_and_run_evaluator(docker_image, | ||
submission.submission_id, | ||
project_path, | ||
submission_solution_path) | ||
|
||
submission_output_path = path.join(submission_path, "output") | ||
test_output_path = path.join(submission_output_path, "test_output.log") | ||
|
||
exit_code = container.wait() | ||
|
||
with open(path.join(test_output_path), "w", encoding='utf-8') as output_file: | ||
output_file.write(container.logs().decode('utf-8')) | ||
|
||
container.remove() | ||
|
||
return exit_code['StatusCode'] | ||
|
||
def create_and_run_evaluator(docker_image: str, | ||
submission_id: int, | ||
project_path: str, | ||
submission_solution_path: str): | ||
""" | ||
Create and run the evaluator container. | ||
Args: | ||
docker_image (str): The path to the docker image. | ||
submission_id (int): The id of the submission. | ||
project_path (str): The path to the project. | ||
submission_solution_path (str): The path to the submission solution. | ||
Returns: | ||
docker.models.containers.Container: The container that is running the evaluator. | ||
""" | ||
client = docker.from_env() | ||
image, _ = client.images.build(path=docker_image, tag=f"submission_{submission_id}") | ||
|
||
|
||
container = client.containers.run( | ||
image.id, | ||
detach=True, | ||
command="bash entry_point.sh", | ||
volumes={ | ||
path.abspath(project_path): {'bind': "/tests", 'mode': 'rw'}, | ||
path.abspath(submission_solution_path): {'bind': "/submission", 'mode': 'rw'} | ||
}, | ||
stderr=True, | ||
stdout=True, | ||
pids_limit=256 | ||
) | ||
return container |
3 changes: 3 additions & 0 deletions
3
backend/project/utils/submissions/evaluators/python/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM python:3.9-slim | ||
|
||
COPY . . |
40 changes: 40 additions & 0 deletions
40
backend/project/utils/submissions/evaluators/python/entry_point.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/bin/bash | ||
|
||
|
||
tests_manifest_file="/tests/req-manifest.txt" | ||
|
||
if [ -f "$tests_manifest_file" ]; then | ||
echo "Tests manifest file found. Installing tests requirements..." | ||
pip3 install -r $tests_manifest_file &> /dev/null | ||
else | ||
echo "No tests manifest file found." | ||
submission_requirements_file="/submission/requirements.txt" | ||
if [ -f "$submission_requirements_file" ]; then | ||
echo "Requirements file found. Installing requirements..." | ||
pip3 install -r $submission_requirements_file &> /dev/null | ||
else | ||
echo "No requirements file found." | ||
fi | ||
|
||
submission_dev_requirements_file="/submission/dev-requirements.txt" | ||
|
||
if [ -f "$submission_dev_requirements_file" ]; then | ||
echo "Dev requirements file found. Installing dev requirements..." | ||
pip3 install -r $submission_dev_requirements_file &> /dev/null | ||
else | ||
echo "No dev requirements file found." | ||
fi | ||
|
||
tests_requirements_file="/tests/requirements.txt" | ||
|
||
if [ -f "$tests_requirements_file" ]; then | ||
echo "Tests requirements file found. Installing tests requirements..." | ||
pip3 install -r $tests_requirements_file &> /dev/null | ||
else | ||
echo "No tests requirements file found." | ||
fi | ||
fi | ||
|
||
echo "Running tests..." | ||
ls /submission | ||
bash /tests/run_test.sh |
Oops, something went wrong.