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

Added requirements file to config zip and GUI view of requirements #3593

Merged
merged 10 commits into from
Sep 20, 2021
5 changes: 5 additions & 0 deletions apps/challenges/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
views.get_challenge_phases_by_challenge_pk,
name="get_challenge_phases_by_challenge_pk",
),
url(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinceli1004 can we remove this API

r"^challenge/requirements/(?P<challenge_pk>[0-9]+)/$",
views.get_challenge_requirements_by_challenge_pk,
name="get_challenge_requirements_by_challenge_pk",
),
url(
r"^challenge/phase/(?P<pk>[0-9]+)/$",
views.get_challenge_phase_by_pk,
Expand Down
54 changes: 54 additions & 0 deletions apps/challenges/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,60 @@ def get_challenge_phases_by_challenge_pk(request, challenge_pk):
return Response(response_data, status=status.HTTP_200_OK)


@api_view(["GET"])
@throttle_classes([AnonRateThrottle])
def get_challenge_requirements_by_challenge_pk(request, challenge_pk):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vinceli1004 can we remove this API

"""
API endpoint to fetch all the requirements corresponding to a challenge using challenge pk
Arguments:
challenge_pk -- Challenge Id for which the requirements are to be fetched
Returns:
Response Object -- An object containing all requirements
"""
requirement_data = []

challenge = get_challenge_model(challenge_pk)
base_location = tempfile.mkdtemp()
zip_location = join(
base_location, "{}.zip".format(challenge_pk)
)
extract_location = join(
base_location, "data{}".format(challenge_pk)
)

zip_ref = zipfile.ZipFile(challenge.evaluation_script, "r")
zip_ref.extractall(extract_location)
zip_ref.close()
try:
os.remove(zip_location)
except Exception as e:
logger.exception(
"Temporary directory {} for challenge {} not removed. Error: {}".format(
zip_location, challenge_pk, e
)
)

requirements_location = join(extract_location, "requirements.txt")

if os.path.isfile(requirements_location):
f = open(requirements_location, "r")
requirement_data = f.read()
requirement_data = requirement_data.splitlines()
f.close()

try:
shutil.rmtree(base_location)
except Exception as e:
logger.exception(
"Temporary directory {} for challenge {} not removed. Error: {}".format(
base_location, challenge_pk, e
)
)

response_data = {"requirements": requirement_data}
return Response(response_data, status=status.HTTP_200_OK)


@api_view(["GET"])
@throttle_classes([AnonRateThrottle])
def get_challenge_phase_by_pk(request, pk):
Expand Down
15 changes: 13 additions & 2 deletions scripts/workers/submission_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import requests
import signal
import shutil
import subprocess
import sys
import tempfile
import time
Expand Down Expand Up @@ -260,12 +261,13 @@ def extract_challenge_data(challenge, phases):
challenge_data_directory = CHALLENGE_DATA_DIR.format(
challenge_id=challenge.id
)
# create challenge directory as package
create_dir_as_python_package(challenge_data_directory)

evaluation_script_url = challenge.evaluation_script.url
evaluation_script_url = return_file_url_per_environment(
evaluation_script_url
)
# create challenge directory as package
create_dir_as_python_package(challenge_data_directory)

# set entry in map
PHASE_ANNOTATION_FILE_NAME_MAP[challenge.id] = {}
Expand All @@ -277,6 +279,15 @@ def extract_challenge_data(challenge, phases):
evaluation_script_url, challenge_zip_file, challenge_data_directory
)

try:
requirements_location = join(challenge_data_directory, "requirements.txt")
if os.path.isfile(requirements_location):
subprocess.check_output([sys.executable, "-m", "pip", "install", "-r", requirements_location])
else:
logger.info("No requirements for challenge {}".format(challenge.id))
except Exception as e:
logger.error(e)

phase_data_base_directory = PHASE_DATA_BASE_DIR.format(
challenge_id=challenge.id
)
Expand Down