Use run_main_scheduler where possible.
authorZac Medico <zmedico@gentoo.org>
Sat, 29 Dec 2012 07:44:39 +0000 (23:44 -0800)
committerZac Medico <zmedico@gentoo.org>
Sat, 29 Dec 2012 07:44:39 +0000 (23:44 -0800)
bin/egencache
pym/_emerge/actions.py

index 79a996d7552ee811ef184a41cf19360d8db9df10..110c47dcdcfed02d40d2763f5177d5b2e425ee14 100755 (executable)
@@ -50,6 +50,7 @@ from portage.cache.cache_errors import CacheError, StatCollision
 from portage.manifest import guessManifestFileType
 from portage.package.ebuild._parallel_manifest.ManifestScheduler import ManifestScheduler
 from portage.util import cmp_sort_key, writemsg_level
+from portage.util._async.run_main_scheduler import run_main_scheduler
 from portage.util._eventloop.global_event_loop import global_event_loop
 from portage import cpv_getkey
 from portage.dep import Atom, isjustname
@@ -357,34 +358,9 @@ class GenCache(object):
                                        level=logging.ERROR, noiselevel=-1)
 
        def run(self):
-
-               received_signal = []
-
-               def sighandler(signum, frame):
-                       signal.signal(signal.SIGINT, signal.SIG_IGN)
-                       signal.signal(signal.SIGTERM, signal.SIG_IGN)
-                       self._regen.terminate()
-                       received_signal.append(128 + signum)
-
-               earlier_sigint_handler = signal.signal(signal.SIGINT, sighandler)
-               earlier_sigterm_handler = signal.signal(signal.SIGTERM, sighandler)
-
-               try:
-                       self._regen.start()
-                       self._regen.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:
-                       sys.exit(received_signal[0])
+               signum = run_main_scheduler(self._regen)
+               if signum is not None:
+                       sys.exit(128 + signum)
 
                self.returncode |= self._regen.returncode
 
@@ -1058,33 +1034,9 @@ def egencache_main(args):
                        max_load=options.load_average,
                        event_loop=event_loop)
 
-               received_signal = []
-
-               def sighandler(signum, frame):
-                       signal.signal(signal.SIGINT, signal.SIG_IGN)
-                       signal.signal(signal.SIGTERM, signal.SIG_IGN)
-                       received_signal.append(128 + 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:
-                       sys.exit(received_signal[0])
+               signum = run_main_scheduler(scheduler)
+               if signum is not None:
+                       sys.exit(128 + signum)
 
                if options.tolerant:
                        ret.append(os.EX_OK)
index 982197b70afb5f1b0bed45d1be196a1ae05bae8d..a498ae49da25eb0b5293ee4114457135e4749638 100644 (file)
@@ -55,6 +55,7 @@ from portage._sets.base import InternalPackageSet
 from portage.util import cmp_sort_key, writemsg, varexpand, \
        writemsg_level, writemsg_stdout
 from portage.util.digraph import digraph
+from portage.util._async.run_main_scheduler import run_main_scheduler
 from portage.util._async.SchedulerInterface import SchedulerInterface
 from portage.util._eventloop.global_event_loop import global_event_loop
 from portage._global_updates import _global_updates
@@ -1951,35 +1952,10 @@ def action_regen(settings, portdb, max_jobs, max_load):
 
        regen = MetadataRegen(portdb, max_jobs=max_jobs,
                max_load=max_load, main=True)
-       received_signal = []
 
-       def emergeexitsig(signum, frame):
-               signal.signal(signal.SIGINT, signal.SIG_IGN)
-               signal.signal(signal.SIGTERM, signal.SIG_IGN)
-               portage.util.writemsg("\n\nExiting on signal %(signal)s\n" % \
-                       {"signal":signum})
-               regen.terminate()
-               received_signal.append(128 + signum)
-
-       earlier_sigint_handler = signal.signal(signal.SIGINT, emergeexitsig)
-       earlier_sigterm_handler = signal.signal(signal.SIGTERM, emergeexitsig)
-
-       try:
-               regen.start()
-               regen.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:
-               sys.exit(received_signal[0])
+       signum = run_main_scheduler(regen)
+       if signum is not None:
+               sys.exit(128 + signum)
 
        portage.writemsg_stdout("done!\n")
        return regen.returncode