aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ionite34 <[email protected]>2022-11-24 13:43:55 +0800
committerGravatar ionite34 <[email protected]>2022-11-24 13:43:55 +0800
commitdeeec17623d61b0899e2a40e4f3cdf75356e1211 (patch)
treeb71b051ee8be14b937d3a7e936b105be8d0c43b5
parentAdd request file IO error handling (diff)
Include json in file parse time limit
-rw-r--r--snekbox/api/resources/eval.py2
-rw-r--r--snekbox/memfs.py8
-rw-r--r--snekbox/nsjail.py2
-rw-r--r--snekbox/snekio.py4
4 files changed, 12 insertions, 4 deletions
diff --git a/snekbox/api/resources/eval.py b/snekbox/api/resources/eval.py
index 14fa295..38a5625 100644
--- a/snekbox/api/resources/eval.py
+++ b/snekbox/api/resources/eval.py
@@ -117,5 +117,5 @@ class EvalResource:
resp.media = {
"stdout": result.stdout,
"returncode": result.returncode,
- "files": [f.to_dict() for f in result.files],
+ "files": [f.json for f in result.files],
}
diff --git a/snekbox/memfs.py b/snekbox/memfs.py
index 0c157dc..fb62ce9 100644
--- a/snekbox/memfs.py
+++ b/snekbox/memfs.py
@@ -21,6 +21,7 @@ def parse_files(
fs: MemFS,
files_limit: int,
files_pattern: str,
+ preload_json: bool = False,
) -> list[FileAttachment]:
"""
Parse files in a MemFS.
@@ -29,11 +30,16 @@ def parse_files(
fs: The MemFS to parse.
files_limit: The maximum number of files to parse.
files_pattern: The glob pattern to match files against.
+ preload_json: Whether to preload and cache JSON data.
Returns:
List of FileAttachments sorted lexically by path name.
"""
- return sorted(fs.attachments(files_limit, files_pattern), key=lambda file: file.path)
+ res = sorted(fs.attachments(files_limit, files_pattern), key=lambda file: file.path)
+ if preload_json:
+ for file in res:
+ _ = file.json
+ return res
class MemFS:
diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py
index b33e41e..696f2f9 100644
--- a/snekbox/nsjail.py
+++ b/snekbox/nsjail.py
@@ -253,7 +253,7 @@ class NsJail:
try:
attachments = timed(
parse_files,
- (fs, self.files_limit, self.files_pattern),
+ (fs, self.files_limit, self.files_pattern, True),
timeout=self.files_timeout,
)
log.info(f"Found {len(attachments)} files.")
diff --git a/snekbox/snekio.py b/snekbox/snekio.py
index 7852301..fd48585 100644
--- a/snekbox/snekio.py
+++ b/snekbox/snekio.py
@@ -3,6 +3,7 @@ from __future__ import annotations
from base64 import b64decode, b64encode
from dataclasses import dataclass
+from functools import cached_property
from pathlib import Path
from typing import Generic, TypeVar
@@ -89,7 +90,8 @@ class FileAttachment(Generic[T]):
else:
file.write_bytes(self.content)
- def to_dict(self) -> dict[str, str]:
+ @cached_property
+ def json(self) -> dict[str, str]:
"""Convert the attachment to a dict."""
content = b64encode(self.as_bytes()).decode("ascii")
return {