From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id D71001381FA for ; Sat, 29 Dec 2012 07:46:05 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id EE6DBE06B4; Sat, 29 Dec 2012 07:45:49 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 47B2AE06B3 for ; Sat, 29 Dec 2012 07:45:49 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 3057333D790 for ; Sat, 29 Dec 2012 07:45:48 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 55543E543D for ; Sat, 29 Dec 2012 07:45:46 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1356766207.c7b3b1fbffb23f30c07fe57946221b62c54c1af9.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/util/_async/ X-VCS-Repository: proj/portage X-VCS-Files: pym/portage/util/_async/run_main_scheduler.py X-VCS-Directories: pym/portage/util/_async/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: c7b3b1fbffb23f30c07fe57946221b62c54c1af9 X-VCS-Branch: master Date: Sat, 29 Dec 2012 07:45:46 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: bfee2efe-a87a-431c-a606-0051c0042422 X-Archives-Hash: edd571a320aee8066968e58c2ad4092a commit: c7b3b1fbffb23f30c07fe57946221b62c54c1af9 Author: Zac Medico gentoo org> AuthorDate: Sat Dec 29 07:18:47 2012 +0000 Commit: Zac Medico gentoo org> CommitDate: Sat Dec 29 07:30:07 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=c7b3b1fb Add run_main_scheduler helper function. --- pym/portage/util/_async/run_main_scheduler.py | 41 +++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 deletions(-) diff --git a/pym/portage/util/_async/run_main_scheduler.py b/pym/portage/util/_async/run_main_scheduler.py new file mode 100644 index 0000000..10fed34 --- /dev/null +++ b/pym/portage/util/_async/run_main_scheduler.py @@ -0,0 +1,41 @@ + +import signal + +def run_main_scheduler(scheduler): + """ + Start and run an AsyncScheduler (or compatible object), and handle + SIGINT or SIGTERM by calling its terminate() method and waiting + for it to clean up after itself. If SIGINT or SIGTERM is received, + return signum, else return None. Any previous SIGINT or SIGTERM + signal handlers are automatically saved and restored before + returning. + """ + + received_signal = [] + + def sighandler(signum, frame): + signal.signal(signal.SIGINT, signal.SIG_IGN) + signal.signal(signal.SIGTERM, signal.SIG_IGN) + received_signal.append(signum) + scheduler.terminate() + + earlier_sigint_handler = signal.signal(signal.SIGINT, sighandler) + earlier_sigterm_handler = signal.signal(signal.SIGTERM, sighandler) + + try: + scheduler.start() + scheduler.wait() + finally: + # Restore previous handlers + if earlier_sigint_handler is not None: + signal.signal(signal.SIGINT, earlier_sigint_handler) + else: + signal.signal(signal.SIGINT, signal.SIG_DFL) + if earlier_sigterm_handler is not None: + signal.signal(signal.SIGTERM, earlier_sigterm_handler) + else: + signal.signal(signal.SIGTERM, signal.SIG_DFL) + + if received_signal: + return received_signal[0] + return None