diff --git a/requirements-swh.txt b/requirements-swh.txt --- a/requirements-swh.txt +++ b/requirements-swh.txt @@ -1 +1 @@ -swh.core >= 0.0.75 +swh.core[http] >= 0.0.75 diff --git a/swh/deposit/api/common.py b/swh/deposit/api/common.py --- a/swh/deposit/api/common.py +++ b/swh/deposit/api/common.py @@ -4,6 +4,7 @@ # See top-level LICENSE file for more information import hashlib +import logging from typing import Sequence, Type @@ -58,6 +59,9 @@ ACCEPT_ARCHIVE_CONTENT_TYPES = ["application/zip", "application/x-tar"] +logger = logging.getLogger(__name__) + + class AuthenticatedAPIView(APIView): """Mixin intended as a based API view to enforce the basic authentication check diff --git a/swh/deposit/config.py b/swh/deposit/config.py --- a/swh/deposit/config.py +++ b/swh/deposit/config.py @@ -1,15 +1,16 @@ -# Copyright (C) 2017-2018 The Software Heritage developers +# Copyright (C) 2017-2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os -import logging -from typing import Any, Dict, Tuple +from typing import Any, Dict -from swh.core.config import SWHConfig +from swh.core import config from swh.scheduler import get_scheduler +from swh.scheduler.interface import SchedulerInterface + # IRIs (Internationalized Resource identifier) sword 2.0 specified EDIT_SE_IRI = "edit_se_iri" @@ -49,6 +50,15 @@ } +DEFAULT_CONFIG = { + "deposit": { + "max_upload_size": 209715200, + "checks": True, + "scheduler": {"cls": "remote", "args": {"url": "http://localhost:5008/"}}, + }, +} + + def setup_django_for(platform=None, config_file=None): """Setup function for command line tools (swh.deposit.create_user) to initialize the needed db access. @@ -81,30 +91,18 @@ django.setup() -class APIConfig(SWHConfig): - """Mixin intended to enrich views with SWH configuration. +class APIConfig: + """API Configuration centralized class + + This loads explicitly the configuration file out of the SWH_CONFIG_FILENAME + environment variable. """ - CONFIG_BASE_FILENAME = "deposit/server" - - DEFAULT_CONFIG = { - "max_upload_size": ("int", 209715200), - "checks": ("bool", True), - "scheduler": ( - "dict", - {"cls": "remote", "args": {"url": "http://localhost:5008/"}}, - ), - } - - ADDITIONAL_CONFIG = {} # type: Dict[str, Tuple[str, Any]] - - def __init__(self, **config): - super().__init__() - self.config = self.parse_config_file( - additional_configs=[self.ADDITIONAL_CONFIG] - ) - self.config.update(config) - self.log = logging.getLogger("swh.deposit") - if self.config.get("scheduler"): - self.scheduler = get_scheduler(**self.config["scheduler"]) + def __init__(self): + config_file = os.environ["SWH_CONFIG_FILENAME"] + conf = config.read_raw_config(config.config_basepath(config_file)) + self.config: Dict[str, Any] = config.merge_configs(DEFAULT_CONFIG, conf)[ + "deposit" + ] + self.scheduler[SchedulerInterface] = get_scheduler(**self.config["scheduler"]) diff --git a/swh/deposit/tests/conftest.py b/swh/deposit/tests/conftest.py --- a/swh/deposit/tests/conftest.py +++ b/swh/deposit/tests/conftest.py @@ -78,15 +78,12 @@ def deposit_autoconfig(monkeypatch, deposit_config, swh_scheduler_config): """Enforce config for deposit classes inherited from APIConfig.""" - def mock_parse_config(*args, **kw): - config = deposit_config.copy() - config["scheduler"] = { - "cls": "local", - "args": swh_scheduler_config, - } - return config - - monkeypatch.setattr(APIConfig, "parse_config_file", mock_parse_config) + # add the scheduler to configuration + config = deposit_config.copy() + config["scheduler"] = { + "cls": "local", + "args": swh_scheduler_config, + } scheduler = get_scheduler("local", swh_scheduler_config) task_type = { @@ -96,6 +93,12 @@ } scheduler.create_task_type(task_type) + def init_api_config(self): + self.config = config + self.scheduler = scheduler + + monkeypatch.setattr(APIConfig, "__init__", init_api_config) + @pytest.fixture(scope="session") def django_db_setup(request, django_db_blocker, postgresql_proc):