]> SALOME platform Git repositories - tools/ydefx.git/commitdiff
Salome HOME
You can now use YDEFX without a running Salome session.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 10 Jul 2019 15:52:20 +0000 (17:52 +0200)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 10 Jul 2019 15:52:20 +0000 (17:52 +0200)
src/pydefx/CMakeLists.txt
src/pydefx/__init__.py
src/pydefx/configuration.py
src/pydefx/parameters.py
src/pydefx/pystudy.py
src/pydefx/salome_proxy.py [new file with mode: 0644]
src/pyexample/CTestTestfileInstall.cmake
src/pyexample/test_insitu.py
src/pyexample/test_prescript.py

index 8d532e428aaaf0ef46923030fa85e54e58417236..900095adf378b8615594e9144157c129cc107e9f 100644 (file)
@@ -28,6 +28,7 @@ SET(SCRIPTS
   defaultschemabuilder.py
   studyexception.py
   studyresult.py
+  salome_proxy.py
   )
 
 INSTALL(FILES ${SCRIPTS} DESTINATION ${SALOME_INSTALL_PYTHON}/pydefx)
index 29d9f05c5b3739df2ebb0af6278c6ad7feb8a77a..b8f3e94642993fcb967551699fbf70dabad97d0e 100644 (file)
@@ -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
index 570cc8bdf561e556d63e8664f0fc8ae4b8ead8ab..591b3e6be72d74545ecf694ea34044ca4acbaca1 100644 (file)
 #
 # 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)
index 52a1ade532a4e67b96c116047de7cf20ae69def4..4feba18ac5a1128ff658464b29fd0c4172fa3e9a 100644 (file)
 #
 # 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)
index 6172bb37fa0c61006223f147b4ded923c5f0da20..cf2c05655fdb52d031e8cac7fb44c4d915c2e9d7 100644 (file)
@@ -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 (file)
index 0000000..bb1155b
--- /dev/null
@@ -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
index 6dc4cef7561784e7ee9baad7dad86f68bf40c595..6362ad65922db4600e6acfb20ca8816dcf347f8b 100644 (file)
@@ -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}"
+                    )
                     
index 5b564766db81619af92be416500480529b3fd1d6..0bdca1bd6282d632d99924b0b163b83dabb038ac 100644 (file)
@@ -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")
 
index 6faf099a0efdc1703d7a1482a9af6b7f08e13c51..b1ebfae310be828aa6272d8b408e8065e3310f02 100644 (file)
@@ -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")