[Autotest][PATCH 1/5] Autotest: Add utils for OpenVSwitch patch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ForAllxx:
  run object method on every object in list

  ForAll[a,b,c].print()

Signed-off-by: Jiří Župka <jzupka@xxxxxxxxxx>
---
 client/shared/base_utils.py |   81 +++++++++++++++++++++++++++++++++++-------
 1 files changed, 67 insertions(+), 14 deletions(-)

diff --git a/client/shared/base_utils.py b/client/shared/base_utils.py
index 0734742..573b907 100644
--- a/client/shared/base_utils.py
+++ b/client/shared/base_utils.py
@@ -1224,6 +1224,56 @@ def system_output_parallel(commands, timeout=None, ignore_status=False,
     return out
 
 
+class ForAll(list):
+    def __getattr__(self, name):
+        def wrapper(*args, **kargs):
+            return map(lambda o: o.__getattribute__(name)(*args, **kargs), self)
+        return wrapper
+
+
+class ForAllP(list):
+    """
+    Parallel version of ForAll
+    """
+    def __getattr__(self, name):
+        def wrapper(*args, **kargs):
+            threads = []
+            for o in self:
+                threads.append(InterruptedThread(o.__getattribute__(name),
+                                        args=args, kwargs=kargs))
+            for t in threads:
+                t.start()
+            return map(lambda t: t.join(), threads)
+        return wrapper
+
+
+class ForAllPSE(list):
+    """
+    Parallel version of and suppress exception.
+    """
+    def __getattr__(self, name):
+        def wrapper(*args, **kargs):
+            threads = []
+            for o in self:
+                threads.append(InterruptedThread(o.__getattribute__(name),
+                                        args=args, kwargs=kargs))
+            for t in threads:
+                t.start()
+
+            result = []
+            for t in threads:
+                ret = {}
+                try:
+                    ret["return"] = t.join()
+                except Exception:
+                    ret["exception"] = sys.exc_info()
+                    ret["args"] = args
+                    ret["kargs"] = kargs
+                result.append(ret)
+            return result
+        return wrapper
+
+
 def etraceback(prep, exc_info):
     """
     Enhanced Traceback formats traceback into lines "prep: line\nname: line"
@@ -1733,9 +1783,12 @@ def import_site_function(path, module, funcname, dummy, modulefile=None):
     return import_site_symbol(path, module, funcname, dummy, modulefile)
 
 
-def _get_pid_path(program_name):
-    pid_files_dir = GLOBAL_CONFIG.get_config_value("SERVER", 'pid_files_dir',
-                                                   default="")
+def get_pid_path(program_name, pid_files_dir=None):
+    if pid_files_dir is None:
+        pid_files_dir = GLOBAL_CONFIG.get_config_value("SERVER",
+                                                       'pid_files_dir',
+                                                       default="")
+
     if not pid_files_dir:
         base_dir = os.path.dirname(__file__)
         pid_path = os.path.abspath(os.path.join(base_dir, "..", "..",
@@ -1746,25 +1799,25 @@ def _get_pid_path(program_name):
     return pid_path
 
 
-def write_pid(program_name):
+def write_pid(program_name, pid_files_dir=None):
     """
     Try to drop <program_name>.pid in the main autotest directory.
 
     Args:
       program_name: prefix for file name
     """
-    pidfile = open(_get_pid_path(program_name), "w")
+    pidfile = open(get_pid_path(program_name, pid_files_dir), "w")
     try:
         pidfile.write("%s\n" % os.getpid())
     finally:
         pidfile.close()
 
 
-def delete_pid_file_if_exists(program_name):
+def delete_pid_file_if_exists(program_name, pid_files_dir=None):
     """
     Tries to remove <program_name>.pid from the main autotest directory.
     """
-    pidfile_path = _get_pid_path(program_name)
+    pidfile_path = get_pid_path(program_name, pid_files_dir)
 
     try:
         os.remove(pidfile_path)
@@ -1774,18 +1827,18 @@ def delete_pid_file_if_exists(program_name):
         raise
 
 
-def get_pid_from_file(program_name):
+def get_pid_from_file(program_name, pid_files_dir=None):
     """
     Reads the pid from <program_name>.pid in the autotest directory.
 
     @param program_name the name of the program
     @return the pid if the file exists, None otherwise.
     """
-    pidfile_path = _get_pid_path(program_name)
+    pidfile_path = get_pid_path(program_name, pid_files_dir)
     if not os.path.exists(pidfile_path):
         return None
 
-    pidfile = open(_get_pid_path(program_name), 'r')
+    pidfile = open(get_pid_path(program_name, pid_files_dir), 'r')
 
     try:
         try:
@@ -1808,27 +1861,27 @@ def get_process_name(pid):
     return get_field(read_file("/proc/%d/stat" % pid), 1)[1:-1]
 
 
-def program_is_alive(program_name):
+def program_is_alive(program_name, pid_files_dir=None):
     """
     Checks if the process is alive and not in Zombie state.
 
     @param program_name the name of the program
     @return True if still alive, False otherwise
     """
-    pid = get_pid_from_file(program_name)
+    pid = get_pid_from_file(program_name, pid_files_dir)
     if pid is None:
         return False
     return pid_is_alive(pid)
 
 
-def signal_program(program_name, sig=signal.SIGTERM):
+def signal_program(program_name, sig=signal.SIGTERM, pid_files_dir=None):
     """
     Sends a signal to the process listed in <program_name>.pid
 
     @param program_name the name of the program
     @param sig signal to send
     """
-    pid = get_pid_from_file(program_name)
+    pid = get_pid_from_file(program_name, pid_files_dir)
     if pid:
         signal_pid(pid, sig)
 
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux