Salome HOME
venv directory is configured in config_appli.xml file
[modules/kernel.git] / bin / appliskel / salome_tester / salome_instance.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2015-2021  CEA/DEN, EDF R&D, 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     self.port = None
31   #
32
33   def get_port(self):
34     return self.port
35   #
36
37   @staticmethod
38   def start(shutdown_servers=False, with_gui=False, args=[]):
39     import tempfile
40     log = tempfile.NamedTemporaryFile(suffix='_nsport', delete=True)
41     port_log = log.name
42     log.close()
43     port_log = port_log + "-%s.log"%(os.getpid())
44     instance_args = [
45       "--ns-port-log=%s"%port_log,
46       "--shutdown-servers=%d"%shutdown_servers
47       ] + args
48
49     salome_instance = SalomeInstance()
50     salome_instance.__run(args=instance_args, with_gui=with_gui)
51
52     try:
53       with open(port_log) as f:
54         salome_instance.port = int(f.readline())
55       os.remove(port_log)
56     except Exception:
57       pass
58
59     return salome_instance
60   #
61
62   def __run(self, args=None, with_gui=False):
63     if args is None:
64       args = []
65
66     sys.argv = ['runSalome'] + args
67
68     if with_gui:
69       # :WARNING: NOT TESTED YET
70       sys.argv += ["--gui"]
71       sys.argv += ["--show-desktop=1"]
72       sys.argv += ["--splash=0"]
73       #sys.argv += ["--standalone=study"]
74       #sys.argv += ["--embedded=SalomeAppEngine,cppContainer,registry,moduleCatalog"]
75     else:
76       sys.argv += ["--terminal"]
77       #sys.argv += ["--shutdown-servers=1"]
78       #sys.argv += ["--modules=FIELDS,PARAVIS,GUI"]
79       pass
80
81     import setenv
82     setenv.main(True)
83     import runSalome
84     runSalome.runSalome()
85
86     if not with_gui:
87       import salome
88       salome.salome_init()
89       session_server = salome.naming_service.Resolve('/Kernel/Session')
90       if session_server:
91         session_server.emitMessage("connect_to_study")
92         session_server.emitMessage("activate_viewer/ParaView")
93         pass
94   #
95
96   def stop(self):
97     from multiprocessing import Process
98     from killSalomeWithPort import killMyPort
99     import tempfile
100     with tempfile.NamedTemporaryFile():
101       p = Process(target = killMyPort, args=(self.port,))
102       p.start()
103       p.join()
104     pass
105   #
106
107 #
108
109 if __name__ == "__main__":
110   print("##### Start instance...")
111   salome_instance = SalomeInstance.start()
112   port = salome_instance.get_port()
113   print("#####    ...instance started on port %s"%port)
114
115   print("##### Terminate instance running on port %s"%port)
116   salome_instance.stop()
117 #