Salome HOME
461e95b74f51c7e542ed8236aff7d8b643986da6
[modules/kernel.git] / bin / appliskel / salome_tester / salome_instance.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2015-2023  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import sys
22 import os
23
24 # Example of args:
25 #      args=["--gui", "--show-desktop=1", "--splash=0"]
26 #      args=["--terminal","--modules=FIELDS,PARAVIS,GUI"]
27 class SalomeInstance:
28
29   def __init__(self):
30     import KernelBasis
31     KernelBasis.setSSLMode(False)
32     self.port = None
33   #
34
35   def get_port(self):
36     return self.port
37   #
38
39   @staticmethod
40   def start(shutdown_servers=False, with_gui=False, args=[]):
41     import tempfile
42     log = tempfile.NamedTemporaryFile(suffix='_nsport', delete=True)
43     port_log = log.name
44     log.close()
45     port_log = port_log + "-%s.log"%(os.getpid())
46     instance_args = [
47       "--ns-port-log=%s"%port_log,
48       "--shutdown-servers=%d"%shutdown_servers
49       ] + args
50
51     salome_instance = SalomeInstance()
52     salome_instance.__run(args=instance_args, with_gui=with_gui)
53
54     try:
55       with open(port_log) as f:
56         salome_instance.port = int(f.readline())
57       os.remove(port_log)
58     except Exception:
59       pass
60
61     return salome_instance
62   #
63
64   def __run(self, args=None, with_gui=False):
65     if args is None:
66       args = []
67
68     sys.argv = ['runSalomeOld'] + args
69
70     if with_gui:
71       # :WARNING: NOT TESTED YET
72       sys.argv += ["--gui"]
73       sys.argv += ["--show-desktop=1"]
74       sys.argv += ["--splash=0"]
75       #sys.argv += ["--standalone=study"]
76       #sys.argv += ["--embedded=SalomeAppEngine,cppContainer,registry,moduleCatalog"]
77     else:
78       sys.argv += ["--terminal"]
79       #sys.argv += ["--shutdown-servers=1"]
80       #sys.argv += ["--modules=FIELDS,PARAVIS,GUI"]
81       pass
82
83     import setenv
84     setenv.main(True)
85     import runSalomeOld
86     runSalomeOld.runSalome()
87
88     if not with_gui:
89       import salome
90       salome.salome_init()
91       session_server = salome.naming_service.Resolve('/Kernel/Session')
92       if session_server:
93         session_server.emitMessage("connect_to_study")
94         session_server.emitMessage("activate_viewer/ParaView")
95         pass
96   #
97
98   def stop(self):
99     from multiprocessing import Process
100     from killSalomeWithPort import killMyPort
101     import tempfile
102     with tempfile.NamedTemporaryFile():
103       p = Process(target = killMyPort, args=(self.port,))
104       p.start()
105       p.join()
106     pass
107   #
108
109 #
110
111 if __name__ == "__main__":
112   print("##### Start instance...")
113   salome_instance = SalomeInstance.start()
114   port = salome_instance.get_port()
115   print("#####    ...instance started on port %s"%port)
116
117   print("##### Terminate instance running on port %s"%port)
118   salome_instance.stop()
119 #