Salome HOME
9c648214dd6c7972154960850164d22ce519d345
[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 import os
23 import sys
24 import string
25 import socket
26
27 _session = None
28
29 def startSession(modules=[]):
30     global _session
31     if _session: return
32     searchFreePort()
33     _session = SalomeSession(modules)
34     return
35
36 def getShortHostName():
37     """
38     gives Hostname without domain extension.
39     SALOME naming service needs short Hostnames (without domain extension).
40     HOSTNAME is not allways defined in environment,
41     socket.gethostname() gives short or complete Hostname, depending on
42     defined aliases.
43     """
44     from salome_utils import getShortHostName
45     return getShortHostName()
46
47 def searchFreePort():
48     """
49     Search free port for SALOME session.
50     Returns first found free port number.
51     """
52     print "Searching a free port for naming service:",
53     from salome_utils import generateFileName, getHostName
54     hostname = getHostName()
55     NSPORT = 2810
56     limit  = NSPORT+100
57     while 1:
58         print "%s "%(NSPORT),
59         status = os.system("netstat -ltn | grep -E :%s"%(NSPORT))
60         if status:
61             home  = os.getenv("HOME")
62             appli = os.getenv("APPLI")
63             kwargs={}
64             if appli is not None: 
65               home = os.path.join(home, appli,"USERS")
66               kwargs["with_username"]=True
67             omniorb_config = generateFileName(home, prefix="omniORB",
68                                               extension="cfg",
69                                               hidden=True,
70                                               with_hostname=True,
71                                               with_port=NSPORT,
72                                               **kwargs)
73             f = open(omniorb_config, "w")
74             f.write("ORBInitRef NameService=corbaname::%s:%s\n"%(hostname, NSPORT))
75             f.close()
76             os.environ['OMNIORB_CONFIG'] = omniorb_config
77             last_running_config = generateFileName(home, prefix="omniORB",
78                                                    suffix="last",
79                                                    extension="cfg",
80                                                    hidden=True,
81                                                    **kwargs)
82             os.environ['LAST_RUNNING_CONFIG'] = last_running_config
83             if os.access(last_running_config,os.F_OK):
84                 os.unlink(last_running_config)
85                 pass
86             os.symlink(omniorb_config,last_running_config)
87             print "- Ok"
88             break
89         if NSPORT == limit:
90             msg  = ""
91             msg += "Can not find a free port to launch omniNames\n"
92             msg += "Kill the running servers and try again.\n"
93             raise msg
94         NSPORT = NSPORT+1
95         pass
96     os.environ['NSHOST'] = hostname
97     os.environ['NSPORT'] = str(NSPORT)
98     return NSPORT
99
100
101 class SalomeSession(object):
102     import runSalome
103     import killSalomeWithPort
104     import killSalome
105     def __init__(self, modules):
106         import runSalome
107         sys.argv  = ["dummy.py"]
108         sys.argv += ["--terminal"]
109         if modules:
110             sys.argv += ['--modules=%s'%(",".join(modules))]
111             pass
112         runSalome.clt, runSalome.args = runSalome.main()
113         import salome
114         salome.salome_init()
115         return
116     def __del__(self):
117         import runSalome
118         runSalome.killLocalPort()
119         return
120     pass