def load_app_default(app_name: str): """ Load default app config """ from akrr.util import exec_files_to_dict try: default_app_cfg_filename = os.path.join(default_dir, "default.app.conf") app_cfg_filename = os.path.join(default_dir, app_name + ".app.conf") if not os.path.isfile(default_app_cfg_filename): raise AkrrError( "Default application kernel configuration file do not exists (%s)!" % default_app_cfg_filename) if not os.path.isfile(app_cfg_filename): raise AkrrError("application kernel configuration file do not exists (%s)!" % app_cfg_filename) app = exec_files_to_dict(default_app_cfg_filename, app_cfg_filename, var_in={'name': app_name, 'nickname': app_name + ".@nnodes@"}) # last modification time for future reloading app['default_app_cfg_filename'] = default_app_cfg_filename app['app_cfg_filename'] = app_cfg_filename app['default_app_cfg_file_last_mod_time'] = os.path.getmtime(default_app_cfg_filename) app['app_cfg_file_last_mod_time'] = os.path.getmtime(app_cfg_filename) except Exception: log.exception("Exception occurred during app kernel default configuration loading for %s." % app_name) raise AkrrError("Can not load default app configuration for %s." % app_name) return app
def load_resource(resource_name: str, resource_cfg_filename: str = None, validate=True): """ load resource configuration file, do minimalistic validation return dict with resource parameters raises error if can not load """ import warnings from .util import exec_files_to_dict try: default_resource_cfg_filename = os.path.join(default_dir, "default.resource.conf") if resource_cfg_filename is None: resource_cfg_filename = os.path.join(cfg_dir, 'resources', resource_name, "resource.conf") if not os.path.isfile(default_resource_cfg_filename): raise AkrrError( "Default resource configuration file do not exists (%s)!" % default_resource_cfg_filename) if not os.path.isfile(resource_cfg_filename): raise AkrrError( "Configuration file for resource %s does not exist (%s)!" % (resource_name, resource_cfg_filename)) # execute conf file resource = exec_files_to_dict(default_resource_cfg_filename, resource_cfg_filename) # mapped options in resource input file to those used in AKRR if 'name' not in resource: resource['name'] = resource_name # last modification time for future reloading resource[ 'default_resource_cfg_filename'] = default_resource_cfg_filename resource['resource_cfg_filename'] = resource_cfg_filename resource['resource_cfg_directory'] = os.path.dirname( resource_cfg_filename) resource['default_resource_cfg_file_last_mod_time'] = os.path.getmtime( default_resource_cfg_filename) resource['resource_cfg_file_last_mod_time'] = os.path.getmtime( resource_cfg_filename) # here should be validation if validate: resource = verify_resource_params(resource) return resource except Exception: log.exception( "Exception occurred during resource configuration loading for %s." % resource_name) raise AkrrError("Can not load resource configuration for %s." % resource_name)
def test_exec_files_to_dict(datadir): from akrr.util import exec_files_to_dict tmp = exec_files_to_dict(str(datadir.join("test.py"))) assert "__builtins__" not in tmp assert "sys" not in tmp assert "my_path" in tmp assert "banana" in tmp assert tmp["banana"] == "yellow"
def read_old_akrr_conf_dir(self, old_akrr_conf_dir): """Read old AKRR configuration file""" if not os.path.isdir(old_akrr_conf_dir): log.error("Directory with old AKRR configuration do not exist: " + old_akrr_conf_dir) exit(1) old_akrr_conf_file = os.path.join(old_akrr_conf_dir, "akrr.conf") if not os.path.isfile(old_akrr_conf_file): log.error("File with old AKRR configuration do not exist: " + old_akrr_conf_file) exit(1) from akrr.util import exec_files_to_dict log.info("Reading old AKRR configuration from: " + old_akrr_conf_file) self.old_akrr_conf = exec_files_to_dict(old_akrr_conf_file)
def testconfig(): """ testconfig fixture. It returns test configuration specified in testconfig.conf.py """ global __testconfig if __testconfig is None: import inspect import os from akrr.util import exec_files_to_dict curdir = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) testconfig_filename = os.path.join(curdir, "testconfig.conf.py") if os.path.exists(testconfig_filename): __testconfig = exec_files_to_dict(testconfig_filename) else: __testconfig = {} __testconfig["loads count"] = 0 __testconfig["loads count"] += 1 return __testconfig
def load_app_on_resource(app_name: str, resource_name: str, resource: Dict, app: Dict, app_on_resource_cfg_filename: str = None, validate: bool = True) -> Dict: """ load app configuration for the resource file, do minimalistic validation return dict with app parameters raises error if can not load """ log.debug("Loading app %s", app_name) from akrr.util import exec_files_to_dict try: # load resource specific parameters if app_on_resource_cfg_filename is None: app_on_resource_cfg_filename = os.path.join( cfg_dir, "resources", resource_name, app_name + ".app.conf") if not os.path.isfile(app_on_resource_cfg_filename): # raise error because a specific app on resource was asked if app['need_resource_specific_conf']: raise AkrrError( "application kernel configuration file do not exists (%s)!" % app_on_resource_cfg_filename) else: return {} # init default app_on_resource = copy.deepcopy( app['appkernel_on_resource']['default']) if 'name' not in app_on_resource: app_on_resource['name'] = app_name if 'nickname' not in app_on_resource: app_on_resource['nickname'] = app_name + ".@nnodes@" # set execution_method from resource config execution_method = resource.get("execution_method", "hpc") # set execution_method from app on resource config execution_method = _get_app_execution_method( app_on_resource_cfg_filename, default=execution_method) # read default config app_on_resource_cfg_default = os.path.join( default_dir, "%s.%s.app.conf" % (app_name, execution_method)) if os.path.isfile(app_on_resource_cfg_default): app_on_resource = exec_files_to_dict(app_on_resource_cfg_default, var_in=app_on_resource) elif execution_method != "hpc": log.warning("%s doen't have default for %s execution method" % (app_name, execution_method)) # read resource specific configuration app_on_resource[ 'resource_specific_app_cfg_filename'] = app_on_resource_cfg_filename app_on_resource['resource_specific_app_cfg_file_last_mod_time'] = 0 if os.path.isfile(app_on_resource_cfg_filename): app_on_resource = exec_files_to_dict(app_on_resource_cfg_filename, var_in=app_on_resource) app_on_resource['resource_specific_app_cfg_file_last_mod_time'] = \ os.path.getmtime(app_on_resource_cfg_filename) # validation combined config if validate: app_combined = {} app_combined.update(resource) app_combined.update(app) app_combined.update(app_on_resource) app_on_resource = verify_app_params(app_combined, app_on_resource) return app_on_resource except Exception: log.exception( "Exception occurred during app kernel configuration loading for %s." % app_name) raise AkrrError("Can not load app configuration for %s." % app_name)
def load_app(app_name): """ load app configuration file, do minimalistic validation return dict with app parameters raises error if can not load """ from akrr.util import exec_files_to_dict try: default_app_cfg_filename = os.path.join(default_dir, "default.app.conf") app_cfg_filename = os.path.join(default_dir, app_name + ".app.conf") if not os.path.isfile(default_app_cfg_filename): raise AkrrError( "Default application kernel configuration file do not exists (%s)!" % default_app_cfg_filename) if not os.path.isfile(app_cfg_filename): raise AkrrError( "application kernel configuration file do not exists (%s)!" % app_cfg_filename) app = exec_files_to_dict(default_app_cfg_filename, app_cfg_filename) # load resource specific parameters for resource_name in os.listdir(os.path.join(cfg_dir, "resources")): if resource_name not in ['notactive', 'templates']: app_on_resource_cfg_filename = os.path.join( cfg_dir, "resources", resource_name, app_name + ".app.conf") if os.path.isfile(app_on_resource_cfg_filename): app['appkernel_on_resource'][ resource_name] = exec_files_to_dict( app_on_resource_cfg_filename, var_in=app['appkernel_on_resource']['default']) app['appkernel_on_resource'][resource_name][ 'resource_specific_app_cfg_filename'] = app_on_resource_cfg_filename app['appkernel_on_resource'][resource_name]['resource_specific_app_cfg_file_last_mod_time'] = \ os.path.getmtime(app_on_resource_cfg_filename) # mapped options in app input file to those used in AKRR if 'name' not in app: app['name'] = app_name if 'nickname' not in app: app['nickname'] = app_name + ".@nnodes@" # last modification time for future reloading app['default_app_cfg_filename'] = default_app_cfg_filename app['app_cfg_filename'] = app_cfg_filename app['default_app_cfg_file_last_mod_time'] = os.path.getmtime( default_app_cfg_filename) app['app_cfg_file_last_mod_time'] = os.path.getmtime(app_cfg_filename) # here should be validation app = verify_app_params(app) return app except Exception: log.exception( "Exception occurred during app kernel configuration loading for %s." % app_name) raise AkrrError("Can not load app configuration for %s." % app_name)