Merge "Rename test/unit/cmd directory"
[yardstick.git] / yardstick / common / utils.py
index 6ac99a5..82e20be 100644 (file)
@@ -76,7 +76,7 @@ def import_modules_from_package(package):
     """
     yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__))
     path = os.path.join(yardstick_root, *package.split("."))
-    for root, dirs, files in os.walk(path):
+    for root, _, files in os.walk(path):
         matches = (filename for filename in files if filename.endswith(".py") and
                    not filename.startswith("__"))
         new_package = os.path.relpath(root, yardstick_root).replace(os.sep, ".")
@@ -251,10 +251,10 @@ def set_dict_value(dic, keys, value):
 
 def get_free_port(ip):
     with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
-        while True:
+        port = random.randint(5000, 10000)
+        while s.connect_ex((ip, port)) == 0:
             port = random.randint(5000, 10000)
-            if s.connect_ex((ip, port)) != 0:
-                return port
+        return port
 
 
 def mac_address_to_hex_list(mac):
@@ -350,10 +350,13 @@ def config_to_dict(config):
 
 
 def validate_non_string_sequence(value, default=None, raise_exc=None):
-    if isinstance(value, collections.Sequence) and not isinstance(value, str):
+    # NOTE(ralonsoh): refactor this function to check if raise_exc is an
+    # Exception. Remove duplicate code, this function is duplicated in this
+    # repository.
+    if isinstance(value, collections.Sequence) and not isinstance(value, six.string_types):
         return value
     if raise_exc:
-        raise raise_exc
+        raise raise_exc  # pylint: disable=raising-bad-type
     return default
 
 
@@ -365,6 +368,13 @@ def join_non_strings(separator, *non_strings):
     return str(separator).join(str(non_string) for non_string in non_strings)
 
 
+def safe_decode_utf8(s):
+    """Safe decode a str from UTF"""
+    if six.PY3 and isinstance(s, bytes):
+        return s.decode('utf-8', 'surrogateescape')
+    return s
+
+
 class ErrorClass(object):
 
     def __init__(self, *args, **kwargs):