Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/api/server.py
# 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 | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import os | import os | ||||
from typing import Any, Dict | |||||
from swh.core import config | from swh.core import config | ||||
from swh.core.api import RPCServerApp | from swh.core.api import RPCServerApp | ||||
from swh.core.api import encode_data_server as encode_data | from swh.core.api import encode_data_server as encode_data | ||||
from swh.core.api import error_handler | from swh.core.api import error_handler | ||||
from swh.storage import get_storage as get_swhstorage | from swh.storage import get_storage as get_swhstorage | ||||
from ..exc import StorageArgumentException | from ..exc import StorageArgumentException | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | |||||
@timed | @timed | ||||
def refresh_stat_counters(): | def refresh_stat_counters(): | ||||
return encode_data(get_storage().refresh_stat_counters()) | return encode_data(get_storage().refresh_stat_counters()) | ||||
api_cfg = None | api_cfg = None | ||||
def load_and_check_config(config_file, type="local"): | def load_and_check_config(config_path: str) -> Dict[str, Any]: | ||||
"""Check the minimal configuration is set to run the api or raise an | """Check the minimal configuration is set to run the api or raise an | ||||
error explanation. | error explanation. | ||||
Args: | Args: | ||||
config_file (str): Path to the configuration file to load | config_path: Path to the configuration file to load | ||||
type (str): configuration type. For 'local' type, more | |||||
checks are done. | |||||
Raises: | Raises: | ||||
Error if the setup is not as expected | Error if the setup is not as expected | ||||
Returns: | Returns: | ||||
configuration as a dict | configuration as a dict | ||||
""" | """ | ||||
if not config_file: | if not config_path: | ||||
raise EnvironmentError("Configuration file must be defined") | raise EnvironmentError("Configuration file must be defined") | ||||
if not os.path.exists(config_file): | if not os.path.exists(config_path): | ||||
raise FileNotFoundError("Configuration file %s does not exist" % (config_file,)) | raise FileNotFoundError(f"Configuration file {config_path} does not exist") | ||||
cfg = config.read(config_file) | cfg = config.read(config_path) | ||||
if "storage" not in cfg: | if "storage" not in cfg: | ||||
raise KeyError("Missing '%storage' configuration") | raise KeyError("Missing '%storage' configuration") | ||||
vlorentz: drop the % | |||||
Done Inline Actionsardumont: D4305 | |||||
return cfg | return cfg | ||||
def make_app_from_configfile(): | def make_app_from_configfile(): | ||||
"""Run the WSGI app from the webserver, loading the configuration from | """Run the WSGI app from the webserver, loading the configuration from | ||||
a configuration file. | a configuration file. | ||||
SWH_CONFIG_FILENAME environment variable defines the | SWH_CONFIG_FILENAME environment variable defines the | ||||
configuration path to load. | configuration path to load. | ||||
""" | """ | ||||
global api_cfg | global api_cfg | ||||
if not api_cfg: | if not api_cfg: | ||||
config_file = os.environ.get("SWH_CONFIG_FILENAME") | config_path = os.environ.get("SWH_CONFIG_FILENAME") | ||||
api_cfg = load_and_check_config(config_file) | api_cfg = load_and_check_config(config_path) | ||||
app.config.update(api_cfg) | app.config.update(api_cfg) | ||||
handler = logging.StreamHandler() | handler = logging.StreamHandler() | ||||
app.logger.addHandler(handler) | app.logger.addHandler(handler) | ||||
return app | return app | ||||
ardumontAuthorUnsubmitted Done Inline Actionscoverage should be better in D4306 now. ardumont: coverage should be better in D4306 now. | |||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
print("Deprecated. Use swh-storage") | print("Deprecated. Use swh-storage") |
drop the %