Python 2.7x and 3.xx compatibility fixes

Change-Id: I84eb3bf7691fa867acadf9dae8c4f56a9781bf73
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/bin/process_plug_in_packages.py b/bin/process_plug_in_packages.py
index d6845cb..fc9f64b 100755
--- a/bin/process_plug_in_packages.py
+++ b/bin/process_plug_in_packages.py
@@ -1,7 +1,10 @@
 #!/usr/bin/env python
 
 import sys
-import __builtin__
+try:
+    import __builtin__
+except ImportError:
+    import builtins as __builtin__
 import subprocess
 import os
 import argparse
diff --git a/bin/validate_plug_ins.py b/bin/validate_plug_ins.py
index 9c0a1fd..ba4cebb 100755
--- a/bin/validate_plug_ins.py
+++ b/bin/validate_plug_ins.py
@@ -1,7 +1,11 @@
 #!/usr/bin/env python
 
 import sys
-import __builtin__
+try:
+    import __builtin__
+except ImportError:
+    import builtins as __builtin__
+
 import os
 
 # python puts the program's directory path in sys.path[0].  In other words,
diff --git a/lib/bmc_ssh_utils.py b/lib/bmc_ssh_utils.py
index 059ba9a..71e08da 100755
--- a/lib/bmc_ssh_utils.py
+++ b/lib/bmc_ssh_utils.py
@@ -5,13 +5,6 @@
 bmc_execute_command.
 """
 
-import sys
-try:
-    import exceptions
-except ImportError:
-    import builtins as exception
-import re
-import gen_print as gp
 import gen_valid as gv
 import gen_robot_ssh as grs
 from robot.libraries.BuiltIn import BuiltIn
diff --git a/lib/boot_data.py b/lib/boot_data.py
index 8dfdff0..070a0e6 100755
--- a/lib/boot_data.py
+++ b/lib/boot_data.py
@@ -74,7 +74,7 @@
 
     # For every boot_type we should have a corresponding mfg mode boot type.
     enhanced_boot_table = DotDict()
-    for key, value in boot_table.iteritems():
+    for key, value in boot_table.items():
         enhanced_boot_table[key] = value
         enhanced_boot_table[key + " (mfg)"] = value
 
diff --git a/lib/code_update_utils.py b/lib/code_update_utils.py
index cdb2ac4..16487db 100644
--- a/lib/code_update_utils.py
+++ b/lib/code_update_utils.py
@@ -155,11 +155,13 @@
     for member in tar.getmembers():
         f = tar.extractfile(member)
         content = f.read()
-        if "version=" in content:
-            content = content.split("\n")
-            content = [x for x in content if "version=" in x]
-            version = content[0].split("=")[-1]
-            break
+        if content.find(b"version=") == -1:
+            # This tar member does not contain the version.
+            continue
+        content = content.decode("utf-8").split("\n")
+        content = [x for x in content if "version=" in x]
+        version = content[0].split("=")[-1]
+        break
     tar.close()
     return version
 
diff --git a/lib/code_update_utils.robot b/lib/code_update_utils.robot
index dba32c1..29d7916 100644
--- a/lib/code_update_utils.robot
+++ b/lib/code_update_utils.robot
@@ -175,7 +175,7 @@
     ${image_data}=  OperatingSystem.Get Binary File  ${image_file_path}
 
     Wait Until Keyword Succeeds  3 times  60 sec
-    ...   Upload Image To BMC  /upload/image  data=${image_data}
+    ...   Upload Image To BMC  /upload/image  timeout=${30}  data=${image_data}
     ${ret}  ${version_id}=  Verify Image Upload  ${image_version}
     Should Be True  ${ret}
 
diff --git a/lib/dump_utils.py b/lib/dump_utils.py
index 94b6b36..536b73e 100755
--- a/lib/dump_utils.py
+++ b/lib/dump_utils.py
@@ -124,7 +124,7 @@
     status, ret_values = grk.run_key("Open Connection for SCP", quiet=quiet)
 
     dump_file_list = []
-    for dump_id, source_file_path in dump_dict.iteritems():
+    for dump_id, source_file_path in dump_dict.items():
         targ_file_path = targ_dir_path + targ_file_prefix \
             + os.path.basename(source_file_path)
         status, ret_values = grk.run_key("scp.Get File  " + source_file_path
diff --git a/lib/func_timer.py b/lib/func_timer.py
index e4855e7..5f83241 100644
--- a/lib/func_timer.py
+++ b/lib/func_timer.py
@@ -86,7 +86,11 @@
         try:
             gp.lprint_executing()
             gp.lprint_var(self.__child_pid)
-        except AttributeError:
+        except (AttributeError, KeyError):
+            # NOTE: In python 3, this code fails with "KeyError:
+            # ('__main__',)" when calling functions like lprint_executing that
+            # use inspect.stack() during object destruction.  No fixes found
+            # so tolerating the error.
             pass
 
         # If self.__child_pid is 0, then we are either running as the child
@@ -189,9 +193,11 @@
             self.__time_out = kwargs['time_out']
             del kwargs['time_out']
             # Convert "none" string to None.
-            if type(self.__time_out) in (str, unicode)\
-               and self.__time_out.lower() == "none":
-                self.__time_out = None
+            try:
+                if self.__time_out.lower() == "none":
+                    self.__time_out = None
+            except AttributeError:
+                pass
             if self.__time_out is not None:
                 self.__time_out = int(self.__time_out)
                 # Ensure that time_out is non-negative.
diff --git a/lib/gen_cmd.py b/lib/gen_cmd.py
index 5e004c1..6f88be1 100644
--- a/lib/gen_cmd.py
+++ b/lib/gen_cmd.py
@@ -96,7 +96,11 @@
     out_buf = ""
     if return_stderr:
         for line in sub_proc.stderr:
-            err_buf += line
+            try:
+                err_buf += line
+            except TypeError:
+                line = line.decode("utf-8")
+                err_buf += line
             if not print_output:
                 continue
             if robot_env:
@@ -104,7 +108,11 @@
             else:
                 sys.stdout.write(line)
     for line in sub_proc.stdout:
-        out_buf += line
+        try:
+            out_buf += line
+        except TypeError:
+            line = line.decode("utf-8")
+            out_buf += line
         if not print_output:
             continue
         if robot_env:
@@ -417,12 +425,20 @@
         try:
             if return_stderr:
                 for line in sub_proc.stderr:
-                    err_buf += line
+                    try:
+                        err_buf += line
+                    except TypeError:
+                        line = line.decode("utf-8")
+                        err_buf += line
                     if not print_output:
                         continue
                     func_stdout += line
             for line in sub_proc.stdout:
-                out_buf += line
+                try:
+                    out_buf += line
+                except TypeError:
+                    line = line.decode("utf-8")
+                    out_buf += line
                 if not print_output:
                     continue
                 func_stdout += line
diff --git a/lib/gen_misc.py b/lib/gen_misc.py
index f236320..db7ecf4 100755
--- a/lib/gen_misc.py
+++ b/lib/gen_misc.py
@@ -260,7 +260,7 @@
     try:
         config_parser = ConfigParser.ConfigParser()
     except NameError:
-        config_parser = configparser.ConfigParser()
+        config_parser = configparser.ConfigParser(strict=False)
     # Make the property names case-sensitive.
     config_parser.optionxform = str
     # Read the properties from the string file.
diff --git a/lib/gen_plug_in.py b/lib/gen_plug_in.py
index 1bd70a1..66a8583 100755
--- a/lib/gen_plug_in.py
+++ b/lib/gen_plug_in.py
@@ -6,7 +6,6 @@
 
 import sys
 import os
-import commands
 import glob
 
 import gen_print as gp
diff --git a/lib/gen_print.py b/lib/gen_print.py
index 5e5a013..b43f8c7 100755
--- a/lib/gen_print.py
+++ b/lib/gen_print.py
@@ -806,7 +806,7 @@
     try:
         string_types = (str, unicode)
     except NameError:
-        string_types = (str,)
+        string_types = (bytes, str)
     simple_types = int_types + string_types + (float, bool)
     if type(var_value) in simple_types \
        or var_value is None:
diff --git a/lib/gen_robot_plug_in.py b/lib/gen_robot_plug_in.py
index 5c1dd06..a89e07c 100755
--- a/lib/gen_robot_plug_in.py
+++ b/lib/gen_robot_plug_in.py
@@ -8,7 +8,6 @@
 import sys
 import subprocess
 from robot.libraries.BuiltIn import BuiltIn
-import commands
 import os
 import tempfile
 
@@ -32,16 +31,13 @@
     """
 
     cmd_buf = "validate_plug_ins.py \"" + plug_in_dir_paths + "\""
