Skip to contentSkip to conversation

Utils: Moving function ask to common lib utils

PATCH New Lucas Meneghel Rodrigues 1 participant 2 messages May 5, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Utils: Moving function ask to common lib utils

This way we can use it in other interactive scripts in autotest.

---
 client/common_lib/base_utils.py |   14 ++++++++++++++
 utils/check_patch.py            |   22 ++++------------------
 2 files changed, 18 insertions(+), 18 deletions(-)
client/common_lib/base_utils.py +14 -0
index c8f2f1a..db7b869 100644
@@ -1715,3 +1715,17 @@ def get_unused_port():
         # Check if this port is unused on the other protocol.
         if port and try_bind(port, socket.SOCK_DGRAM, socket.IPPROTO_UDP):
             return port
+
+
+def ask(question, auto=False):
+    """
+    Raw input with a prompt that emulates logging.
+
+    @param question: Question to be asked
+    @param auto: Whether to return "y" instead of asking the question
+    """
+    if auto:
+        logging.info("%s (y/n) y" % question)
+        return "y"
+    return raw_input("%s INFO | %s (y/n) " %
+                     (time.strftime("%H:%M:%S", time.localtime()), question))
utils/check_patch.py +4 -18
index 78af6b9..bd60d66 100755
@@ -32,20 +32,6 @@ class CheckPatchLoggingConfig(logging_config.LoggingConfig):
                                                                verbose=verbose)
 
 
-def ask(question, auto=False):
-    """
-    Raw input with a prompt that emulates logging.
-
-    @param question: Question to be asked
-    @param auto: Whether to return "y" instead of asking the question
-    """
-    if auto:
-        logging.info("%s (y/n) y" % question)
-        return "y"
-    return raw_input("%s INFO | %s (y/n) " %
-                     (time.strftime("%H:%M:%S", time.localtime()), question))
-
-
 class VCS(object):
     """
     Abstraction layer to the version control system.
@@ -307,8 +293,8 @@ class FileChecker(object):
             self._check_unittest()
         if self.corrective_actions:
             for action in self.corrective_actions:
-                answer = ask("Would you like to execute %s?" % action,
-                             auto=self.confirm)
+                answer = utils.ask("Would you like to execute %s?" % action,
+                                   auto=self.confirm)
                 if answer == "y":
                     rc = utils.system(action, ignore_status=True)
                     if rc != 0:
@@ -334,7 +320,7 @@ class PatchChecker(object):
         if changed_files_before:
             logging.error("Repository has changed files prior to patch "
                           "application. ")
-            answer = ask("Would you like to revert them?", auto=self.confirm)
+            answer = utils.ask("Would you like to revert them?", auto=self.confirm)
             if answer == "n":
                 logging.error("Not safe to proceed without reverting files.")
                 sys.exit(1)
@@ -382,7 +368,7 @@ class PatchChecker(object):
             for untracked_file in add_to_vcs:
                 logging.info(untracked_file)
             logging.info("Might need to be added to VCS")
-            answer = ask("Would you like to add them to VCS ?")
+            answer = utils.ask("Would you like to add them to VCS ?")
             if answer == "y":
                 for untracked_file in add_to_vcs:
                     self.vcs.add_untracked_file(untracked_file)
Signature · 1 line
1.7.5
#1 lore raw

PATCH 2/2 KVM test: Improve get_started.py script

* Space steps so it's easier to spot the blocks of info
* Use ask to make the questions look better
* Give the user the chance to not check SHA1 sum of the isos
* Print download URLs if asked to download
* Print CD SHA1 sum hashes

You can see how a typical execution looks like here:

http://www.linux-kvm.org/page/KVM-Autotest/Client_Install#For_the_impatient

---
 client/tests/kvm/get_started.py |   62 ++++++++++++++++++++++++++++++---------
 1 files changed, 48 insertions(+), 14 deletions(-)
client/tests/kvm/get_started.py +48 -14
index c0cbdb7..c986f5e 100755
@@ -23,21 +23,43 @@ def check_iso(url, destination, hash):
     @param destination: Directory in local disk where we'd like the iso to be.
     @param hash: SHA1 hash for the ISO image.
     """
-    logging.info("Verifying iso %s", os.path.basename(url))
+    file_ok = False
     if not destination:
         os.makedirs(destination)
     iso_path = os.path.join(destination, os.path.basename(url))
-    if not os.path.isfile(iso_path) or (
-                            utils.hash_file(iso_path, method="sha1") != hash):
-        logging.warning("%s not found or corrupted", iso_path)
-        logging.warning("Would you like to download it? (y/n)")
-        iso_download = raw_input()
-        if iso_download == 'y':
-            utils.unmap_url_cache(destination, url, hash, method="sha1")
+    if not os.path.isfile(iso_path):
+        logging.warning("File %s not found", iso_path)
+        logging.warning("Expected SHA1 sum: %s", hash)
+        answer = utils.ask("Would you like to download it from %s?" % url)
+        if answer == 'y':
+            try:
+                utils.unmap_url_cache(destination, url, hash, method="sha1")
+                file_ok = True
+            except EnvironmentError, e:
+                logging.error(e)
         else:
