Salome HOME
To see which hdf5 we're using in configure log
[modules/kernel.git] / bin / salome_session.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 ## @package salome_session
23 # \brief Module that provides the SalomeSession object that helps to launch
24 #        a SALOME script session
25 #
26 #
27
28 import os
29 import sys
30 import string
31 import socket
32
33 _session = None
34
35 def startSession(modules=[]):
36     global _session
37     if _session: return
38     searchFreePort()
39     _session = SalomeSession(modules)
40     return
41
42 def getShortHostName():
43     """
44     gives Hostname without domain extension.
45     SALOME naming service needs short Hostnames (without domain extension).
46     HOSTNAME is not allways defined in environment,
47     socket.gethostname() gives short or complete Hostname, depending on
48     defined aliases.
49     """
50     from salome_utils import getShortHostName
51     return getShortHostName()
52
53 def searchFreePort():
54     """
55     Search free port for SALOME session.
56     Returns first found free port number.
57     """
58     print "Searching a free port for naming service:",
59     from salome_utils import generateFileName, getHostName
60     hostname = getHostName()
61     NSPORT = 2810
62     limit  = NSPORT+100
63     while 1:
64         print "%s "%(NSPORT),
65         status = os.system("netstat -ltn | grep -E :%s"%(NSPORT))
66         if status:
67             home  = os.getenv("HOME")
68             appli = os.getenv("APPLI")
69             kwargs={}
70             if appli is not None: 
71               home = os.path.join(home, appli,"USERS")
72               kwargs["with_username"]=True
73             omniorb_config = generateFileName(home, prefix="omniORB",
74                                               extension="cfg",
75                                               hidden=True,
76                                               with_hostname=True,
77                                               with_port=NSPORT,
78                                               **kwargs)
79             f = open(omniorb_config, "w")
80             f.write("ORBInitRef NameService=corbaname::%s:%s\n"%(hostname, NSPORT))
81             f.close()
82             os.environ['OMNIORB_CONFIG'] = omniorb_config
83             last_running_config = generateFileName(home, prefix="omniORB",
84                                                    suffix="last",
85                                                    extension="cfg",
86                                                    hidden=True,
87                                                    **kwargs)
88             os.environ['LAST_RUNNING_CONFIG'] = last_running_config
89             if os.access(last_running_config,os.F_OK):
90                 os.unlink(last_running_config)
91                 pass
92             os.symlink(omniorb_config,last_running_config)
93             print "- Ok"
94             break
95         if NSPORT == limit:
96             msg  = ""
97             msg += "Can not find a free port to launch omniNames\n"
98             msg += "Kill the running servers and try again.\n"
99             raise RuntimeError, msg
100         NSPORT = NSPORT+1
101         pass
102     os.environ['NSHOST'] = hostname
103     os.environ['NSPORT'] = str(NSPORT)
104     return NSPORT
105
106
107 class SalomeSession(object):
108     """Salome session launcher"""
109     import runSalome
110     import killSalomeWithPort
111     import killSalome
112     def __init__(self, modules):
113         import runSalome
114         sys.argv  = ["dummy.py"]
115         sys.argv += ["--terminal"]
116         if modules:
117             sys.argv += ['--modules=%s'%(",".join(modules))]
118             pass
119         runSalome.clt, runSalome.args = runSalome.main()
120         import salome
121         salome.salome_init()
122         return
123     def __del__(self):
124         import runSalome
125         runSalome.killLocalPort()
126         return
127     pass