Skip to content

Commit

Permalink
Merge pull request #8 from AntaresSimulatorTeam/feature/remove-defini…
Browse files Browse the repository at this point in the history
…tions-dependency

Feature/remove definitions dependency
  • Loading branch information
sgatto authored May 25, 2021
2 parents 1daf66a + 2e59ee4 commit 4c608fc
Show file tree
Hide file tree
Showing 46 changed files with 591 additions and 432 deletions.
33 changes: 29 additions & 4 deletions antareslauncher/advanced_launch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
import sys

from antareslauncher.main import run_with
from antareslauncher.main_option_parser import MainOptionParser
from antareslauncher import definitions
from antareslauncher.main import run_with, MainParameters
from antareslauncher.main_option_parser import (
MainOptionParser,
MainOptionsParameters,
)

if __name__ == "__main__":
parser: MainOptionParser = MainOptionParser()
main_options_parameters = MainOptionsParameters(
default_wait_time=definitions.DEFAULT_WAIT_TIME,
default_time_limit=definitions.DEFAULT_TIME_LIMIT,
default_n_cpu=definitions.DEFAULT_N_CPU,
studies_in_dir=definitions.STUDIES_IN_DIR,
log_dir=definitions.LOG_DIR,
finished_dir=definitions.FINISHED_DIR,
ssh_config_file_is_required=definitions.SSH_CONFIG_FILE_IS_REQUIRED,
ssh_configfile_path_alternate1=definitions.SSH_CONFIGFILE_PATH_ALTERNATE1,
ssh_configfile_path_alternate2=definitions.SSH_CONFIGFILE_PATH_ALTERNATE2,
)
parser: MainOptionParser = MainOptionParser(main_options_parameters)
parser.add_basic_arguments()
parser.add_advanced_arguments()
arguments = parser.parse_args()
run_with(arguments)

main_parameters = MainParameters(
json_dir=definitions.JSON_DIR,
default_json_db_name=definitions.DEFAULT_JSON_DB_NAME,
slurm_script_path=definitions.SLURM_SCRIPT_PATH,
antares_versions_on_remote_server=definitions.ANTARES_VERSIONS_ON_REMOTE_SERVER,
default_ssh_dict_from_embedded_json=definitions.DEFAULT_SSH_DICT_FROM_EMBEDDED_JSON,
db_primary_key=definitions.DB_PRIMARY_KEY,
)

run_with(arguments=arguments, parameters=main_parameters, show_banner=True)
if not len(sys.argv) > 1:
input("Press ENTER to exit.")
12 changes: 9 additions & 3 deletions antareslauncher/antares_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
from antareslauncher.use_cases.check_remote_queue.check_queue_controller import (
CheckQueueController,
)
from antareslauncher.use_cases.create_list.study_list_composer import StudyListComposer
from antareslauncher.use_cases.kill_job.job_kill_controller import JobKillController
from antareslauncher.use_cases.create_list.study_list_composer import (
StudyListComposer,
)
from antareslauncher.use_cases.kill_job.job_kill_controller import (
JobKillController,
)
from antareslauncher.use_cases.launch.launch_controller import LaunchController
from antareslauncher.use_cases.retrieve.retrieve_controller import RetrieveController
from antareslauncher.use_cases.retrieve.retrieve_controller import (
RetrieveController,
)
from antareslauncher.use_cases.wait_loop_controller.wait_controller import (
WaitController,
)
Expand Down
35 changes: 31 additions & 4 deletions antareslauncher/basic_launch.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import sys

from antareslauncher import main
from antareslauncher.main_option_parser import MainOptionParser
from antareslauncher import main, definitions
from antareslauncher.main import MainParameters
from antareslauncher.main_option_parser import (
MainOptionParser,
MainOptionsParameters,
)

if __name__ == "__main__":
parser: MainOptionParser = MainOptionParser()
main_options_parameters = MainOptionsParameters(
default_wait_time=definitions.DEFAULT_WAIT_TIME,
default_time_limit=definitions.DEFAULT_TIME_LIMIT,
default_n_cpu=definitions.DEFAULT_N_CPU,
studies_in_dir=definitions.STUDIES_IN_DIR,
log_dir=definitions.LOG_DIR,
finished_dir=definitions.FINISHED_DIR,
ssh_config_file_is_required=definitions.SSH_CONFIG_FILE_IS_REQUIRED,
ssh_configfile_path_alternate1=definitions.SSH_CONFIGFILE_PATH_ALTERNATE1,
ssh_configfile_path_alternate2=definitions.SSH_CONFIGFILE_PATH_ALTERNATE2,
)
parser: MainOptionParser = MainOptionParser(
main_options_parameters=main_options_parameters
)
parser.add_basic_arguments()
input_arguments = parser.parse_args()
main.run_with(input_arguments)

main_parameters = MainParameters(
json_dir=definitions.JSON_DIR,
default_json_db_name=definitions.DEFAULT_JSON_DB_NAME,
slurm_script_path=definitions.SLURM_SCRIPT_PATH,
antares_versions_on_remote_server=definitions.ANTARES_VERSIONS_ON_REMOTE_SERVER,
default_ssh_dict_from_embedded_json=definitions.DEFAULT_SSH_DICT_FROM_EMBEDDED_JSON,
db_primary_key=definitions.DB_PRIMARY_KEY,
)

