Salome HOME
Improve SALOME logger: customize log file name (add port, user and host ids) to avoid...
[modules/kernel.git] / bin / salome_session.py
1 # Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
2 #           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either 
6 # version 2.1 of the License.
7
8 # This library is distributed in the hope that it will be useful 
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 # Lesser General Public License for more details.
12
13 # You should have received a copy of the GNU Lesser General Public  
14 # License along with this library; if not, write to the Free Software 
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19
20 import os
21 import sys
22 import string
23 import socket
24
25 _session = None
26
27 def startSession(modules=[]):
28     global _session
29     if _session: return
30     searchFreePort()
31     _session = SalomeSession(modules)
32     return
33
34 def getShortHostName():
35     """
36     gives Hostname without domain extension.
37     SALOME naming service needs short Hostnames (without domain extension).
38     HOSTNAME is not allways defined in environment,
39     socket.gethostname() gives short or complete Hostname, depending on
40     defined aliases.
41     """
42     return string.split(socket.gethostname(),'.')[0]
43
44 def searchFreePort():
45     print "Searching a free port for naming service:",
46     NSPORT=2810
47     limit=NSPORT
48     limit=limit+100
49     while 1:
50         print "%s "%(NSPORT),
51         status = os.system("netstat -ltn | grep -E :%s"%(NSPORT))
52         if status:
53             home = os.environ['HOME']
54             appli=os.environ.get("APPLI")
55             if appli is None:
56                 #Run outside application context
57                 home=home
58             else:
59                 home='%s/%s'%(home,appli)
60             hostname=getShortHostName()
61             omniorb_config = '%s/.omniORB_%s_%s.cfg'%(home,hostname, NSPORT)
62             os.environ['OMNIORB_CONFIG'] = omniorb_config
63             f = open(omniorb_config, "w")
64             f.write("ORBInitRef NameService=corbaname::%s:%s\n"%(hostname, NSPORT))
65             f.close()
66             last_running_config = '%s/.omniORB_last.cfg'%(home, hostname)
67             os.environ['LAST_RUNNING_CONFIG'] = last_running_config
68             if os.access(last_running_config,os.F_OK):
69                 os.unlink(last_running_config)
70                 pass
71             os.symlink(omniorb_config,last_running_config)
72             #            LAST_RUNNING_CONFIG=${HOME}/${APPLI}/.omniORB_${myhost}_last.cfg
73             print "- Ok"
74             break
75         if NSPORT == limit:
76             msg  = ""
77             msg += "I Can't find a free port to launch omniNames\n"
78             msg += "I suggest you to kill the running servers and try again.\n"
79             raise msg
80         NSPORT=NSPORT+1
81         pass
82     os.environ['NSHOST']=hostname
83     os.environ['NSPORT']=str(NSPORT)
84     return NSPORT
85
86
87 class SalomeSession(object):
88     import runSalome
89     import killSalomeWithPort
90     import killSalome
91     def __init__(self, modules):
92         import runSalome
93         sys.argv  = ["dummy.py"]
94         sys.argv += ["--terminal"]
95         if modules:
96             sys.argv += ['--modules=%s'%(",".join(modules))]
97             pass
98         runSalome.clt, runSalome.args = runSalome.main()
99         import salome
100         salome.salome_init()
101         return
102     def __del__(self):
103         import runSalome
104         runSalome.killLocalPort()
105         return
106     pass