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