Revision 451575de lib/utils.py

b/lib/utils.py
2068 2068
                "Failed to unlock %s" % self.filename)
2069 2069

  
2070 2070

  
2071
def SignalHandled(signums):
2072
  """Signal Handled decoration.
2073

  
2074
  This special decorator installs a signal handler and then calls the target
2075
  function. The function must accept a 'signal_handlers' keyword argument,
2076
  which will contain a dict indexed by signal number, with SignalHandler
2077
  objects as values.
2078

  
2079
  The decorator can be safely stacked with iself, to handle multiple signals
2080
  with different handlers.
2081

  
2082
  @type signums: list
2083
  @param signums: signals to intercept
2084

  
2085
  """
2086
  def wrap(fn):
2087
    def sig_function(*args, **kwargs):
2088
      assert 'signal_handlers' not in kwargs or \
2089
             kwargs['signal_handlers'] is None or \
2090
             isinstance(kwargs['signal_handlers'], dict), \
2091
             "Wrong signal_handlers parameter in original function call"
2092
      if 'signal_handlers' in kwargs and kwargs['signal_handlers'] is not None:
2093
        signal_handlers = kwargs['signal_handlers']
2094
      else:
2095
        signal_handlers = {}
2096
        kwargs['signal_handlers'] = signal_handlers
2097
      sighandler = SignalHandler(signums)
2098
      try:
2099
        for sig in signums:
2100
          signal_handlers[sig] = sighandler
2101
        return fn(*args, **kwargs)
2102
      finally:
2103
        sighandler.Reset()
2104
    return sig_function
2105
  return wrap
2106

  
2107

  
2071 2108
class SignalHandler(object):
2072 2109
  """Generic signal handler class.
2073 2110

  

Also available in: Unified diff