diff --git a/docs/README.md b/docs/README.md index 0c6c2c2..4b56b64 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,41 +1 @@ -# Website - -This website is built using [Docusaurus](https://docusaurus.io/), a modern static website generator. - -### Installation - -``` -$ yarn -``` - -### Local Development - -``` -$ yarn start -``` - -This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server. - -### Build - -``` -$ yarn build -``` - -This command generates static content into the `build` directory and can be served using any static contents hosting service. - -### Deployment - -Using SSH: - -``` -$ USE_SSH=true yarn deploy -``` - -Not using SSH: - -``` -$ GIT_USER= yarn deploy -``` - -If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch. +# DataRush Docs diff --git a/services/backend/apps/competition/models.py b/services/backend/apps/competition/models.py index 4a9bf78..1a114b9 100644 --- a/services/backend/apps/competition/models.py +++ b/services/backend/apps/competition/models.py @@ -47,6 +47,9 @@ class Competition(BaseModel): def __str__(self): return self.title + + @property + def class Meta: verbose_name = "соревнование" diff --git a/services/backend/apps/task/tasks.py b/services/backend/apps/task/tasks.py index 9899246..a9f5ad6 100644 --- a/services/backend/apps/task/tasks.py +++ b/services/backend/apps/task/tasks.py @@ -19,6 +19,7 @@ ALLOWED_MODULES = { "csv", "math", "statistics", + "statsmodels", } @@ -63,18 +64,28 @@ def validate_code(code_str): raise SecurityException(f"Security check failed: {e!s}") -def secure_exec(code_str, result_path): +def secure_exec(code_str, result_path, input_files=None): original_dir = os.getcwd() original_stdout = sys.stdout sys.stdout = captured_stdout = StringIO() result_content = None + if input_files is None: + input_files = [] + with tempfile.TemporaryDirectory() as temp_dir: try: os.chdir(temp_dir) + + for file in input_files: + file_path = os.path.join(temp_dir, file["bind_at"]) + os.makedirs(os.path.dirname(file_path), exist_ok=True) + with open(file_path, "wb") as f: + f.write(file["content"]) + restricted_globals = { "__builtins__": { - "open": lambda f, *a, **kw: open(f, *a, **kw), + "open": open, "print": print, "str": str, "int": int, @@ -105,11 +116,15 @@ def secure_exec(code_str, result_path): @app.task(bind=True) -def analyze_data_task(self, code_str, result_path, expected_bytes): +def analyze_data_task( + self, code_str, result_path, expected_bytes, input_files=[] +): try: validate_code(code_str) - result_content = secure_exec(code_str, result_path) + result_content = secure_exec(code_str, result_path, input_files) + + print(result_content * 1024) result_hash = hashlib.sha256(result_content).hexdigest() expected_hash = hashlib.sha256(expected_bytes).hexdigest() diff --git a/services/backend/apps/task/tests/__init__.py b/services/backend/apps/task/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/services/backend/apps/task/tests/test_tasks.py b/services/backend/apps/task/tests/test_tasks.py new file mode 100644 index 0000000..b61bb11 --- /dev/null +++ b/services/backend/apps/task/tests/test_tasks.py @@ -0,0 +1,30 @@ +import unittest + +from apps.task.tasks import analyze_data_task + + +class TestAnalyzeDataTask(unittest.TestCase): + def test_task_execution_basic(self): + code_str = 'print("Hello, World!")' + result_path = "stdout" + expected_bytes = b"Hello, World!\n" + result = analyze_data_task(code_str, result_path, expected_bytes) + self.assertTrue(result["success"]) + self.assertTrue(result["match"]) + + def test_task_execution_with_files(self): + code_str = """ +with open("file.txt") as f: + print(f.read()) + """ + result_path = "stdout" + expected_bytes = b"some_content\n" + result = analyze_data_task( + code_str, + result_path, + expected_bytes, + input_files=[{"bind_at": "file.txt", "content": b"some_content"}], + ) + print(result) + self.assertTrue(result["success"]) + self.assertTrue(result["match"])