def __init__(self, app=None, conf_path='/etc/neos/neos.conf'): """If the app is not set, the Launcher tries to figure out by itself the best choice between App and AppInEnv based on parameters.""" self.app = app # first instanciations of Conf and Job singletons here self.conf = Conf() self.job = Job() self.load_conf_file(conf_path) self.args = AppArgs(self.conf) self.parse_args() self.setup_logger()
class Launcher(object): """Launcher class is responsible of initializing runtime configuration based on configuration file and arguments, logger and then launching the application given in parameter.""" def __init__(self, app=None, conf_path='/etc/neos/neos.conf'): """If the app is not set, the Launcher tries to figure out by itself the best choice between App and AppInEnv based on parameters.""" self.app = app # first instanciations of Conf and Job singletons here self.conf = Conf() self.job = Job() self.load_conf_file(conf_path) self.args = AppArgs(self.conf) self.parse_args() self.setup_logger() def load_conf_file(self, conf_path): """Parse config file and update app conf accordingly""" loader = ConfLoader(conf_path) loader.update_conf(self.conf) def parse_args(self): """Parse args and update app conf accordingly""" self.args.update_conf(self.conf) def setup_logger(self): """Setup application logger""" app_logger = logging.getLogger('neos') if self.conf.debug is True: app_logger.setLevel(logging.DEBUG) else: app_logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter('%(levelname)s: %(message)s') handler.setFormatter(formatter) app_logger.addHandler(handler) def can_run_outside_job(self): """NEOS can run out of job environment only to print help (automatically handled by argparse), version or list scenarios. In all other cases, NEOS will fail if run outside job environment.""" return self.conf.print_version or self.conf.list_scenarios def run(self): """Initialize and run the application only if expected partition and job task 0. This way, NEOS behaves the same when launched with either srun and sbatch.""" # Check if outside job environment and in authorized exceptions, else # leave with error. if self.job.unknown and not self.can_run_outside_job(): logger.error("NEOS cannot run outside of job environment") return 1 if self.job.known: if self.job.partition != self.conf.cluster_partition: logger.error("cannot run on partition %s", self.job.partition) return 1 if self.job.procid: logger.debug("instantly leaving on procid %d", self.job.procid) return 0 # If the app is not yet defined at this stage, the launcher tries to # find out the best one based on parameters. if self.app is None: if self.can_run_outside_job() or self.conf.module is None: self.app = AppInEnv else: self.app = App logger.debug("app selected: %s", self.app.__name__) app = self.app(self.conf, self.job) return app.run()