-    if int(quiet) != 1:
-        gp.print_issuing(cmd_buf)
-    rc, out_buf = commands.getstatusoutput(cmd_buf)
+    rc, out_buf = gc.shell_cmd(cmd_buf, print_output=0)
     if rc != 0:
-        message = gp.sprint_varx("rc", rc, 1) + out_buf
-        gp.printn(out_buf, 'STDERR')
         BuiltIn().fail(gp.sprint_error("Validate plug ins call failed.  See"
                                        + " stderr text for details.\n"))
 
-    plug_in_packages_list = out_buf.split("\n")
+    # plug_in_packages_list = out_buf.split("\n")
+    plug_in_packages_list = list(filter(None, out_buf.split("\n")))
     if len(plug_in_packages_list) == 1 and plug_in_packages_list[0] == "":
         return []
 
diff --git a/lib/gen_robot_ssh.py b/lib/gen_robot_ssh.py
index 7891059..1c9f564 100755
--- a/lib/gen_robot_ssh.py
+++ b/lib/gen_robot_ssh.py
@@ -13,7 +13,7 @@
 try:
     import exceptions
 except ImportError:
-    import builtins as exception
+    import builtins as exceptions
 
 import gen_print as gp
 import func_timer as ft
@@ -99,7 +99,7 @@
     for connection in sshlib.get_connections():
         # Create connection_dict from connection object.
         connection_dict = dict((key, str(value)) for key, value in
-                               connection._config.iteritems())
+                               connection._config.items())
         if dict(connection_dict, **open_connection_args) == connection_dict:
             return connection
 
