From: Ovidiu Mircescu Date: Wed, 28 Oct 2020 15:48:39 +0000 (+0100) Subject: Configuration reworked. X-Git-Tag: V9_7_0b1~1^2~2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=1c063c9730bb989213cdaa3dbed610910b876779;p=tools%2Fydefx.git Configuration reworked. --- diff --git a/src/pydefx/configuration.py b/src/pydefx/configuration.py index 4e07e82..84516f6 100644 --- a/src/pydefx/configuration.py +++ b/src/pydefx/configuration.py @@ -65,12 +65,14 @@ def exportConfig(dicconfig, directory = None): """ Save the configuration to a directory. dicconfig is a dictionary which contains the parameters to be saved. If directory is None, the configuration is saved to the current directory. + Return the path to the configuration file. """ if directory is None: directory = os.getcwd() configpath = os.path.join(directory, "idefixconfig.json") with open(configpath, "w") as f: json.dump(dicconfig, f, indent=2) + return configpath def loadConfig(directory = None): """ Return the configuration dictionary from a directory. @@ -79,7 +81,12 @@ def loadConfig(directory = None): if directory is None: directory = os.getcwd() configpath = os.path.join(directory, "idefixconfig.json") - with open("idefixconfig.json", "r") as f: + if not pathlib.Path(configpath).is_file(): + configpath = os.path.join(directory, "..", "idefixconfig.json") + if not pathlib.Path(configpath).is_file(): + message = "Configuration file not found in directory " + str(directory) + raise FileNotFoundError(message) + with open(configpath, "r") as f: config = json.load(f) return config @@ -88,12 +95,8 @@ def loadJobConfig(directory = None): a idefixconfig.json file. If the directory is None, use the current directory. """ - result = None - try: - config = loadConfig(directory) - params = parameters.Parameters() - params.loadDict(config["params"]) - result = params.salome_parameters - except: - result = None + config = loadConfig(directory) + params = parameters.Parameters() + params.loadDict(config["params"]) + result = params.salome_parameters return result diff --git a/src/pydefx/localstudy.py b/src/pydefx/localstudy.py index e733cb8..a6b455f 100644 --- a/src/pydefx/localstudy.py +++ b/src/pydefx/localstudy.py @@ -23,7 +23,7 @@ import json from . import pystudy from . import localbuilder from . import salome_proxy - +from . import configuration class LocalStudy(pystudy.PyStudy): """ @@ -38,15 +38,11 @@ class LocalStudy(pystudy.PyStudy): self._check(script,sample) self.sample = sample self.params = copy.deepcopy(params) - main_job_work_dir = os.path.join( - self.params.salome_parameters.work_directory, - "idefixjob") # dump the remote jobs parameters to the configuration file params_dic = params.dumpDict() # modify the parameters for the local loop job self.params.salome_parameters.resource_required.name = "localhost" self.params.salome_parameters.job_type = "command_salome" #"python_salome" - self.params.salome_parameters.work_directory = main_job_work_dir self.params.createTmpResultDirectory() result_directory = self.params.salome_parameters.result_directory # export sample to result_directory @@ -56,15 +52,13 @@ class LocalStudy(pystudy.PyStudy): self.params.salome_parameters.job_file = self.schemaBuilder.getMainJob() # export config - configpath = os.path.join(result_directory, "idefixconfig.json") dicconfig = {} dicconfig["nbbranches"] = self.params.nb_branches dicconfig["studymodule"] = "idefixstudy" dicconfig["sampleIterator"] = self.sampleManager.getModuleName() dicconfig["params"] = params_dic dicconfig["plugin"] = self.schemaBuilder.getPluginName() - with open(configpath, "w") as f: - json.dump(dicconfig, f, indent=2) + configpath = configuration.exportConfig(dicconfig, result_directory) studypath = os.path.join(result_directory, "idefixstudy.py") with open(studypath, "w") as f: f.write(script.script) diff --git a/src/pydefx/multijobstudy.py b/src/pydefx/multijobstudy.py index 2f31656..f46ac53 100644 --- a/src/pydefx/multijobstudy.py +++ b/src/pydefx/multijobstudy.py @@ -23,6 +23,7 @@ import json from . import pystudy from . import multijobbuilder from . import salome_proxy +from . import configuration class MultiJobStudy(pystudy.PyStudy): @@ -57,15 +58,13 @@ class MultiJobStudy(pystudy.PyStudy): self.params.salome_parameters.job_file = self.schemaBuilder.getMainJob() # export config - configpath = os.path.join(result_directory, "idefixconfig.json") dicconfig = {} dicconfig["nbbranches"] = self.params.nb_branches dicconfig["studymodule"] = "idefixstudy" dicconfig["sampleIterator"] = self.sampleManager.getModuleName() dicconfig["params"] = params_dic dicconfig["plugin"] = self.schemaBuilder.getPluginName() - with open(configpath, "w") as f: - json.dump(dicconfig, f, indent=2) + configpath = configuration.exportConfig(dicconfig, result_directory) studypath = os.path.join(result_directory, "idefixstudy.py") with open(studypath, "w") as f: f.write(script.script) diff --git a/src/pydefx/pystudy.py b/src/pydefx/pystudy.py index c2d5928..ed3673b 100644 --- a/src/pydefx/pystudy.py +++ b/src/pydefx/pystudy.py @@ -287,13 +287,11 @@ For further details, see {}/logs directory on {}.""".format( inputFiles = self.sampleManager.prepareRun(self.sample, result_directory) # export nbbranches - configpath = os.path.join(result_directory, "idefixconfig.json") dicconfig = {} dicconfig["nbbranches"] = self.params.nb_branches dicconfig["studymodule"] = "idefixstudy" dicconfig["sampleIterator"] = self.sampleManager.getModuleName() - with open(configpath, "w") as f: - json.dump(dicconfig, f, indent=2) + configpath = configuration.exportConfig(dicconfig, result_directory) studypath = os.path.join(result_directory, "idefixstudy.py") with open(studypath, "w") as f: f.write(script.script) diff --git a/src/pydefx/slurmstudy.py b/src/pydefx/slurmstudy.py index 8be211b..3daf48c 100644 --- a/src/pydefx/slurmstudy.py +++ b/src/pydefx/slurmstudy.py @@ -23,7 +23,7 @@ import json from . import pystudy from . import slurmbuilder from . import salome_proxy - +from . import configuration class SlurmStudy(pystudy.PyStudy): def __init__(self, sampleManager=None, schemaBuilder=None): @@ -50,7 +50,6 @@ class SlurmStudy(pystudy.PyStudy): self.params.salome_parameters.job_file = self.schemaBuilder.getMainJob() # export config - configpath = os.path.join(result_directory, "idefixconfig.json") dicconfig = {} dicconfig["nbbranches"] = self.params.nb_branches dicconfig["studymodule"] = "idefixstudy" @@ -58,8 +57,7 @@ class SlurmStudy(pystudy.PyStudy): dicconfig["plugin"] = self.schemaBuilder.getPluginName() nbproc = self.params.salome_parameters.resource_required.nb_proc dicconfig["tasksPerEval"] = nbproc // self.params.nb_branches - with open(configpath, "w") as f: - json.dump(dicconfig, f, indent=2) + configpath = configuration.exportConfig(dicconfig, result_directory) studypath = os.path.join(result_directory, "idefixstudy.py") with open(studypath, "w") as f: f.write(script.script)