diff --git a/swh/storage/__init__.py b/swh/storage/__init__.py --- a/swh/storage/__init__.py +++ b/swh/storage/__init__.py @@ -4,10 +4,11 @@ # See top-level LICENSE file for more information import importlib -from typing import Any, Dict, List +from typing import Any, Dict, List, TYPE_CHECKING import warnings -from .interface import StorageInterface +if TYPE_CHECKING: + from .interface import StorageInterface STORAGE_IMPLEMENTATIONS = { @@ -22,7 +23,7 @@ } -def get_storage(cls: str, **kwargs) -> StorageInterface: +def get_storage(cls: str, **kwargs) -> "StorageInterface": """Get a storage object of class `storage_class` with arguments `storage_args`. @@ -69,7 +70,7 @@ def get_storage_pipeline( steps: List[Dict[str, Any]], check_config=None -) -> StorageInterface: +) -> "StorageInterface": """Recursively get a storage object that may use other storage objects as backends. diff --git a/swh/storage/cli.py b/swh/storage/cli.py --- a/swh/storage/cli.py +++ b/swh/storage/cli.py @@ -1,9 +1,10 @@ -# Copyright (C) 2015-2019 The Software Heritage developers +# Copyright (C) 2015-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 functools +# WARNING: do not import unnecessary things here to keep cli startup time under +# control import logging import os @@ -11,11 +12,7 @@ import click -from swh.core import config from swh.core.cli import CONTEXT_SETTINGS -from swh.journal.client import get_journal_client -from swh.storage import get_storage -from swh.storage.api.server import app try: from systemd.daemon import notify @@ -45,6 +42,8 @@ @click.pass_context def storage(ctx, config_file, check_config): """Software Heritage Storage tools.""" + from swh.core import config + if not config_file: config_file = os.environ.get("SWH_CONFIG_FILENAME") @@ -90,6 +89,8 @@ Do NOT use this in a production environment. """ + from swh.storage.api.server import app + if "log_level" in ctx.obj: logging.getLogger("werkzeug").setLevel(ctx.obj["log_level"]) ensure_check_config(ctx.obj["config"], ctx.obj["check_config"], "write") @@ -164,6 +165,10 @@ There can be several 'replayers' filling a Storage as long as they use the same `group-id`. """ + import functools + + from swh.journal.client import get_journal_client + from swh.storage import get_storage from swh.storage.replay import process_replay_objects ensure_check_config(ctx.obj["config"], ctx.obj["check_config"], "write") diff --git a/swh/storage/tests/test_cli.py b/swh/storage/tests/test_cli.py --- a/swh/storage/tests/test_cli.py +++ b/swh/storage/tests/test_cli.py @@ -34,7 +34,7 @@ def swh_storage(): """An swh-storage object that gets injected into the CLI functions.""" storage = get_storage(**CLI_CONFIG["storage"]) - with patch("swh.storage.cli.get_storage") as get_storage_mock: + with patch("swh.storage.get_storage") as get_storage_mock: get_storage_mock.return_value = storage yield storage