diff options
author | 2022-11-22 14:12:31 -0500 | |
---|---|---|
committer | 2022-11-22 14:12:31 -0500 | |
commit | a3a8eb34037a5ec0e282d9e7cc1ed5d7d15b9d6a (patch) | |
tree | 33a957cbcb8b0e1495cd0f977a47a198e5f1086e | |
parent | Update docstrings (diff) |
Move parse_files to memfs
-rw-r--r-- | snekbox/memfs.py | 14 | ||||
-rw-r--r-- | snekbox/nsjail.py | 25 |
2 files changed, 19 insertions, 20 deletions
diff --git a/snekbox/memfs.py b/snekbox/memfs.py index 03251cd..c5c1505 100644 --- a/snekbox/memfs.py +++ b/snekbox/memfs.py @@ -48,6 +48,20 @@ def unmount_tmpfs(name: str) -> None: tmp.rmdir() +def parse_files( + fs: MemFS, + files_limit: int, + files_pattern: str, +) -> list[FileAttachment]: + """ + Parse files in a MemFS. + + Returns: + List of FileAttachments sorted lexically by path name. + """ + return sorted(fs.attachments(files_limit, files_pattern), key=lambda file: file.path) + + class MemFS: """A temporary directory using tmpfs.""" diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index 4050fae..ee15f71 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -10,17 +10,16 @@ from google.protobuf import text_format from snekbox import DEBUG, utils from snekbox.config_pb2 import NsJailConfig -from snekbox.memfs import MemFS - -__all__ = ("NsJail",) - +from snekbox.memfs import MemFS, parse_files from snekbox.process import EvalResult from snekbox.snekio import FileAttachment from snekbox.utils.timed import timed +__all__ = ("NsJail",) + log = logging.getLogger(__name__) -T = TypeVar("T") +_T = TypeVar("_T") # [level][timestamp][PID]? function_signature:line_no? message LOG_PATTERN = re.compile( @@ -28,7 +27,7 @@ LOG_PATTERN = re.compile( ) -def iter_lstrip(iterable: Iterable[T]) -> Generator[T, None, None]: +def iter_lstrip(iterable: Iterable[_T]) -> Generator[_T, None, None]: """Removes leading falsy objects from an iterable.""" it = iter(iterable) for item in it: @@ -38,20 +37,6 @@ def iter_lstrip(iterable: Iterable[T]) -> Generator[T, None, None]: yield from it -def parse_files( - fs: MemFS, - files_limit: int, - files_pattern: str, -) -> list[FileAttachment]: - """ - Parse files in a MemFS. - - Returns: - List of FileAttachments sorted lexically by path name. - """ - return sorted(fs.attachments(files_limit, files_pattern), key=lambda file: file.path) - - class NsJail: """ Core Snekbox functionality, providing safe execution of Python code. |