diff --git a/lib/gen_robot_utils.py b/lib/gen_robot_utils.py
index 281c27a..fd15eed 100644
--- a/lib/gen_robot_utils.py
+++ b/lib/gen_robot_utils.py
@@ -66,7 +66,7 @@
 
     # If any variable values were changed due to the prior import, set them
     # back to their original values.
-    for key, value in post_var_dict.iteritems():
+    for key, value in post_var_dict.items():
         if key in pre_var_dict:
             if value != pre_var_dict[key]:
                 global_var_name = re.sub("[@&]", "$", key)
diff --git a/lib/gen_robot_valid.py b/lib/gen_robot_valid.py
index 876edcb..c69b8a4 100755
--- a/lib/gen_robot_valid.py
+++ b/lib/gen_robot_valid.py
@@ -187,8 +187,10 @@
         error_message = "Variable \"" + var_name +\
                         "\" not found (i.e. it's undefined).\n"
     else:
-        if isinstance(range, unicode):
+        try:
             range = range.split("..")
+        except AttributeError:
+            pass
         if range[0] == "":
             range[0] = None
         range = [x for x in range if x]
diff --git a/lib/logging_utils.py b/lib/logging_utils.py
index eca4c5e..9c43f21 100644
--- a/lib/logging_utils.py
+++ b/lib/logging_utils.py
@@ -68,8 +68,10 @@
     """
 
     if key_list is not None:
-        if type(key_list) in (str, unicode):
+        try:
             key_list = key_list.split(" ")
+        except AttributeError:
+            pass
         key_list.insert(0, var.BMC_LOGGING_ENTRY + ".*")
 
     gp.print_var(error_logs, hex=1, key_list=key_list)
diff --git a/lib/obmc_boot_test.py b/lib/obmc_boot_test.py
index 11e9b0e..4aa63cd 100755
--- a/lib/obmc_boot_test.py
+++ b/lib/obmc_boot_test.py
@@ -185,8 +185,8 @@
         ffdc_dir_path_style = int(os.environ.get('FFDC_DIR_PATH_STYLE', '0'))
 
     # Convert these program parms to lists for easier processing..
-    boot_list = filter(None, boot_list.split(":"))
-    boot_stack = filter(None, boot_stack.split(":"))
+    boot_list = list(filter(None, boot_list.split(":")))
+    boot_stack = list(filter(None, boot_stack.split(":")))
 
     cleanup_boot_results_file()
     boot_results_file_path = create_boot_results_file_path(pgm_name,
@@ -644,7 +644,7 @@
     try:
         plug_in_ffdc_list = \
             open(ffdc_list_file_path, 'r').read().rstrip("\n").split("\n")
-        plug_in_ffdc_list = filter(None, plug_in_ffdc_list)
+        plug_in_ffdc_list = list(filter(None, plug_in_ffdc_list))
     except IOError:
         plug_in_ffdc_list = []
 
@@ -1008,22 +1008,14 @@
 
     global save_stack
 
+    gp.dprintn()
     # Process function parms.
     for parm_name in main_func_parm_list:
         # Get parm's value.
-        cmd_buf = "parm_value = loc_" + parm_name
-        exec(cmd_buf)
-        gp.dpvar(parm_name)
-        gp.dpvar(parm_value)
+        parm_value = eval("loc_" + parm_name)
+        gp.dpvars(parm_name, parm_value)
 
-        if parm_value is None:
-            # Parm was not specified by the calling function so set it to its
-            # corresponding global value.
-            cmd_buf = "loc_" + parm_name + " = BuiltIn().get_variable_value" +\
-                "(\"${" + parm_name + "}\")"
-            gp.dpissuing(cmd_buf)
-            exec(cmd_buf)
-        else:
+        if parm_value is not None:
             # Save the global value on a stack.
             cmd_buf = "save_stack.push(BuiltIn().get_variable_value(\"${" +\
                 parm_name + "}\"), \"" + parm_name + "\")"
diff --git a/lib/state.py b/lib/state.py
index 0aae066..7cdcb8c 100755
--- a/lib/state.py
+++ b/lib/state.py
@@ -34,7 +34,6 @@
 import gen_cmd as gc
 import bmc_ssh_utils as bsu
 
-import commands
 from robot.libraries.BuiltIn import BuiltIn
 from robot.utils import DotDict
 
@@ -178,14 +177,12 @@
                                ('host', '^$')])
 
 
-def return_state_constant(state_name='default'):
+def return_state_constant(state_name='default_state'):
     r"""
     Return the named state dictionary constant.
     """
 
-    cmd_buf = "state = " + state_name
-    exec(cmd_buf)
-    return state
+    return eval(state_name)
 
 
 def anchor_state(state):
@@ -258,8 +255,10 @@
     if error_message != "":
         BuiltIn().fail(gp.sprint_error(error_message))
 
-    if type(match_state) in (str, unicode):
+    try:
         match_state = return_state_constant(match_state)
+    except TypeError:
+        pass
 
     default_match = (match_type == 'and')
     for key, match_state_value in match_state.items():
@@ -346,10 +345,9 @@
     if os_up:
         if 'os_ping' in req_states:
             # See if the OS pings.
-            cmd_buf = "ping -c 1 -w 2 " + os_host
-            if not quiet:
-                gp.pissuing(cmd_buf)
-            rc, out_buf = commands.getstatusoutput(cmd_buf)
+            rc, out_buf = gc.shell_cmd("ping -c 1 -w 2 " + os_host,
+                                       print_output=0, show_err=0,
+                                       ignore_err=1)
             if rc == 0:
                 os_ping = 1
 
@@ -482,10 +480,9 @@
     # Get the component states.
     if 'ping' in req_states:
         # See if the OS pings.
-        cmd_buf = "ping -c 1 -w 2 " + openbmc_host
-        if not quiet:
-            gp.pissuing(cmd_buf)
-        rc, out_buf = commands.getstatusoutput(cmd_buf)
+        rc, out_buf = gc.shell_cmd("ping -c 1 -w 2 " + openbmc_host,
+                                   print_output=0, show_err=0,
+                                   ignore_err=1)
         if rc == 0:
             ping = 1
 
@@ -493,9 +490,9 @@
         # See if the OS pings.
         cmd_buf = "ping -c 5 -w 5 " + openbmc_host +\
             " | egrep 'packet loss' | sed -re 's/.* ([0-9]+)%.*/\\1/g'"
-        if not quiet:
-            gp.pissuing(cmd_buf)
-        rc, out_buf = commands.getstatusoutput(cmd_buf)
+        rc, out_buf = gc.shell_cmd(cmd_buf,
+                                   print_output=0, show_err=0,
+                                   ignore_err=1)
         if rc == 0:
             packet_loss = out_buf.rstrip("\n")
 
@@ -564,10 +561,12 @@
             for url_path in ret_values:
                 for attr_name in ret_values[url_path]:
                     # Create a state key value based on the attr_name.
-                    if isinstance(ret_values[url_path][attr_name], unicode):
+                    try:
                         ret_values[url_path][attr_name] = \
                             re.sub(r'.*\.', "",
                                    ret_values[url_path][attr_name])
+                    except TypeError:
+                        pass
                     # Do some key name manipulations.
                     new_attr_name = re.sub(r'^Current|(State|Transition)$',
                                            "", attr_name)
@@ -747,8 +746,10 @@
 
     quiet = int(gp.get_var_value(quiet, 0))
 
-    if type(match_state) in (str, unicode):
+    try:
         match_state = return_state_constant(match_state)
+    except TypeError:
+        pass
 
     if not quiet:
         if invert:
diff --git a/lib/utilities.py b/lib/utilities.py
index 5093333..ae3ad24 100755
--- a/lib/utilities.py
+++ b/lib/utilities.py
@@ -118,7 +118,7 @@
 
 
 def main():
-    print get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
+    print(get_vpd_inventory_list('../data/Palmetto.py', 'DIMM'))
 
 
 if __name__ == "__main__":
diff --git a/lib/utils.py b/lib/utils.py
index e32a91b..e2188cc 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -298,4 +298,7 @@
     json_str                        The string containing the JSON data.
     """
 
