Salome HOME
a384db635ad8eabf14156379453960761441b33d
[modules/kernel.git] / bin / searchFreePort.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2013  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 import os
26 import sys
27
28 def __setup_config(nsport, args, save_config):
29   #
30   from salome_utils import generateFileName, getHostName
31   hostname = getHostName()
32   #
33   omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
34   kwargs={}
35   if omniorbUserPath is not None:
36     kwargs["with_username"]=True
37   #
38   from ORBConfigFile import writeORBConfigFile
39   omniorb_config, giopsize = writeORBConfigFile(omniorbUserPath, hostname, nsport, kwargs)
40   args['port'] = os.environ['NSPORT']
41   #
42   if save_config:
43     last_running_config = generateFileName(omniorbUserPath, prefix="omniORB",
44                                            suffix="last",
45                                            extension="cfg",
46                                            hidden=True,
47                                            **kwargs)
48     #os.environ['LAST_RUNNING_CONFIG'] = last_running_config
49     try:
50       if sys.platform == "win32":
51         import shutil
52         shutil.copyfile(omniorb_config, last_running_config)
53       else:
54         try:
55           if os.access(last_running_config, os.F_OK):
56             os.remove(last_running_config)
57         except OSError:
58           pass
59         os.symlink(omniorb_config, last_running_config)
60         pass
61       pass
62     except:
63       pass
64   #
65 #
66
67 def searchFreePort(args={}, save_config=1, use_port=None):
68   """
69   Search free port for SALOME session.
70   Returns first found free port number.
71   """
72
73   import PortManager
74   client = PortManager.start_client() # :NOTE: might specify a (remote) IP
75   portManager = client.PortManager()
76
77   if use_port:
78     print "Check if port can be used: %d" % use_port,
79     if not portManager.isPortUsed(use_port):
80       print "- OK"
81       __setup_config(use_port, args, save_config)
82       return
83     else:
84       print "- KO: port is busy"
85       pass
86   #
87   print "Searching for a free port for naming service:",
88   port = portManager.getPort()
89   print "%s - OK"%(port)
90   __setup_config(port, args, save_config)
91   port = portManager.releasePort(port)
92 #