Consolidated code update Python code and placed in lib

Resolves openbmc/openbmc-test-automation#834

Change-Id: I571bab9d63abab4386accec025d1402e739a6a56
Signed-off-by: Charles Paul Hofer <Charles.Hofer@ibm.com>
diff --git a/extended/code_update/code_update.py b/extended/code_update/code_update.py
deleted file mode 100644
index 1569107..0000000
--- a/extended/code_update/code_update.py
+++ /dev/null
@@ -1,74 +0,0 @@
-#!/usr/bin/env python
-
-r"""
-This module is the python counterpart to code_update.robot.
-"""
-
-import os
-import sys
-import re
-import string
-import tarfile
-import time
-
-robot_pgm_dir_path = os.path.dirname(__file__) + os.sep
-repo_lib_path = re.sub('/extended/code_update/', '/lib', robot_pgm_dir_path)
-repo_data_path = re.sub('/extended/code_update/', '/data', robot_pgm_dir_path)
-sys.path.append(repo_lib_path)
-sys.path.append(repo_data_path)
-
-import gen_robot_keyword as keyword
-import gen_print as gp
-import gen_valid as gv
-import variables as var
-from robot.libraries.BuiltIn import BuiltIn
-
-
-###############################################################################
-def delete_all_pnor_images():
-
-    r"""
-    Delete all PNOR images from the BMC.
-    """
-
-    status, images = keyword.run_key("Read Properties  "
-                                     + var.SOFTWARE_VERSION_URI + "enumerate")
-    for image_name in images:
-        image_id = image_name.split('/')[-1]
-        image_purpose = images[image_name]["Purpose"]
-        if var.VERSION_PURPOSE_HOST == image_purpose:
-            # Delete twice, in case the image is in the /tmp/images directory
-            keyword.run_key("Call Method  " + var.SOFTWARE_VERSION_URI
-                            + image_id + "  delete  data={\"data\":[]}")
-            keyword.run_key("Call Method  " + var.SOFTWARE_VERSION_URI
-                            + image_id + "  delete  data={\"data\":[]}")
-
-###############################################################################
-
-
-###############################################################################
-def wait_for_activation_state_change(version_id, initial_state):
-
-    r"""
-    Wait for the current activation state of ${version_id} to
-    change from the state provided by the calling function.
-
-    Description of argument(s):
-    version_id     The version ID whose state change we are waiting for.
-    initial_state  The activation state we want to wait for.
-    """
-
-    keyword.run_key_u("Open Connection And Log In")
-    retry = 0
-    while (retry < 20):
-        status, software_state = keyword.run_key("Read Properties  " +
-                                    var.SOFTWARE_VERSION_URI + str(version_id))
-        current_state = (software_state)["Activation"]
-        if (initial_state == current_state):
-            time.sleep(60)
-            retry += 1
-        else:
-            return
-    return
-
-###############################################################################
diff --git a/extended/code_update/code_update.robot b/extended/code_update/code_update.robot
index 0d57788..73e86b6 100644
--- a/extended/code_update/code_update.robot
+++ b/extended/code_update/code_update.robot
@@ -14,9 +14,7 @@
 ...                 - Wait for code update to complete
 ...                 - Verify the new version
 
-#TODO: Move test_uploadimage.py to lib/
-Library           ../test_uploadimage.py
-Library           code_update.py
+Library           ../../lib/code_update_utils.py
 Library           OperatingSystem
 Variables         ../../data/variables.py
 Resource          ../lib/rest_client.robot
diff --git a/extended/test_uploadimage.robot b/extended/test_uploadimage.robot
index 8aea9d6..8aa0595 100644
--- a/extended/test_uploadimage.robot
+++ b/extended/test_uploadimage.robot
@@ -19,7 +19,7 @@
 Library               Collections
 Library               String
 Library               OperatingSystem
-Library               test_uploadimage.py
+Library               ../lib/code_update_utils.py
 
 Test Teardown  Upload Image Teardown
 
diff --git a/extended/test_uploadimage.py b/lib/code_update_utils.py
similarity index 68%
rename from extended/test_uploadimage.py
rename to lib/code_update_utils.py
index 0b6bcd7..a785fe6 100644
--- a/extended/test_uploadimage.py
+++ b/lib/code_update_utils.py
@@ -1,27 +1,73 @@
 #!/usr/bin/env python
 
 r"""
-This module is the python counterpart to test_uploadimage.robot.
+This module provides utilities for code updates.
 """
 
 import os
-import sys
 import re
-import string
+import sys
 import tarfile
 import time
 
 robot_pgm_dir_path = os.path.dirname(__file__) + os.sep
-repo_lib_path = re.sub('/extended/', '/lib', robot_pgm_dir_path)
-repo_data_path = re.sub('/extended/', '/data', robot_pgm_dir_path)
-sys.path.append(repo_lib_path)
+repo_data_path = re.sub('/lib', '/data', robot_pgm_dir_path)
 sys.path.append(repo_data_path)
 
-import gen_robot_keyword as grk
+import gen_robot_keyword as keyword
 import gen_print as gp
 import variables as var
 from robot.libraries.BuiltIn import BuiltIn
 
