Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / build / android / pylib / ports.py
index 34efb52..578152c 100644 (file)
@@ -9,11 +9,9 @@ import fcntl
 import httplib
 import logging
 import os
-import re
 import socket
 import traceback
 
-from pylib import cmd_helper
 from pylib import constants
 
 
@@ -57,7 +55,7 @@ def AllocateTestServerPort():
     with open(constants.TEST_SERVER_PORT_FILE, 'r+') as fp:
       port = int(fp.read())
       ports_tried.append(port)
-      while IsHostPortUsed(port):
+      while not IsHostPortAvailable(port):
         port += 1
         ports_tried.append(port)
       if (port > constants.TEST_SERVER_PORT_LAST or
@@ -67,7 +65,7 @@ def AllocateTestServerPort():
         fp.seek(0, os.SEEK_SET)
         fp.write('%d' % (port + 1))
   except Exception as e:
-    logging.info(e)
+    logging.error(e)
   finally:
     if fp_lock:
       fcntl.flock(fp_lock, fcntl.LOCK_UN)
@@ -80,25 +78,23 @@ def AllocateTestServerPort():
   return port
 
 
-def IsHostPortUsed(host_port):
-  """Checks whether the specified host port is used or not.
-
-  Uses -n -P to inhibit the conversion of host/port numbers to host/port names.
+def IsHostPortAvailable(host_port):
+  """Checks whether the specified host port is available.
 
   Args:
-    host_port: Port on host we want to check.
+    host_port: Port on host to check.
 
   Returns:
-    True if the port on host is already used, otherwise returns False.
+    True if the port on host is available, otherwise returns False.
   """
-  port_info = '(\*)|(127\.0\.0\.1)|(localhost):%d' % host_port
-  # TODO(jnd): Find a better way to filter the port. Note that connecting to the
-  # socket and closing it would leave it in the TIME_WAIT state. Setting
-  # SO_LINGER on it and then closing it makes the Python HTTP server crash.
-  re_port = re.compile(port_info, re.MULTILINE)
-  if re_port.search(cmd_helper.GetCmdOutput(['lsof', '-nPi:%d' % host_port])):
+  s = socket.socket()
+  try:
+    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+    s.bind(('', host_port))
+    s.close()
     return True
-  return False
+  except socket.error:
+    return False
 
 
 def IsDevicePortUsed(device, device_port, state=''):