From f7bef76f39beb9daeab6f6f34acbf434d07f8d5a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Tue, 11 May 2021 13:00:44 -0400 Subject: [PATCH] island: Move load_server_config() to config_loader.py Because `monkey_island.py` has the same name as the `monkey_island` module, pytest can't import monkey_island.py and run any tests against its code. --- monkey/monkey_island.py | 21 ++------------------- monkey/monkey_island/config_loader.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 monkey/monkey_island/config_loader.py diff --git a/monkey/monkey_island.py b/monkey/monkey_island.py index b0e510d21..8a2e0608b 100644 --- a/monkey/monkey_island.py +++ b/monkey/monkey_island.py @@ -7,26 +7,9 @@ gevent_monkey.patch_all() import json # noqa: E402 import os # noqa: E402 -from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR # noqa: E402 +from monkey_island import config_loader # noqa: E402 from monkey_island.cc.server_utils.island_logger import setup_logging # noqa: E402 -DEFAULT_LOG_LEVEL = "INFO" - - -def load_server_config(path): - with open(server_config_path, "r") as f: - config_content = f.read() - data = json.loads(config_content) - data_dir = os.path.abspath( - os.path.expanduser( - os.path.expandvars(data["data_dir"] if "data_dir" in data else DEFAULT_DATA_DIR) - ) - ) - log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL - - return data_dir, log_level - - if "__main__" == __name__: island_args = parse_cli_args() @@ -35,7 +18,7 @@ if "__main__" == __name__: try: server_config_path = os.path.expanduser(island_args.server_config) - data_dir, log_level = load_server_config(server_config_path) + data_dir, log_level = config_loader.load_server_config(server_config_path) setup_logging(data_dir, log_level) diff --git a/monkey/monkey_island/config_loader.py b/monkey/monkey_island/config_loader.py new file mode 100644 index 000000000..8f0a7d19b --- /dev/null +++ b/monkey/monkey_island/config_loader.py @@ -0,0 +1,20 @@ +import json +import os + +from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR + +DEFAULT_LOG_LEVEL = "INFO" + + +def load_server_config(server_config_path): + with open(server_config_path, "r") as f: + config_content = f.read() + data = json.loads(config_content) + data_dir = os.path.abspath( + os.path.expanduser( + os.path.expandvars(data["data_dir"] if "data_dir" in data else DEFAULT_DATA_DIR) + ) + ) + log_level = data["log_level"] if "log_level" in data else DEFAULT_LOG_LEVEL + + return data_dir, log_level