From 58321dbf55802492d3ab087f9f119d26c59da495 Mon Sep 17 00:00:00 2001 From: Marina Golosova Date: Fri, 27 Oct 2017 14:38:50 +0000 Subject: [PATCH] pyDKB/cds: add custom signal handlers for SIGINT and SIGTERM Another try to deal with Issue #45; doesn`t seem to work, though. --- Utils/Dataflow/pyDKB/dataflow/cds.py | 29 ++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/Utils/Dataflow/pyDKB/dataflow/cds.py b/Utils/Dataflow/pyDKB/dataflow/cds.py index 38d23ab43..fb6a798bb 100644 --- a/Utils/Dataflow/pyDKB/dataflow/cds.py +++ b/Utils/Dataflow/pyDKB/dataflow/cds.py @@ -6,6 +6,7 @@ import sys import signal +import os try: import kerberos @@ -22,6 +23,19 @@ class CDSInvenioConnector(cds.CDSInvenioConnector): """ CDSInvenioConnector which closes the browser in most cases. """ + + orig_handlers = {} + handlers = False + + def __init__(self, *args): + self.orig_handlers = { + signal.SIGINT: signal.signal(signal.SIGINT, self.kill), + signal.SIGTERM: signal.signal(signal.SIGTERM, self.kill) + } + handlers = True + super(CDSInvenioConnector, self).__init__(*args) + + def __enter__(self): """ Enter the with...as construction. """ return self @@ -34,13 +48,24 @@ def __exit__(self, exc_type, exc_val, exc_tb): return True def __del__(self): - self.delete() + self.delete(False) - def delete(self): + def delete(self, restore_handlers=True): if getattr(self, 'browser', None): self.browser.driver.service.process.send_signal(signal.SIGTERM) self.browser.quit() del self.browser + if restore_handlers: + for s in self.orig_handlers: + signal.signal(s, self.orig_handlers[s]) + + + def kill(self, signum, frame): + """ Run del and propagate signal. """ + self.delete() + os.kill(os.getpid(), signum) + + class KerberizedCDSInvenioConnector(CDSInvenioConnector): """