Back to index

salome-kernel  6.5.0
salome_session.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 ## @package salome_session
00025 # \brief Module that provides the SalomeSession object that helps to launch
00026 #        a SALOME script session
00027 #
00028 
00029 import os
00030 import sys
00031 import string
00032 import socket
00033 
00034 _session = None
00035 
00036 def startSession(modules=[]):
00037     global _session
00038     if _session: return
00039     searchFreePort()
00040     _session = SalomeSession(modules)
00041     return
00042 
00043 def getShortHostName():
00044     """
00045     gives Hostname without domain extension.
00046     SALOME naming service needs short Hostnames (without domain extension).
00047     HOSTNAME is not allways defined in environment,
00048     socket.gethostname() gives short or complete Hostname, depending on
00049     defined aliases.
00050     """
00051     from salome_utils import getShortHostName
00052     return getShortHostName()
00053 
00054 def searchFreePort():
00055     """
00056     Search free port for SALOME session.
00057     Returns first found free port number.
00058     """
00059     print "Searching a free port for naming service:",
00060     from salome_utils import generateFileName, getHostName
00061     hostname = getHostName()
00062     NSPORT = 2810
00063     limit  = NSPORT+100
00064     while 1:
00065         print "%s "%(NSPORT),
00066         status = os.system("netstat -ltn | grep -E :%s"%(NSPORT))
00067         if status:
00068             home  = os.getenv("HOME")
00069             appli = os.getenv("APPLI")
00070             kwargs={}
00071             if appli is not None: 
00072               home = os.path.join(home, appli,"USERS")
00073               kwargs["with_username"]=True
00074             omniorb_config = generateFileName(home, prefix="omniORB",
00075                                               extension="cfg",
00076                                               hidden=True,
00077                                               with_hostname=True,
00078                                               with_port=NSPORT,
00079                                               **kwargs)
00080             f = open(omniorb_config, "w")
00081             f.write("ORBInitRef NameService=corbaname::%s:%s\n"%(hostname, NSPORT))
00082             f.close()
00083             os.environ['OMNIORB_CONFIG'] = omniorb_config
00084             last_running_config = generateFileName(home, prefix="omniORB",
00085                                                    suffix="last",
00086                                                    extension="cfg",
00087                                                    hidden=True,
00088                                                    **kwargs)
00089             os.environ['LAST_RUNNING_CONFIG'] = last_running_config
00090             if os.access(last_running_config,os.F_OK):
00091                 os.unlink(last_running_config)
00092                 pass
00093             os.symlink(omniorb_config,last_running_config)
00094             print "- Ok"
00095             break
00096         if NSPORT == limit:
00097             msg  = ""
00098             msg += "Can not find a free port to launch omniNames\n"
00099             msg += "Kill the running servers and try again.\n"
00100             raise RuntimeError, msg
00101         NSPORT = NSPORT+1
00102         pass
00103     os.environ['NSHOST'] = hostname
00104     os.environ['NSPORT'] = str(NSPORT)
00105     return NSPORT
00106 
00107 
00108 class SalomeSession(object):
00109     """Salome session launcher"""
00110     import runSalome
00111     import killSalomeWithPort
00112     import killSalome
00113     def __init__(self, modules):
00114         import runSalome
00115         sys.argv  = ["dummy.py"]
00116         sys.argv += ["--terminal"]
00117         if modules:
00118             sys.argv += ['--modules=%s'%(",".join(modules))]
00119             pass
00120         runSalome.clt, runSalome.args = runSalome.main()
00121         import salome
00122         salome.salome_init()
00123         return
00124     def __del__(self):
00125         import runSalome
00126         runSalome.killLocalPort()
00127         return
00128     pass