-    return json.loads(json_str, object_pairs_hook=DotDict)
+    try:
+        return json.loads(json_str, object_pairs_hook=DotDict)
+    except TypeError:
+        return json.loads(json_str.decode("utf-8"), object_pairs_hook=DotDict)
diff --git a/syslib/utils_os.robot b/syslib/utils_os.robot
index 0c518a3..89a5d5e 100755
--- a/syslib/utils_os.robot
+++ b/syslib/utils_os.robot
@@ -2,6 +2,7 @@
 Documentation      Keywords for system test.
 
 Library            ../lib/gen_robot_keyword.py
+Library            ../lib/gen_robot_print.py
 Resource           ../lib/boot_utils.robot
 Resource           ../extended/obmc_boot_test_resource.robot
 Resource           ../lib/utils.robot
@@ -222,14 +223,14 @@
     ${temperature_objs}=  Read Properties
     ...  ${SENSORS_URI}temperature/enumerate
     # Filter the dictionary to get just the CPU temperature info.
-    ${cmd}=  Catenate  {k:v for k,v in $temperature_objs.iteritems()
+    ${cmd}=  Catenate  {k:v for k,v in $temperature_objs.items()
     ...  if re.match('${SENSORS_URI}temperature/p.*core.*temp', k)}
     ${cpu_temperatuture_objs}  Evaluate  ${cmd}  modules=re
     # Create a list of the CPU temperature values (current).
     ${cpu_temperatures}=  Evaluate
     ...  [ x['Value'] for x in $cpu_temperatuture_objs.values() ]
 
-    ${cpu_max_temp}  Evaluate  max(map(int, $cpu_temperatures))/1000
+    ${cpu_max_temp}  Evaluate  int(max(map(int, $cpu_temperatures))/1000)
     [Return]  ${cpu_max_temp}
 
 
@@ -239,14 +240,14 @@
     ${temperature_objs}=  Read Properties
     ...  ${SENSORS_URI}temperature/enumerate
     # Filter the dictionary to get just the CPU temperature info.
-    ${cmd}=  Catenate  {k:v for k,v in $temperature_objs.iteritems()
+    ${cmd}=  Catenate  {k:v for k,v in $temperature_objs.items()
     ...  if re.match('${SENSORS_URI}temperature/p.*core.*temp', k)}
     ${cpu_temperatuture_objs}=  Evaluate  ${cmd}  modules=re
     # Create a list of the CPU temperature values (current).
     ${cpu_temperatures}=  Evaluate
     ...  [ x['Value'] for x in $cpu_temperatuture_objs.values() ]
 
-    ${cpu_min_temp}  Evaluate  min(map(int, $cpu_temperatures))/1000
+    ${cpu_min_temp}  Evaluate  int(min(map(int, $cpu_temperatures))/1000)
     [Return]  ${cpu_min_temp}
 
 
@@ -397,7 +398,7 @@
     ${temperature_objs}=  Read Properties  ${SENSORS_URI}temperature/enumerate
     ...  timeout=30  quiet=1
 
-    ${core_temperatures_list}=  Catenate  {k:v for k,v in $temperature_objs.iteritems()
+    ${core_temperatures_list}=  Catenate  {k:v for k,v in $temperature_objs.items()
     ...  if re.match('${SENSORS_URI}temperature/.*_core_temp', k)}
     ${gpu_temperature_objs_dict}=  Evaluate  ${core_temperatures_list}  modules=re
 
@@ -407,7 +408,8 @@
 
     # Find the max temperature value and divide by 1000 to get just the integer
     # portion.
-    ${max_gpu_temperature}=  Evaluate  max(map(int, $gpu_temperatures))/1000
+    ${max_gpu_temperature}=  Evaluate
+    ...  int(max(map(int, $gpu_temperatures))/1000)
 
     [Return]  ${max_gpu_temperature}