add errors module (sys.excepthook), made win32 filemon use it

This commit is contained in:
Lance Edgar 2012-08-10 22:44:43 -07:00
parent b6d48d692f
commit ec71a8463c
2 changed files with 105 additions and 4 deletions

View file

@ -27,16 +27,17 @@
"""
import os.path
import sys
import socket
import time
import Queue
from traceback import format_exception
import edbob
from edbob.filemon import MonitorProfile
from edbob.filemon.win32 import WatcherWin32, ACTION_CREATE, ACTION_UPDATE
from edbob.win32 import file_is_free
import sys
if sys.platform == 'win32': # docs should build for everyone
import win32serviceutil
import win32service
@ -94,8 +95,9 @@ class FileMonitorService(win32serviceutil.ServiceFramework):
except Queue.Empty:
pass
else:
if ftype == 'file' and action in (
ACTION_CREATE, ACTION_UPDATE):
# if ftype == 'file' and action in (
# ACTION_CREATE, ACTION_UPDATE):
if ftype == 'file' and action == ACTION_CREATE:
self.do_actions(key, fpath)
win32api.SleepEx(250, True)
@ -118,7 +120,34 @@ class FileMonitorService(win32serviceutil.ServiceFramework):
func = action
args = []
func = edbob.load_spec(func)
func(path, *args)
try:
func(path, *args)
except:
exc_info = sys.exc_info()
# Call the system exception hook in case anything special has
# been registered there, e.g. if edbob.errors.init() has
# happened. Note that this is especially necessary since
# PythonService.exe doesn't seem to honor sys.excepthook.
sys.excepthook(*exc_info)
# Go ahead and write exception info to the Windows Event Log
# while we're at it.
msg = "File monitor action failed.\n"
msg += "\n"
msg += "Profile: %s\n" % key
msg += "Action: %s\n" % action
msg += "File Path: %s\n" % path
msg += "\n"
msg += ''.join(format_exception(*exc_info))
servicemanager.LogErrorMsg(msg)
# Don't re-raise the exception since the service should
# continue running despite any problems it encounters. But
# this file probably shouldn't be processed any further.
break
if __name__ == '__main__':