main.run_with(arguments=input_arguments, parameters=main_parameters, show_banner=True)
if not len(sys.argv) > 1:
input("Press ENTER to exit.")
19 changes: 12 additions & 7 deletions antareslauncher/data_repo/data_repo_tinydb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
from tinydb import TinyDB, where

from antareslauncher.data_repo.idata_repo import IDataRepo
from antareslauncher.definitions import DB_PRIMARY_KEY
from antareslauncher.study_dto import StudyDTO


class DataRepoTinydb(IDataRepo):
def __init__(self, database_name):
def __init__(self, database_name, db_primary_key: str):
super(DataRepoTinydb, self).__init__()
self.database_name = database_name
self.logger = logging.getLogger(__name__ + "." + __class__.__name__)
self.db_primary_key = db_primary_key

@property
def db(self):
def db(self) -> tinydb.database.TinyDB:
return TinyDB(self.database_name, sort_keys=True, indent=4)

@staticmethod
Expand All @@ -42,7 +42,7 @@ def is_study_inside_database(self, study: StudyDTO) -> bool:
Returns:
True if the study has been found and is unique inside the database, False otherwise
"""
key = DB_PRIMARY_KEY
key = self.db_primary_key
value = study.__getattribute__(key)
self.logger.info(f"Checking if study {study.path}: is inside database")

Expand Down Expand Up @@ -86,14 +86,19 @@ def save_study(self, study: StudyDTO):
if self.is_study_inside_database(study=study):
self.logger.info(
f"Updating study already existing inside database with"
f"{DB_PRIMARY_KEY}: {study.__getattribute__(DB_PRIMARY_KEY)}"
f"{self.db_primary_key}: {study.__getattribute__(self.db_primary_key)}"
)
self.db.update(
study.__dict__,
where(DB_PRIMARY_KEY) == study.__getattribute__(DB_PRIMARY_KEY),
where(self.db_primary_key)
== study.__getattribute__(self.db_primary_key),
)
else:
self.logger.info(
f"Inserting new study with {DB_PRIMARY_KEY}: {study.__getattribute__(DB_PRIMARY_KEY)} inside database"
f"Inserting new study with {self.db_primary_key}: {study.__getattribute__(self.db_primary_key)} inside database"
)
self.db.insert(study.__dict__)

def remove_study(self, study_name: str) -> None:
self.logger.info(f"Removing study with {self.db_primary_key}:{study_name}")
self.db.remove(where(self.db_primary_key) == study_name)
2 changes: 1 addition & 1 deletion antareslauncher/data_repo/idata_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def save_study(self, study: StudyDTO):
raise NotImplementedError

@abstractmethod
def is_study_inside_database(self, study: StudyDTO):
def is_study_inside_database(self, study: StudyDTO) -> bool:
raise NotImplementedError

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions antareslauncher/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def use_my_constants():
exec(f"{key} = {value.__repr__()}")


SSH_CONFIGFILE_PATH_PROD_CWD = Path.cwd() / DEFAULT_SSH_CONFIGFILE_NAME
SSH_CONFIGFILE_PATH_PROD_USER = (
SSH_CONFIGFILE_PATH_ALTERNATE1 = Path.cwd() / DEFAULT_SSH_CONFIGFILE_NAME
SSH_CONFIGFILE_PATH_ALTERNATE2 = (
Path.home() / "antares_launcher_settings" / DEFAULT_SSH_CONFIGFILE_NAME
)

Expand Down
4 changes: 3 additions & 1 deletion antareslauncher/file_manager/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def zip_dir_excluding_subdir(self, dir_path, zipfile_path, subdir_to_exclude):
subdir_to_exclude: Subdirectory that will not be zipped
"""
list_dir = self._get_list_dir_without_subdir(dir_path, subdir_to_exclude)
file_paths = self._get_complete_list_of_files_and_dirs_in_list_dir(dir_path, list_dir)
file_paths = self._get_complete_list_of_files_and_dirs_in_list_dir(
dir_path, list_dir
)
root_dir = str(Path(dir_path).parent)
self.zip_file_paths_with_rootdir_to_zipfile_path(
zipfile_path, file_paths, root_dir
Expand Down
75 changes: 51 additions & 24 deletions antareslauncher/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import List, Dict

from antareslauncher import definitions, VERSION
from antareslauncher import VERSION
from antareslauncher.antares_launcher import AntaresLauncher
from antareslauncher.data_repo.data_repo_tinydb import DataRepoTinydb
from antareslauncher.definitions import DEFAULT_JSON_DB_NAME, JSON_DIR
from antareslauncher.display.display_terminal import DisplayTerminal
from antareslauncher.file_manager.file_manager import FileManager
from antareslauncher.logger_initializer import LoggerInitializer
Expand All @@ -18,14 +19,23 @@
from antareslauncher.use_cases.check_remote_queue.check_queue_controller import (
CheckQueueController,
)
from antareslauncher.use_cases.check_remote_queue.slurm_queue_show import SlurmQueueShow
from antareslauncher.use_cases.create_list.study_list_composer import StudyListComposer
from antareslauncher.use_cases.check_remote_queue.slurm_queue_show import (
SlurmQueueShow,
)
from antareslauncher.use_cases.create_list.study_list_composer import (
StudyListComposer,
StudyListComposerParameters,
)
from antareslauncher.use_cases.generate_tree_structure.tree_structure_initializer import (
TreeStructureInitializer,
)
from antareslauncher.use_cases.kill_job.job_kill_controller import JobKillController
from antareslauncher.use_cases.kill_job.job_kill_controller import (
JobKillController,
)
from antareslauncher.use_cases.launch.launch_controller import LaunchController
from antareslauncher.use_cases.retrieve.retrieve_controller import RetrieveController
from antareslauncher.use_cases.retrieve.retrieve_controller import (
RetrieveController,
)
from antareslauncher.use_cases.retrieve.state_updater import StateUpdater
from antareslauncher.use_cases.wait_loop_controller.wait_controller import (
WaitController,
Expand Down Expand Up @@ -55,26 +65,36 @@ class SshConnectionNotEstablishedException(Exception):
# fmt: on


# fmt: on
def run_with(arguments):
@dataclass
class MainParameters:
json_dir: Path
default_json_db_name: str
slurm_script_path: str
antares_versions_on_remote_server: List[str]
default_ssh_dict_from_embedded_json: Dict
db_primary_key: str


def run_with(arguments, parameters: MainParameters, show_banner=False):
"""Instantiates all the objects necessary to antares-launcher, and runs the program"""
if arguments.version:
print(f"Antares_Launcher v{VERSION}")
return

print(ANTARES_LAUNCHER_BANNER)
if show_banner:
print(ANTARES_LAUNCHER_BANNER)

studies_in = Path(arguments.studies_in).resolve()
display = DisplayTerminal()
file_manager = FileManager(display)

json_file_name = JSON_DIR / DEFAULT_JSON_DB_NAME
json_file_name = parameters.json_dir / parameters.default_json_db_name

tree_structure_initializer = TreeStructureInitializer(
display,
file_manager,
arguments.studies_in,
definitions.LOG_DIR,
arguments.log_dir,
arguments.output_dir,
)

Expand All @@ -85,24 +105,29 @@ def run_with(arguments):
logger_initializer.init_logger()

# connection
ssh_dict = get_ssh_config_dict(file_manager, arguments.json_ssh_config)
ssh_dict = get_ssh_config_dict(file_manager, arguments.json_ssh_config, parameters)
connection = ssh_connection.SshConnection(config=ssh_dict)
verify_connection(connection, display)

slurm_script_features = SlurmScriptFeatures()
slurm_script_features = SlurmScriptFeatures(parameters.slurm_script_path)
environment = RemoteEnvironmentWithSlurm(connection, slurm_script_features)
data_repo = DataRepoTinydb(database_name=json_file_name)
data_repo = DataRepoTinydb(
database_name=json_file_name, db_primary_key=parameters.db_primary_key
)
study_list_composer = StudyListComposer(
repo=data_repo,
file_manager=file_manager,
display=display,
studies_in_dir=studies_in,
time_limit=arguments.time_limit,
n_cpu=arguments.n_cpu,
log_dir=arguments.log_dir,
output_dir=arguments.output_dir,
xpansion_mode=arguments.xpansion_mode,
post_processing=arguments.post_processing,
parameters=StudyListComposerParameters(
studies_in_dir=arguments.studies_in,
time_limit=arguments.time_limit,
log_dir=arguments.log_dir,
n_cpu=arguments.n_cpu,
xpansion_mode=arguments.xpansion_mode,
output_dir=arguments.output_dir,
post_processing=arguments.post_processing,
antares_versions_on_remote_server=parameters.antares_versions_on_remote_server,
),
)
launch_controller = LaunchController(
repo=data_repo,
Expand All @@ -120,7 +145,9 @@ def run_with(arguments):
)
slurm_queue_show = SlurmQueueShow(env=environment, display=display)
check_queue_controller = CheckQueueController(
slurm_queue_show=slurm_queue_show, state_updater=state_updater, repo=data_repo
slurm_queue_show=slurm_queue_show,
state_updater=state_updater,
repo=data_repo,
)
job_kill_controller = JobKillController(
env=environment,
Expand Down Expand Up @@ -151,9 +178,9 @@ def verify_connection(connection, display):
display.show_message("Ssh connection established", __name__)


def get_ssh_config_dict(file_manager, json_ssh_config):
def get_ssh_config_dict(file_manager, json_ssh_config, parameters: MainParameters):
if json_ssh_config is None:
ssh_dict = definitions.DEFAULT_SSH_DICT_FROM_EMBEDDED_JSON
ssh_dict = parameters.default_ssh_dict_from_embedded_json
else:
ssh_dict = file_manager.convert_json_file_to_dict(json_ssh_config)
if ssh_dict is None:
Expand Down
Loading

0 comments on commit 4c608fc

Please sign in to comment.