diff options
author | 2023-09-16 11:51:40 -0700 | |
---|---|---|
committer | 2023-10-27 11:59:01 -0700 | |
commit | 56376361a941383555e2db129baf34250d409b06 (patch) | |
tree | bf82c21e75140088f769ab35ff0e3a3646a752ba | |
parent | Merge #195 - Python 3.12 (diff) |
Refactor modules into subpackages
-rw-r--r-- | config/gunicorn.conf.py | 2 | ||||
-rw-r--r-- | snekbox/__init__.py | 2 | ||||
-rw-r--r-- | snekbox/api/resources/eval.py | 3 | ||||
-rw-r--r-- | snekbox/limits/__init__.py | 3 | ||||
-rw-r--r-- | snekbox/limits/cgroup.py (renamed from snekbox/utils/cgroup.py) | 0 | ||||
-rw-r--r-- | snekbox/limits/swap.py (renamed from snekbox/utils/swap.py) | 0 | ||||
-rw-r--r-- | snekbox/limits/timed.py (renamed from snekbox/utils/timed.py) | 0 | ||||
-rw-r--r-- | snekbox/logging/__init__.py | 4 | ||||
-rw-r--r-- | snekbox/logging/gunicorn.py (renamed from snekbox/utils/gunicorn.py) | 2 | ||||
-rw-r--r-- | snekbox/logging/init.py (renamed from snekbox/utils/logging.py) | 0 | ||||
-rw-r--r-- | snekbox/nsjail.py | 13 | ||||
-rw-r--r-- | snekbox/snekio/__init__.py | 6 | ||||
-rw-r--r-- | snekbox/snekio/attachment.py (renamed from snekbox/snekio.py) | 12 | ||||
-rw-r--r-- | snekbox/snekio/errors.py | 6 | ||||
-rw-r--r-- | snekbox/snekio/filesystem.py (renamed from snekbox/filesystem.py) | 0 | ||||
-rw-r--r-- | snekbox/snekio/memfs.py (renamed from snekbox/memfs.py) | 2 | ||||
-rw-r--r-- | snekbox/utils/__init__.py | 3 | ||||
-rw-r--r-- | tests/limits/__init__.py | 0 | ||||
-rw-r--r-- | tests/limits/test_timed.py (renamed from tests/test_timed.py) | 2 | ||||
-rw-r--r-- | tests/snekio/__init__.py | 0 | ||||
-rw-r--r-- | tests/snekio/test_filesystem.py (renamed from tests/test_filesystem.py) | 2 | ||||
-rw-r--r-- | tests/snekio/test_memfs.py (renamed from tests/test_memfs.py) | 6 | ||||
-rw-r--r-- | tests/snekio/test_snekio.py (renamed from tests/test_snekio.py) | 0 | ||||
-rw-r--r-- | tests/test_nsjail.py | 4 |
24 files changed, 41 insertions, 31 deletions
diff --git a/config/gunicorn.conf.py b/config/gunicorn.conf.py index 5e492bb..02f02a0 100644 --- a/config/gunicorn.conf.py +++ b/config/gunicorn.conf.py @@ -1,6 +1,6 @@ workers = 2 bind = "0.0.0.0:8060" -logger_class = "snekbox.utils.gunicorn.GunicornLogger" +logger_class = "snekbox.logging.GunicornLogger" access_logformat = "%(m)s %(U)s%(q)s %(s)s %(b)s %(L)ss" access_logfile = "-" wsgi_app = "snekbox:SnekAPI()" diff --git a/snekbox/__init__.py b/snekbox/__init__.py index b45960b..191cec1 100644 --- a/snekbox/__init__.py +++ b/snekbox/__init__.py @@ -9,8 +9,8 @@ except metadata.PackageNotFoundError: # pragma: no cover __version__ = "0.0.0.0+unknown" from snekbox.api import SnekAPI # noqa: E402 +from snekbox.logging import init_logger, init_sentry # noqa: E402 from snekbox.nsjail import NsJail # noqa: E402 -from snekbox.utils.logging import init_logger, init_sentry # noqa: E402 __all__ = ("NsJail", "SnekAPI", "DEBUG") diff --git a/snekbox/api/resources/eval.py b/snekbox/api/resources/eval.py index 9a53577..55bba98 100644 --- a/snekbox/api/resources/eval.py +++ b/snekbox/api/resources/eval.py @@ -6,11 +6,10 @@ import falcon from falcon.media.validators.jsonschema import validate from snekbox.nsjail import NsJail +from snekbox.snekio import FileAttachment, ParsingError __all__ = ("EvalResource",) -from snekbox.snekio import FileAttachment, ParsingError - log = logging.getLogger(__name__) diff --git a/snekbox/limits/__init__.py b/snekbox/limits/__init__.py new file mode 100644 index 0000000..1f986c7 --- /dev/null +++ b/snekbox/limits/__init__.py @@ -0,0 +1,3 @@ +from . import cgroup, swap, timed + +__all__ = ("cgroup", "swap", "timed") diff --git a/snekbox/utils/cgroup.py b/snekbox/limits/cgroup.py index cc16178..cc16178 100644 --- a/snekbox/utils/cgroup.py +++ b/snekbox/limits/cgroup.py diff --git a/snekbox/utils/swap.py b/snekbox/limits/swap.py index 6a919cb..6a919cb 100644 --- a/snekbox/utils/swap.py +++ b/snekbox/limits/swap.py diff --git a/snekbox/utils/timed.py b/snekbox/limits/timed.py index 59a4e7f..59a4e7f 100644 --- a/snekbox/utils/timed.py +++ b/snekbox/limits/timed.py diff --git a/snekbox/logging/__init__.py b/snekbox/logging/__init__.py new file mode 100644 index 0000000..c5d14f2 --- /dev/null +++ b/snekbox/logging/__init__.py @@ -0,0 +1,4 @@ +from .gunicorn import GunicornLogger +from .init import FORMAT, init_logger, init_sentry + +__all__ = ("FORMAT", "init_logger", "init_sentry", "GunicornLogger") diff --git a/snekbox/utils/gunicorn.py b/snekbox/logging/gunicorn.py index 96d2e02..d0ef3e1 100644 --- a/snekbox/utils/gunicorn.py +++ b/snekbox/logging/gunicorn.py @@ -5,7 +5,7 @@ from gunicorn.config import Config from snekbox import DEBUG -from .logging import FORMAT +from .init import FORMAT __all__ = ("GunicornLogger",) diff --git a/snekbox/utils/logging.py b/snekbox/logging/init.py index 0082013..0082013 100644 --- a/snekbox/utils/logging.py +++ b/snekbox/logging/init.py diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py index 9bf20bf..285a5bc 100644 --- a/snekbox/nsjail.py +++ b/snekbox/nsjail.py @@ -10,13 +10,12 @@ from typing import Iterable, TypeVar from google.protobuf import text_format -from snekbox import DEBUG, utils +from snekbox import DEBUG, limits from snekbox.config_pb2 import NsJailConfig -from snekbox.filesystem import Size -from snekbox.memfs import MemFS +from snekbox.limits.timed import time_limit from snekbox.process import EvalResult -from snekbox.snekio import FileAttachment -from snekbox.utils.timed import time_limit +from snekbox.snekio import FileAttachment, MemFS +from snekbox.snekio.filesystem import Size __all__ = ("NsJail",) @@ -89,8 +88,8 @@ class NsJail: self.files_pattern = files_pattern self.config = self._read_config(config_path) - self.cgroup_version = utils.cgroup.init(self.config) - self.ignore_swap_limits = utils.swap.should_ignore_limit(self.config, self.cgroup_version) + self.cgroup_version = limits.cgroup.init(self.config) + self.ignore_swap_limits = limits.swap.should_ignore_limit(self.config, self.cgroup_version) log.info(f"Assuming cgroup version {self.cgroup_version}.") diff --git a/snekbox/snekio/__init__.py b/snekbox/snekio/__init__.py new file mode 100644 index 0000000..e7d2e8f --- /dev/null +++ b/snekbox/snekio/__init__.py @@ -0,0 +1,6 @@ +from . import filesystem +from .attachment import FileAttachment, safe_path +from .errors import IllegalPathError, ParsingError +from .memfs import MemFS + +__all__ = ("filesystem", "safe_path", "FileAttachment", "IllegalPathError", "MemFS", "ParsingError") diff --git a/snekbox/snekio.py b/snekbox/snekio/attachment.py index 821f057..73e26b8 100644 --- a/snekbox/snekio.py +++ b/snekbox/snekio/attachment.py @@ -6,6 +6,10 @@ from dataclasses import dataclass from functools import cached_property from pathlib import Path +from .errors import IllegalPathError, ParsingError + +__all__ = ("safe_path", "FileAttachment") + def safe_path(path: str) -> str: """ @@ -28,14 +32,6 @@ def safe_path(path: str) -> str: return path -class ParsingError(ValueError): - """Raised when an incoming content cannot be parsed.""" - - -class IllegalPathError(ParsingError): - """Raised when a request file has an illegal path.""" - - @dataclass(frozen=True) class FileAttachment: """A file attachment.""" diff --git a/snekbox/snekio/errors.py b/snekbox/snekio/errors.py new file mode 100644 index 0000000..3e710e4 --- /dev/null +++ b/snekbox/snekio/errors.py @@ -0,0 +1,6 @@ +class ParsingError(ValueError): + """Raised when an incoming content cannot be parsed.""" + + +class IllegalPathError(ParsingError): + """Raised when a request file has an illegal path.""" diff --git a/snekbox/filesystem.py b/snekbox/snekio/filesystem.py index 312707c..312707c 100644 --- a/snekbox/filesystem.py +++ b/snekbox/snekio/filesystem.py diff --git a/snekbox/memfs.py b/snekbox/snekio/memfs.py index 40b57c4..6d4a00b 100644 --- a/snekbox/memfs.py +++ b/snekbox/snekio/memfs.py @@ -13,8 +13,8 @@ from types import TracebackType from typing import Type from uuid import uuid4 -from snekbox.filesystem import mount, unmount from snekbox.snekio import FileAttachment +from snekbox.snekio.filesystem import mount, unmount log = logging.getLogger(__name__) diff --git a/snekbox/utils/__init__.py b/snekbox/utils/__init__.py deleted file mode 100644 index 010fa65..0000000 --- a/snekbox/utils/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -from . import cgroup, logging, swap, timed - -__all__ = ("cgroup", "logging", "swap", "timed") diff --git a/tests/limits/__init__.py b/tests/limits/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/limits/__init__.py diff --git a/tests/test_timed.py b/tests/limits/test_timed.py index e46bd37..8a1119b 100644 --- a/tests/test_timed.py +++ b/tests/limits/test_timed.py @@ -2,7 +2,7 @@ import math import time from unittest import TestCase -from snekbox.utils.timed import time_limit +from snekbox.limits.timed import time_limit class TimedTests(TestCase): diff --git a/tests/snekio/__init__.py b/tests/snekio/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/snekio/__init__.py diff --git a/tests/test_filesystem.py b/tests/snekio/test_filesystem.py index e4d081f..9f6b76d 100644 --- a/tests/test_filesystem.py +++ b/tests/snekio/test_filesystem.py @@ -5,7 +5,7 @@ from tempfile import TemporaryDirectory from unittest import TestCase from uuid import uuid4 -from snekbox.filesystem import UnmountFlags, mount, unmount +from snekbox.snekio.filesystem import UnmountFlags, mount, unmount class LibMountTests(TestCase): diff --git a/tests/test_memfs.py b/tests/snekio/test_memfs.py index 0555726..cbe2fe4 100644 --- a/tests/test_memfs.py +++ b/tests/snekio/test_memfs.py @@ -4,7 +4,7 @@ from contextlib import ExitStack from unittest import TestCase, mock from uuid import uuid4 -from snekbox.memfs import MemFS +from snekbox.snekio import MemFS UUID_TEST = uuid4() @@ -12,10 +12,10 @@ UUID_TEST = uuid4() class MemFSTests(TestCase): def setUp(self): super().setUp() - self.logger = logging.getLogger("snekbox.memfs") + self.logger = logging.getLogger("snekbox.snekio.memfs") self.logger.setLevel(logging.WARNING) - @mock.patch("snekbox.memfs.uuid4", lambda: UUID_TEST) + @mock.patch("snekbox.snekio.memfs.uuid4", lambda: UUID_TEST) def test_assignment_thread_safe(self): """Test concurrent mounting works in multi-thread environments.""" # Concurrently create MemFS in threads, check only 1 can be created diff --git a/tests/test_snekio.py b/tests/snekio/test_snekio.py index 8f04429..8f04429 100644 --- a/tests/test_snekio.py +++ b/tests/snekio/test_snekio.py diff --git a/tests/test_nsjail.py b/tests/test_nsjail.py index 5d927c2..d54d31b 100644 --- a/tests/test_nsjail.py +++ b/tests/test_nsjail.py @@ -9,9 +9,9 @@ from itertools import product from pathlib import Path from textwrap import dedent -from snekbox.filesystem import Size from snekbox.nsjail import NsJail from snekbox.snekio import FileAttachment +from snekbox.snekio.filesystem import Size class NsJailTests(unittest.TestCase): @@ -576,7 +576,7 @@ class NsJailCgroupTests(unittest.TestCase): # This should still pass for v2, even if this test isn't relevant. def test_cgroupv1(self): logging.getLogger("snekbox.nsjail").setLevel(logging.ERROR) - logging.getLogger("snekbox.utils.swap").setLevel(logging.ERROR) + logging.getLogger("snekbox.limits.swap").setLevel(logging.ERROR) config_base = dedent( """ |