From be025b5d0505be078667cfd0c320c71553e63348 Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Wed, 10 Jul 2019 17:52:20 +0200 Subject: [PATCH] You can now use YDEFX without a running Salome session. --- src/pydefx/CMakeLists.txt | 1 + src/pydefx/__init__.py | 3 +- src/pydefx/configuration.py | 10 ++-- src/pydefx/parameters.py | 7 ++- src/pydefx/pystudy.py | 27 ++++++----- src/pydefx/salome_proxy.py | 58 ++++++++++++++++++++++++ src/pyexample/CTestTestfileInstall.cmake | 6 +++ src/pyexample/test_insitu.py | 5 +- src/pyexample/test_prescript.py | 4 +- 9 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 src/pydefx/salome_proxy.py diff --git a/src/pydefx/CMakeLists.txt b/src/pydefx/CMakeLists.txt index 8d532e4..900095a 100644 --- a/src/pydefx/CMakeLists.txt +++ b/src/pydefx/CMakeLists.txt @@ -28,6 +28,7 @@ SET(SCRIPTS defaultschemabuilder.py studyexception.py studyresult.py + salome_proxy.py ) INSTALL(FILES ${SCRIPTS} DESTINATION ${SALOME_INSTALL_PYTHON}/pydefx) diff --git a/src/pydefx/__init__.py b/src/pydefx/__init__.py index 29d9f05..b8f3e94 100644 --- a/src/pydefx/__init__.py +++ b/src/pydefx/__init__.py @@ -22,5 +22,4 @@ from .pystudy import PyStudy from .sample import Sample from .defaultschemabuilder import DefaultSchemaBuilder -import salome -salome.salome_init() +from .salome_proxy import forceSalomeServers, forceNoSalomeServers diff --git a/src/pydefx/configuration.py b/src/pydefx/configuration.py index 570cc8b..591b3e6 100644 --- a/src/pydefx/configuration.py +++ b/src/pydefx/configuration.py @@ -16,17 +16,17 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -import salome +from . import salome_proxy import tempfile import pathlib def defaultWorkingDir(resource): - resManager= salome.lcc.getResourcesManager() + resManager = salome_proxy.getResourcesManager() resource_definition = resManager.GetResourceDefinition(resource) return resource_definition.working_directory def defaultNbBranches(resource): - resManager= salome.lcc.getResourcesManager() + resManager = salome_proxy.getResourcesManager() resource_definition = resManager.GetResourceDefinition(resource) ret = resource_definition.nb_node if ret < 1: @@ -50,7 +50,7 @@ def defaultWckey(resource="localhost"): return result def availableResources(): - resManager = salome.lcc.getResourcesManager() - params = salome.ResourceParameters() + resManager = salome_proxy.getResourcesManager() + params = salome_proxy.createSalomeParameters().resource_required params.can_launch_batch_jobs = True return resManager.GetFittingResources(params) diff --git a/src/pydefx/parameters.py b/src/pydefx/parameters.py index 52a1ade..4feba18 100644 --- a/src/pydefx/parameters.py +++ b/src/pydefx/parameters.py @@ -17,17 +17,16 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -import salome -from . import configuration import tempfile +from . import salome_proxy +from . import configuration class Parameters: def __init__(self, resource="localhost", nb_branches=None, salome_parameters=None): if salome_parameters is None : - job_params = salome.JobParameters() + job_params = salome_proxy.createSalomeParameters() job_params.job_type = "yacs_file" - job_params.resource_required = salome.ResourceParameters() job_params.resource_required.name = resource job_params.job_name = "idefix_job" job_params.wckey = configuration.defaultWckey(resource) diff --git a/src/pydefx/pystudy.py b/src/pydefx/pystudy.py index 6172bb3..cf2c056 100644 --- a/src/pydefx/pystudy.py +++ b/src/pydefx/pystudy.py @@ -21,8 +21,8 @@ import inspect import pathlib import tempfile import os -import salome import json +from . import salome_proxy from . import samplecsvmanager from . import parameters from . import configuration @@ -63,10 +63,13 @@ class PyStudy: tmp_workdir = self.params.salome_parameters.result_directory schema_path, extra_files = self._prepareDirectoryForLaunch(tmp_workdir, script) - - self.params.salome_parameters.in_files.extend(extra_files) + # this list manipulation is needed because in_files is not a python list + # if we don't use a salome session. In that case swig uses a python tuple + # in order to map a std::list as a parameter of a structure. + in_files_as_list = list(self.params.salome_parameters.in_files) + self.params.salome_parameters.in_files = in_files_as_list + extra_files self.params.salome_parameters.job_file = schema_path - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() self.job_id = launcher.createJob(self.params.salome_parameters) return self.job_id @@ -76,7 +79,7 @@ class PyStudy: """ self.sample = self.sampleManager.restoreSample(path) job_string = loadJobString(path) - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() self.job_id = launcher.restoreJob(job_string) if job_id >= 0: salome_params = launcher.getJobParameters(job_id) @@ -89,7 +92,7 @@ class PyStudy: Recover a study from a string which contains the description of the job. This string can be obtained by launcher.dumpJob. """ - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() self.job_id = launcher.restoreJob(jobstring) self.params = None self.sample = None @@ -111,7 +114,7 @@ class PyStudy: if jobid < 0: return self.job_id = jobid - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() salome_params = launcher.getJobParameters(job_id) self.params = parameters.Parameters(salome_parameters=salome_params) #TODO: sampleManager should be loaded from result_directory @@ -146,7 +149,7 @@ class PyStudy: raise StudyUseException("Nothing to launch! Job is not created!") tmp_workdir = self.params.salome_parameters.result_directory # run the job - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() launcher.launchJob(self.job_id) #save the job job_string = launcher.dumpJob(self.job_id) @@ -164,7 +167,7 @@ class PyStudy: self.global_result = StudyResult() if self.job_id < 0 : raise StudyUseException("Cannot get the results if the job is not created!") - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() state = launcher.getJobState(self.job_id) tmp_workdir = self.params.salome_parameters.result_directory searchResults = False @@ -240,7 +243,7 @@ For further details, see {}/logs directory on {}.""".format( def getJobState(self): if self.job_id < 0: return "NOT_CREATED" - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() return launcher.getJobState(self.job_id) def getProgress(self): @@ -256,12 +259,12 @@ For further details, see {}/logs directory on {}.""".format( def dump(self): if self.job_id < 0 : raise StudyUseException("Cannot dump the job if it is not created!") - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() return launcher.dumpJob(self.job_id) def wait(self, sleep_delay=10): """ Wait for the end of the job """ - launcher = salome.naming_service.Resolve('/SalomeLauncher') + launcher = salome_proxy.getLauncher() job_id = self.job_id jobState = launcher.getJobState(job_id) import time diff --git a/src/pydefx/salome_proxy.py b/src/pydefx/salome_proxy.py new file mode 100644 index 0000000..bb1155b --- /dev/null +++ b/src/pydefx/salome_proxy.py @@ -0,0 +1,58 @@ +import salome +import pylauncher +import os + +_use_salome_servers = None + +def _default(): + global _use_salome_servers + if _use_salome_servers is None: + try: + salome.salome_init() + _use_salome_servers = True + except RuntimeError: + _use_salome_servers = False + +def forceSalomeServers(): + global _use_salome_servers + if not _use_salome_servers: + salome.salome_init() + _use_salome_servers = True + +def forceNoSalomeServers(): + global _use_salome_servers + _use_salome_servers = False + +def createSalomeParameters(): + _default() + if _use_salome_servers: + result = salome.JobParameters() + result.resource_required = salome.ResourceParameters() + else: + result = pylauncher.JobParameters_cpp() + result.resource_required = pylauncher.resourceParams() + return result + +_resourceManager = None +def getResourcesManager(): + global _resourceManager + _default() + if _resourceManager is None: + if _use_salome_servers: + _resourceManager = salome.lcc.getResourcesManager() + else: + catalog_path = os.environ.get("USER_CATALOG_RESOURCES_FILE", "") + _resourceManager = pylauncher.ResourcesManager_cpp(catalog_path) + return _resourceManager + +_launcher = None +def getLauncher(): + global _launcher + _default() + if _launcher is None: + if _use_salome_servers: + _launcher = salome.naming_service.Resolve('/SalomeLauncher') + else: + _launcher = pylauncher.Launcher_cpp() + _launcher.SetResourcesManager(getResourcesManager()) + return _launcher diff --git a/src/pyexample/CTestTestfileInstall.cmake b/src/pyexample/CTestTestfileInstall.cmake index 6dc4cef..6362ad6 100644 --- a/src/pyexample/CTestTestfileInstall.cmake +++ b/src/pyexample/CTestTestfileInstall.cmake @@ -22,4 +22,10 @@ ADD_TEST(${TEST_NAME} python ${SALOME_TEST_DRIVER} ${TIMEOUT} ./runUnitTest.sh) SET_TESTS_PROPERTIES(${TEST_NAME} PROPERTIES LABELS "${COMPONENT_NAME}" ) + +SET(TEST_NAME ${COMPONENT_NAME}_NoSession_PyExampleTest) +ADD_TEST(${TEST_NAME} ./runUnitTest.sh) +SET_TESTS_PROPERTIES(${TEST_NAME} PROPERTIES + LABELS "${COMPONENT_NAME}" + ) diff --git a/src/pyexample/test_insitu.py b/src/pyexample/test_insitu.py index 5b56476..0bdca1b 100644 --- a/src/pyexample/test_insitu.py +++ b/src/pyexample/test_insitu.py @@ -1,6 +1,8 @@ import unittest import insitu.insitumanager import os +import time + class TestYdefx(unittest.TestCase): def test_insitu(self): @@ -12,7 +14,8 @@ class TestYdefx(unittest.TestCase): myParams = pydefx.Parameters() myParams.configureResource("localhost") mywd = os.path.join(myParams.salome_parameters.work_directory, - "prescript_test") + "insitu_test" + + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())) myParams.salome_parameters.work_directory = mywd myParams.createResultDirectory("/tmp") diff --git a/src/pyexample/test_prescript.py b/src/pyexample/test_prescript.py index 6faf099..b1ebfae 100644 --- a/src/pyexample/test_prescript.py +++ b/src/pyexample/test_prescript.py @@ -1,5 +1,6 @@ import unittest import os +import time class TestYdefx(unittest.TestCase): def test_prescript(self): @@ -12,7 +13,8 @@ class TestYdefx(unittest.TestCase): myParams = pydefx.Parameters() myParams.configureResource("localhost") mywd = os.path.join(myParams.salome_parameters.work_directory, - "prescript_test") + "prescript_test" + + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())) myParams.salome_parameters.work_directory = mywd myParams.createResultDirectory("/tmp") -- 2.39.2