]> SALOME platform Git repositories - modules/kernel.git/blob - bin/searchFreePort.py
Salome HOME
Get info returned by writeORBConfigFile
[modules/kernel.git] / bin / searchFreePort.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2012  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
25 def searchFreePort(args={}, save_config=1):
26   """
27   Search free port for SALOME session.
28   Returns first found free port number.
29   """
30   import sys, os, re, shutil
31   print "Searching for a free port for naming service:",
32
33   # :NOTE: Under windows:
34   #        netstat options -l and -t are unavailable
35   #        grep command is unavailable
36
37   from subprocess import Popen, PIPE
38   (stdout, stderr) = Popen(['netstat','-an'], stdout=PIPE).communicate()
39   import StringIO
40   buf = StringIO.StringIO(stdout)
41   ports = buf.readlines()
42
43   #
44   def portIsUsed(port, data):
45     regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE );
46     for item in data:
47       try:
48         p = int(regObj.match(item).group(1))
49         if p == port: return True
50         pass
51       except:
52         pass
53       pass
54     return False
55   #
56   NSPORT=2810
57   limit=NSPORT+100
58   #
59
60   while 1:
61     if not portIsUsed(NSPORT, ports):
62       print "%s - OK"%(NSPORT)
63       #
64       from salome_utils import generateFileName, getHostName
65       hostname = getHostName()
66       #
67       home  = os.getenv("HOME")
68       appli = os.getenv("APPLI")
69       kwargs={}
70       if appli is not None:
71         home = os.path.join(os.path.realpath(home), appli,"USERS")
72         kwargs["with_username"]=True
73       #
74       from ORBConfigFile import writeORBConfigFile
75       omniorb_config, giopsize = writeORBConfigFile(home, hostname, NSPORT, kwargs)
76
77       args['port'] = os.environ['NSPORT']
78       #
79       if save_config:
80         last_running_config = generateFileName(home, prefix="omniORB",
81                                                suffix="last",
82                                                extension="cfg",
83                                                hidden=True,
84                                                **kwargs)
85         os.environ['LAST_RUNNING_CONFIG'] = last_running_config
86         try:
87           if sys.platform == "win32":
88             import shutil
89             shutil.copyfile(omniorb_config, last_running_config)
90           else:
91             try:
92               if os.access(last_running_config, os.F_OK):
93                 os.remove(last_running_config)
94             except OSError:
95               pass
96             os.symlink(omniorb_config, last_running_config)
97             pass
98           pass
99         except:
100           pass
101       #
102       break
103     print "%s"%(NSPORT),
104     if NSPORT == limit:
105       msg  = "\n"
106       msg += "Can't find a free port to launch omniNames\n"
107       msg += "Try to kill the running servers and then launch SALOME again.\n"
108       raise RuntimeError, msg
109     NSPORT=NSPORT+1
110     pass
111   return