AsynchronousTask: add _cancel template method
authorZac Medico <zmedico@gentoo.org>
Thu, 17 Mar 2011 05:36:33 +0000 (22:36 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 17 Mar 2011 05:36:33 +0000 (22:36 -0700)
pym/_emerge/AsynchronousLock.py
pym/_emerge/AsynchronousTask.py
pym/_emerge/CompositeTask.py
pym/_emerge/FifoIpcDaemon.py
pym/_emerge/PipeReader.py
pym/_emerge/SubProcess.py
pym/_emerge/TaskSequence.py

index c47a207887ba7e5c6bbc351bc25b155257bdc146..6e0a1ac2aa95ff4ecf51aa614130b1bd3cca9671 100644 (file)
@@ -198,6 +198,10 @@ class _LockProcess(AbstractPollTask):
                        raise AssertionError('lock process failed with returncode %s' \
                                % (proc.returncode,))
 
+       def _cancel(self):
+               if self._proc is not None:
+                       self._proc.cancel()
+
        def _wait(self):
                if self.returncode is not None:
                        return self.returncode
index 1918b839bfa14812d605d9515097dec540546ba1..de0080042a9f19bd424b3886267807e3e082d308 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright 1999-2009 Gentoo Foundation
+# Copyright 1999-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from _emerge.SlotObject import SlotObject
@@ -45,8 +45,17 @@ class AsynchronousTask(SlotObject):
                return self.returncode
 
        def cancel(self):
-               self.cancelled = True
-               self.wait()
+               if not self.cancelled:
+                       self.cancelled = True
+                       self._cancel()
+                       self.wait()
+
+       def _cancel(self):
+               """
+               Subclasses should implement this, as a template method
+               to be called by AsynchronousTask.cancel().
+               """
+               pass
 
        def addStartListener(self, f):
                """
index 8e8f0843c8522e118352e968484e310eb08f3a91..edc0768d73c1f144167b7f656ce32f243d20bf12 100644 (file)
@@ -13,15 +13,13 @@ class CompositeTask(AsynchronousTask):
        def isAlive(self):
                return self._current_task is not None
 
-       def cancel(self):
-               self.cancelled = True
+       def _cancel(self):
                if self._current_task is not None:
                        if self._current_task is self._TASK_QUEUED:
                                self.returncode = 1
                                self._current_task = None
                        else:
                                self._current_task.cancel()
-               AsynchronousTask.cancel(self)
 
        def _poll(self):
                """
index c31508a839cbec66e80024d46752516ac0cd09ac..a716dac7c11ef0e0e6cb5940b79a354cd68679d4 100644 (file)
@@ -43,12 +43,10 @@ class FifoIpcDaemon(AbstractPollTask):
        def isAlive(self):
                return self._registered
 
-       def cancel(self):
+       def _cancel(self):
                if self.returncode is None:
                        self.returncode = 1
-                       self.cancelled = True
                self._unregister()
-               AbstractPollTask.cancel(self)
 
        def _wait(self):
                if self.returncode is not None:
index 061921a791ec6947d45e87ebcfbdf03882dd5e35..375c98f6ad1c0eb19bd9562ccfba2cff3a21e645 100644 (file)
@@ -32,11 +32,9 @@ class PipeReader(AbstractPollTask):
        def isAlive(self):
                return self._registered
 
-       def cancel(self):
+       def _cancel(self):
                if self.returncode is None:
                        self.returncode = 1
-                       self.cancelled = True
-               AbstractPollTask.cancel(self)
 
        def _wait(self):
                if self.returncode is not None:
index 16c71fe3b45f2b19bd51bf6b4cf8de7adef95914..115af803d186661fa2af23ff354434450c6194e1 100644 (file)
@@ -41,15 +41,13 @@ class SubProcess(AbstractPollTask):
                self.wait()
                return self.returncode
 
-       def cancel(self):
+       def _cancel(self):
                if self.isAlive():
                        try:
                                os.kill(self.pid, signal.SIGTERM)
                        except OSError as e:
                                if e.errno != errno.ESRCH:
                                        raise
-                               del e
-               AbstractPollTask.cancel(self)
 
        def isAlive(self):
                return self.pid is not None and \
index 7a1bd5a478974381dbcf3e22bcbe09374b41b242..1233c99d3661f56107bc1e72de19720d2524e6ed 100644 (file)
@@ -25,9 +25,8 @@ class TaskSequence(CompositeTask):
        def _start(self):
                self._start_next_task()
 
-       def cancel(self):
+       def _cancel(self):
                self._task_queue.clear()
-               CompositeTask.cancel(self)
 
        def _start_next_task(self):
                self._start_task(self._task_queue.popleft(),