+###############################################################################
+def delete_all_pnor_images():
+
+    r"""
+    Delete all PNOR images from the BMC.
+    """
+
+    status, images = keyword.run_key("Read Properties  "
+                                     + var.SOFTWARE_VERSION_URI + "enumerate")
+    for image_name in images:
+        image_id = image_name.split('/')[-1]
+        image_purpose = images[image_name]["Purpose"]
+        if var.VERSION_PURPOSE_HOST == image_purpose:
+            # Delete twice, in case the image is in the /tmp/images directory
+            keyword.run_key("Call Method  " + var.SOFTWARE_VERSION_URI
+                            + image_id + "  delete  data={\"data\":[]}")
+            keyword.run_key("Call Method  " + var.SOFTWARE_VERSION_URI
+                            + image_id + "  delete  data={\"data\":[]}")
+
+###############################################################################
+
+
+###############################################################################
+def wait_for_activation_state_change(version_id, initial_state):
+
+    r"""
+    Wait for the current activation state of ${version_id} to
+    change from the state provided by the calling function.
+
+    Description of argument(s):
+    version_id     The version ID whose state change we are waiting for.
+    initial_state  The activation state we want to wait for.
+    """
+
+    keyword.run_key_u("Open Connection And Log In")
+    retry = 0
+    while (retry < 20):
+        status, software_state = keyword.run_key("Read Properties  " +
+                                    var.SOFTWARE_VERSION_URI + str(version_id))
+        current_state = (software_state)["Activation"]
+        if (initial_state == current_state):
+            time.sleep(60)
+            retry += 1
+        else:
+            return
+    return
+
+###############################################################################
+
 
 ###############################################################################
 def get_latest_file(dir_path):
@@ -35,9 +81,9 @@
                 calling function.
     """
 
-    grk.run_key_u("Open Connection And Log In")
+    keyword.run_key_u("Open Connection And Log In")
     status, ret_values =\
-            grk.run_key("Execute Command On BMC  cd " + dir_path
+            keyword.run_key("Execute Command On BMC  cd " + dir_path
             + "; stat -c '%Y %n' * | sort -k1,1nr | head -n 1", ignore=1)
     return ret_values.split(" ")[-1]
 
@@ -80,9 +126,9 @@
     file_path    The path to a file that holds the image version.
     """
 
-    grk.run_key_u("Open Connection And Log In")
+    keyword.run_key_u("Open Connection And Log In")
     status, ret_values =\
-            grk.run_key("Execute Command On BMC  cat "
+            keyword.run_key("Execute Command On BMC  cat "
             + file_path + " | grep \"version=\"", ignore=1)
     return (ret_values.split("\n")[0]).split("=")[-1]
 
@@ -99,9 +145,9 @@
     file_path    The path to a file that holds the image purpose.
     """
 
-    grk.run_key_u("Open Connection And Log In")
+    keyword.run_key_u("Open Connection And Log In")
     status, ret_values =\
-            grk.run_key("Execute Command On BMC  cat "
+            keyword.run_key("Execute Command On BMC  cat "
             + file_path + " | grep \"purpose=\"", ignore=1)
     return ret_values.split("=")[-1]
 
@@ -123,9 +169,9 @@
     """
 
     upload_dir = BuiltIn().get_variable_value("${upload_dir_path}")
-    grk.run_key_u("Open Connection And Log In")
+    keyword.run_key_u("Open Connection And Log In")
     status, image_list =\
-            grk.run_key("Execute Command On BMC  ls -d " + upload_dir
+            keyword.run_key("Execute Command On BMC  ls -d " + upload_dir
             + "*/")
 
     image_list = image_list.split("\n")
@@ -159,7 +205,7 @@
     image_version_id = image_path.split("/")[-2]
     BuiltIn().set_global_variable("${version_id}", image_version_id)
 
-    grk.run_key_u("Open Connection And Log In")
+    keyword.run_key_u("Open Connection And Log In")
     image_purpose = get_image_purpose(image_path + "MANIFEST")
     if (image_purpose == var.VERSION_PURPOSE_BMC or
         image_purpose == var.VERSION_PURPOSE_HOST):
@@ -167,7 +213,7 @@
         ret_values = ""
         for itr in range(timeout * 2):
             status, ret_values = \
-                grk.run_key("Read Attribute  " + uri + "  Activation")
+                keyword.run_key("Read Attribute  " + uri + "  Activation")
 
             if ((ret_values == var.READY) or (ret_values == var.INVALID)
                     or (ret_values == var.ACTIVE)):
@@ -200,16 +246,16 @@
                    Default is 3 minutes.
     """
 
-    grk.run_key('Open Connection And Log In')
+    keyword.run_key('Open Connection And Log In')
     upload_dir_path = BuiltIn().get_variable_value("${upload_dir_path}")
     for i in range(timeout * 2):
-        stat, grep_res = grk.run_key('Execute Command On BMC  '
+        stat, grep_res = keyword.run_key('Execute Command On BMC  '
                 + 'ls ' + upload_dir_path + '*/MANIFEST 2>/dev/null '
                 + '| xargs grep -rl "version=' + image_version + '"')
         image_dir = os.path.dirname(grep_res.split('\n')[0])
         if '' != image_dir:
-            grk.run_key('Execute Command On BMC  rm -rf ' + image_dir)
+            keyword.run_key('Execute Command On BMC  rm -rf ' + image_dir)
             BuiltIn().fail('Found invalid BMC Image: ' + image_dir)
         time.sleep(30)
 
-###############################################################################
+###############################################################################
\ No newline at end of file