From patchwork Fri Sep 21 09:31:24 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jiri Zupka X-Patchwork-Id: 1490781 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 2F86E40113 for ; Fri, 21 Sep 2012 09:32:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932855Ab2IUJcB (ORCPT ); Fri, 21 Sep 2012 05:32:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52790 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932609Ab2IUJb7 (ORCPT ); Fri, 21 Sep 2012 05:31:59 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q8L9VbUC032312 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 21 Sep 2012 05:31:38 -0400 Received: from jzupka-pc.local.com (dhcp-27-230.brq.redhat.com [10.34.27.230]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q8L9VYhq005668; Fri, 21 Sep 2012 05:31:36 -0400 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=BDupka?= To: autotest@test.kernel.org, kvm@vger.kernel.org, kvm-autotest@redhat.com, lmr@redhat.com, ldoktor@redhat.com, jzupka@redhat.com Subject: [Autotest][PATCH 1/5] Autotest: Add utils for OpenVSwitch patch Date: Fri, 21 Sep 2012 11:31:24 +0200 Message-Id: <1348219888-23436-2-git-send-email-jzupka@redhat.com> In-Reply-To: <1348219888-23436-1-git-send-email-jzupka@redhat.com> References: <1348219888-23436-1-git-send-email-jzupka@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org ForAllxx: run object method on every object in list ForAll[a,b,c].print() Signed-off-by: Ji?í Župka --- 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 .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 .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 .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 .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)