]> SALOME platform Git repositories - modules/yacs.git/blob - bin/appliskel/salome_tester/salome_instance.py
Salome HOME
Merge branch 'V9_3_BR'
[modules/yacs.git] / bin / appliskel / salome_tester / salome_instance.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2015-2019  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(object):
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     with open(port_log) as f:
53       salome_instance.port = int(f.readline())
54
55     os.remove(port_log)
56     return salome_instance
57   #
58
59   def __run(self, args=None, with_gui=False):
60     if args is None:
61       args = []
62
63     sys.argv = ['runSalome'] + args
64
65     if with_gui:
66       # :WARNING: NOT TESTED YET
67       sys.argv += ["--gui"]
68       sys.argv += ["--show-desktop=1"]
69       sys.argv += ["--splash=0"]
70       #sys.argv += ["--standalone=study"]
71       #sys.argv += ["--embedded=SalomeAppEngine,cppContainer,registry,moduleCatalog"]
72     else:
73       sys.argv += ["--terminal"]
74       #sys.argv += ["--shutdown-servers=1"]
75       #sys.argv += ["--modules=FIELDS,PARAVIS,GUI"]
76       pass
77
78     import setenv
79     setenv.main(True)
80     import runSalome
81     runSalome.runSalome()
82
83     if not with_gui:
84       import salome
85       salome.salome_init()
86       session_server = salome.naming_service.Resolve('/Kernel/Session')
87       if session_server:
88         session_server.emitMessage("connect_to_study")
89         session_server.emitMessage("activate_viewer/ParaView")
90         pass
91   #
92
93   def stop(self):
94     from multiprocessing import Process
95     from killSalomeWithPort import killMyPort
96     import tempfile
97     with tempfile.NamedTemporaryFile():
98       p = Process(target = killMyPort, args=(self.port,))
99       p.start()
100       p.join()
101     pass
102   #
103
104 #
105
106 if __name__ == "__main__":
107   print("##### Start instance...")
108   salome_instance = SalomeInstance.start()
109   port = salome_instance.get_port()
110   print("#####    ...instance started on port %s"%port)
111
112   print("##### Terminate instance running on port %s"%port)
113   salome_instance.stop()
114 #