-            logging.warning("Missing file %s. Please download it", iso_path)
+            logging.warning("Missing file %s", iso_path)
+            logging.warning("Please download it or put an exsiting copy on the "
+                            "appropriate location")
+            return
     else:
-        logging.debug("%s present, with proper checksum", iso_path)
+        logging.info("Found %s", iso_path)
+        logging.info("Expected SHA1 sum: %s", hash)
+        answer = utils.ask("Would you like to check %s? It might take a while" %
+                           iso_path)
+        if answer == 'y':
+            try:
+                utils.unmap_url_cache(destination, url, hash, method="sha1")
+                file_ok = True
+            except EnvironmentError, e:
+                logging.error(e)
+        else:
+            logging.info("File %s present, but chose to not verify it",
+                         iso_path)
+            return
+
+    if file_ok:
+        logging.info("%s present, with proper checksum", iso_path)
 
 
 if __name__ == "__main__":
@@ -45,6 +67,7 @@ if __name__ == "__main__":
                                       verbose=True)
     logging.info("KVM test config helper")
 
+    logging.info("")
     logging.info("1 - Verifying directories (check if the directory structure "
                  "expected by the default test config is there)")
     base_dir = "/tmp/kvm_autotest_root"
@@ -57,14 +80,14 @@ if __name__ == "__main__":
         else:
             logging.debug("Dir %s exists, not creating" %
                           sub_dir_path)
-    logging.info("Do you want to setup NFS mounts for some of those "
-                 "dirs? (y/n)")
-    setup_nfs = raw_input()
-    if setup_nfs == 'y':
+    answer = utils.ask("Do you want to setup NFS mounts for some of those "
+                       "dirs?")
+    if answer == 'y':
         logging.info("Exiting the script so you can setup the NFS mounts. "
                      "When you are done, re-run this script.")
         sys.exit(0)
 
+    logging.info("")
     logging.info("2 - Creating config files from samples (copy the default "
                  "config samples to actual config files)")
     kvm_test_dir = os.path.dirname(sys.modules[__name__].__file__)
@@ -80,6 +103,7 @@ if __name__ == "__main__":
         else:
             logging.debug("Config file %s exists, not touching" % dst_file)
 
+    logging.info("")
     logging.info("3 - Verifying iso (make sure we have the OS ISO needed for "
                  "the default test set)")
 
@@ -89,8 +113,10 @@ if __name__ == "__main__":
                        iso_name)
     hash = "38a4078011bac74493db7ecc53c9d9fbc96dbbd5"
     destination = os.path.join(base_dir, 'isos', 'linux')
+    path = os.path.join(destination, iso_name)
     check_iso(url, destination, hash)
 
+    logging.info("")
     logging.info("4 - Verifying winutils.iso (make sure we have the utility "
                  "ISO needed for Windows testing)")
 
@@ -100,8 +126,10 @@ if __name__ == "__main__":
     url = "http://people.redhat.com/mrodrigu/kvm/winutils.iso"
     hash = "02930224756510e383c44c49bffb760e35d6f892"
     destination = os.path.join(base_dir, 'isos', 'windows')
+    path = os.path.join(destination, iso_name)
     check_iso(url, destination, hash)
 
+    logging.info("")
     logging.info("5 - Checking if qemu is installed (certify qemu and qemu-kvm "
                  "are in the place the default config expects)")
     qemu_default_paths = ['/usr/bin/qemu-kvm', '/usr/bin/qemu-img']
@@ -111,7 +139,10 @@ if __name__ == "__main__":
                             qemu_path)
         else:
             logging.debug("%s present", qemu_path)
+    logging.info("If you wish to change qemu-kvm to qemu or other binary path, "
+                 "you will have to modify tests.cfg")
 
+    logging.info("")
     logging.info("6 - Checking for the KVM module (make sure kvm is loaded "
                  "to accelerate qemu-kvm)")
     if not utils.module_is_loaded("kvm"):
@@ -119,6 +150,7 @@ if __name__ == "__main__":
     else:
         logging.debug("KVM module loaded")
 
+    logging.info("")
     logging.info("7 - Verify needed packages to get started")
     logging.info("Please take a look at the online documentation "
                  "http://www.linux-kvm.org/page/KVM-Autotest/Client_Install "
@@ -127,6 +159,8 @@ if __name__ == "__main__":
     client_dir = os.path.abspath(os.path.join(kvm_test_dir, "..", ".."))
     autotest_bin = os.path.join(client_dir, 'bin', 'autotest')
     control_file = os.path.join(kvm_test_dir, 'control')
+
+    logging.info("")
     logging.info("When you are done fixing eventual warnings found, "
                  "you can run the kvm test using the command line AS ROOT:")
     logging.info("%s --verbose %s", autotest_bin, control_file)
Signature · 1 line
1.7.5