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