Salome HOME
Integration of 0019971: A patch for cmake compilation.
[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     from salome_utils import getShortHostName
43     return getShortHostName()
44
45 def searchFreePort():
46     """
47     Search free port for SALOME session.
48     Returns first found free port number.
49     """
50     print "Searching a free port for naming service:",
51     from salome_utils import generateFileName, getHostName
52     hostname = getHostName()
53     NSPORT = 2810
54     limit  = NSPORT+100
55     while 1:
56         print "%s "%(NSPORT),
57         status = os.system("netstat -ltn | grep -E :%s"%(NSPORT))
58         if status:
59             home  = os.getenv("HOME")
60             appli = os.getenv("APPLI")
61             if appli is not None: home = os.path.join(home, appli)
62             omniorb_config = generateFileName(home, prefix="omniORB",
63                                               extension="cfg",
64                                               hidden=True,
65                                               with_hostname=True,
66                                               with_port=NSPORT)
67             f = open(omniorb_config, "w")
68             f.write("ORBInitRef NameService=corbaname::%s:%s\n"%(hostname, NSPORT))
69             f.close()
70             os.environ['OMNIORB_CONFIG'] = omniorb_config
71             last_running_config = generateFileName(home, prefix="omniORB",
72                                                    suffix="last",
73                                                    extension="cfg",
74                                                    hidden=True)
75             os.environ['LAST_RUNNING_CONFIG'] = last_running_config
76             if os.access(last_running_config,os.F_OK):
77                 os.unlink(last_running_config)
78                 pass
79             os.symlink(omniorb_config,last_running_config)
80             print "- Ok"
81             break
82         if NSPORT == limit:
83             msg  = ""
84             msg += "Can not find a free port to launch omniNames\n"
85             msg += "Kill the running servers and try again.\n"
86             raise msg
87         NSPORT = NSPORT+1
88         pass
89     os.environ['NSHOST'] = hostname
90     os.environ['NSPORT'] = str(NSPORT)
91     return NSPORT
92
93
94 class SalomeSession(object):
95     import runSalome
96     import killSalomeWithPort
97     import killSalome
98     def __init__(self, modules):
99         import runSalome
100         sys.argv  = ["dummy.py"]
101         sys.argv += ["--terminal"]
102         if modules:
103             sys.argv += ['--modules=%s'%(",".join(modules))]
104             pass
105         runSalome.clt, runSalome.args = runSalome.main()
106         import salome
107         salome.salome_init()
108         return
109     def __del__(self):
110         import runSalome
111         runSalome.killLocalPort()
112         return
113     pass