Salome HOME
[EDF29322] Revert "[EDF] Taking SALOME launcher options into account through environm...
[modules/kernel.git] / bin / searchFreePort.py
index 999a952e9b28352d8f5f14fec9d69fb99acee29c..59ce2f0ea4742ec55240d31e671b0d0a93c287d7 100644 (file)
@@ -1,6 +1,5 @@
-#!/usr/bin/env python
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -59,93 +58,37 @@ def __setup_config(nsport, args, save_config):
         os.symlink(omniorb_config, last_running_config)
         pass
       pass
-    except:
+    except Exception:
       pass
   #
 #
 
-def searchFreePort_withoutPortManager(args={}, save_config=1, use_port=None):
-  # :NOTE: Under windows:
-  #        netstat options -l and -t are unavailable
-  #        grep command is unavailable
-  from subprocess import Popen, PIPE
-  (stdout, stderr) = Popen(['netstat','-an'], stdout=PIPE).communicate()
-  import StringIO
-  buf = StringIO.StringIO(stdout)
-  ports = buf.readlines()
-
-  #
-  def portIsUsed(port, data):
-    import re
-    regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE );
-    for item in data:
-      try:
-        p = int(regObj.match(item).group(1))
-        if p == port: return True
-        pass
-      except:
-        pass
-      pass
-    return False
-  #
-
-  if use_port:
-    print "Check if port can be used: %d" % use_port,
-    if not portIsUsed(use_port, ports):
-      print "- OK"
-      __setup_config(use_port, args, save_config)
-      return
-    else:
-      print "- KO: port is busy"
-    pass
-  #
-
-  print "Searching for a free port for naming service:",
-  #
-
-  NSPORT=2810
-  limit=NSPORT+100
-  #
-
-  while 1:
-    if not portIsUsed(NSPORT, ports):
-      print "%s - OK"%(NSPORT)
-      __setup_config(NSPORT, args, save_config)
-      break
-    print "%s"%(NSPORT),
-    if NSPORT == limit:
-      msg  = "\n"
-      msg += "Can't find a free port to launch omniNames\n"
-      msg += "Try to kill the running servers and then launch SALOME again.\n"
-      raise RuntimeError, msg
-    NSPORT=NSPORT+1
-    pass
-  #
-#
-
 def searchFreePort_withPortManager(queue, args={}, save_config=1, use_port=None):
   from PortManager import getPort
   port = getPort(use_port)
 
   if use_port:
-    print "Check if port can be used: %d" % use_port,
+    print("Check if port can be used: %d" % use_port, end=' ')
     if port == use_port and port != -1:
-      print "- OK"
+      print("- OK")
       __setup_config(use_port, args, save_config)
+      queue.put([os.environ['OMNIORB_CONFIG'],
+                 os.environ['NSPORT'],
+                 os.environ['NSHOST']])
       return
     else:
-      print "- KO: port is busy"
+      print("- KO: port is busy")
       pass
   #
-  print "Searching for a free port for naming service:",
+  print("Searching for a free port for naming service:", end=' ')
   if port == -1: # try again
     port = getPort(use_port)
 
   if port != -1:
-    print "%s - OK"%(port)
+    print("%s - OK"%(port))
     __setup_config(port, args, save_config)
   else:
-    print "Unable to obtain port"
+    print("Unable to obtain port")
 
   queue.put([os.environ['OMNIORB_CONFIG'],
              os.environ['NSPORT'],
@@ -155,7 +98,7 @@ def searchFreePort_withPortManager(queue, args={}, save_config=1, use_port=None)
 def __savePortToFile(args):
   # Save Naming service port name into
   # the file args["ns_port_log_file"]
-  if args.has_key('ns_port_log_file'):
+  if 'ns_port_log_file' in args:
     omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
     file_name = os.path.join(omniorbUserPath, args["ns_port_log_file"])
     with open(file_name, "w") as f:
@@ -167,7 +110,6 @@ def searchFreePort(args={}, save_config=1, use_port=None):
   Search free port for SALOME session.
   Returns first found free port number.
   """
-
   try:
     import PortManager # mandatory
     from multiprocessing import Process, Queue
@@ -178,11 +120,11 @@ def searchFreePort(args={}, save_config=1, use_port=None):
 
     os.environ['OMNIORB_CONFIG'] = info[0]
     os.environ['NSPORT'] = info[1]
+    args['port'] = os.environ['NSPORT']
     os.environ['NSHOST'] = info[2]
     __savePortToFile(args)
 
     p.join() # this blocks until the process terminates
   except ImportError:
-    searchFreePort_withoutPortManager(args, save_config, use_port)
-    __savePortToFile(args)
+    raise Exception('PortManager module not found')
 #