From fc7104cde5fe260e0d2f5cadbd8c7ab66da66f7b Mon Sep 17 00:00:00 2001 From: Eric Herrera Date: Mon, 4 Oct 2021 21:15:10 -0500 Subject: [PATCH] fix: Address PR comments Address fixes recommended by @timmc-edx. --- common/lib/capa/capa/safe_exec/exceptions.py | 3 -- common/lib/capa/capa/safe_exec/helpers.py | 39 +++++++++----------- lms/envs/common.py | 12 ------ 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/common/lib/capa/capa/safe_exec/exceptions.py b/common/lib/capa/capa/safe_exec/exceptions.py index c6cb78c8f8..e3843d0617 100644 --- a/common/lib/capa/capa/safe_exec/exceptions.py +++ b/common/lib/capa/capa/safe_exec/exceptions.py @@ -7,18 +7,15 @@ class CodejailServiceParseError(Exception): """ An exception that is raised whenever we have issues with data parsing. """ - pass # lint-amnesty, pylint: disable=unnecessary-pass class CodejailServiceStatusError(Exception): """ An exception that is raised whenever Codejail service response status is different to 200. """ - pass # lint-amnesty, pylint: disable=unnecessary-pass class CodejailServiceUnavailable(Exception): """ An exception that is raised whenever Codejail service is unavailable. """ - pass # lint-amnesty, pylint: disable=unnecessary-pass diff --git a/common/lib/capa/capa/safe_exec/helpers.py b/common/lib/capa/capa/safe_exec/helpers.py index 76c62e4259..42d567f3ab 100644 --- a/common/lib/capa/capa/safe_exec/helpers.py +++ b/common/lib/capa/capa/safe_exec/helpers.py @@ -9,7 +9,7 @@ import logging from codejail.safe_exec import SafeExecException from django.conf import settings from django.utils.translation import ugettext as _ -from edx_toggles.toggles import SettingDictToggle +from edx_toggles.toggles import SettingToggle from requests.exceptions import RequestException, HTTPError from simplejson import JSONDecodeError @@ -18,17 +18,14 @@ from .exceptions import CodejailServiceParseError, CodejailServiceStatusError, C log = logging.getLogger(__name__) # .. toggle_name: ENABLE_CODEJAIL_REST_SERVICE -# .. toggle_implementation: DjangoSetting +# .. toggle_implementation: SettingToggle # .. toggle_default: False # .. toggle_description: Set this to True if you want to run Codejail code using # a separate VM or container and communicate with edx-platform using REST API. # .. toggle_use_cases: open_edx # .. toggle_creation_date: 2021-08-19 -# .. toggle_target_removal_date: None -# .. toggle_warnings: -# .. toggle_tickets: -ENABLE_CODEJAIL_REST_SERVICE = SettingDictToggle( - "FEATURES", "ENABLE_CODEJAIL_REST_SERVICE", default=False, module_name=__name__ +ENABLE_CODEJAIL_REST_SERVICE = SettingToggle( + "ENABLE_CODEJAIL_REST_SERVICE", default=False, module_name=__name__ ) @@ -37,21 +34,19 @@ def is_codejail_rest_service_enabled(): def get_codejail_rest_service_endpoint(): - return "".join([ - settings.CODE_JAIL_REST_SERVICE_HOST, - "/api/v0/code-exec"]) + return f"{settings.CODE_JAIL_REST_SERVICE_HOST}/api/v0/code-exec" def send_safe_exec_request(data, extra_files): """ Sends a request to a codejail api service forwarding required code and files. Arguments: - data: Dict containing code and othe parameters + data: Dict containing code and other parameters required for jailed code execution. extra_files: python_lib.zip file containing extra files required by the codejail execution. Returns: - Response received from codejsail api service + Response received from codejail api service """ globals_dict = data["globals_dict"] @@ -66,30 +61,30 @@ def send_safe_exec_request(data, extra_files): timeout=(settings.CODE_JAIL_REST_SERVICE_CONNECT_TIMEOUT, settings.CODE_JAIL_REST_SERVICE_READ_TIMEOUT) ) - except RequestException: + except RequestException as err: log.error("Failed to connect to codejail api service: url=%s, params=%s", codejail_service_endpoint, str(payload)) - raise CodejailServiceUnavailable(_("Codejail API Service is unavailable. Please try again in a few minutes.")) # lint-amnesty, pylint: disable=raise-missing-from + raise CodejailServiceUnavailable(_( + "Codejail API Service is unavailable. " + "Please try again in a few minutes." + )) from err try: response.raise_for_status() - except HTTPError: - raise CodejailServiceStatusError(_("Codejail API Service invalid response.")) # lint-amnesty, pylint: disable=raise-missing-from + except HTTPError as err: + raise CodejailServiceStatusError(_("Codejail API Service invalid response.")) from err try: response_json = response.json() - except JSONDecodeError: + except JSONDecodeError as err: log.error("Invalid JSON response received from codejail api service: Response_Content=%s", response.content) - raise CodejailServiceParseError(_("Invalid JSON response received from codejail api service.")) # lint-amnesty, pylint: disable=raise-missing-from + raise CodejailServiceParseError(_("Invalid JSON response received from codejail api service.")) from err emsg = response_json.get("emsg") exception = None if emsg: - exception_msg = ". ".join([ - emsg, - "For more information check Codejail Service logs."]) - + exception_msg = f"{emsg}. For more information check Codejail Service logs." exception = SafeExecException(exception_msg) globals_dict.update(response_json.get("globals_dict")) diff --git a/lms/envs/common.py b/lms/envs/common.py index 15c81ea58c..40c0343be0 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -948,18 +948,6 @@ FEATURES = { # .. toggle_target_removal_date: 2021-10-01 # .. toggle_tickets: 'https://openedx.atlassian.net/browse/MICROBA-1405' 'ENABLE_V2_CERT_DISPLAY_SETTINGS': False, - - # .. toggle_name: ENABLE_CODEJAIL_REST_SERVICE - # .. toggle_implementation: DjangoSetting - # .. toggle_default: False - # .. toggle_description: Set this to True if you want to run Codejail code using - # a separate VM or container and communicate with edx-platform using REST API. - # .. toggle_use_cases: open_edx - # .. toggle_creation_date: 2021-08-19 - # .. toggle_target_removal_date: None - # .. toggle_warnings: - # .. toggle_tickets: - 'ENABLE_CODEJAIL_REST_SERVICE': False, } # Specifies extra XBlock fields that should available when requested via the Course Blocks API