added possible daemon usage

This commit is contained in:
Nathaniel Kenschaft 2020-06-13 22:53:20 -04:00
parent a032cf12ad
commit 01e71853bf
2 changed files with 9 additions and 7 deletions

View File

@ -7,8 +7,9 @@ import event_processor
class SkoolOSDaemon: class SkoolOSDaemon:
"""Constructor""" """Constructor"""
def __init__(self, work_dir): def __init__(self, work_dir, daemonize):
self.work_dir = work_dir self.work_dir = work_dir
self.daemonize = daemonize
self.start_time = None self.start_time = None
self.end_time = None self.end_time = None
self.log_file = None self.log_file = None
@ -29,9 +30,9 @@ class SkoolOSDaemon:
self.__write_pid_file() self.__write_pid_file()
self.start_time = time.time() self.start_time = time.time()
self.log_file = open('/tmp/skooloslogs/' + str(self.start_time), 'w') self.log_file = open('/tmp/skooloslogs/' + str(self.start_time), 'w')
self.log_file.write("Start time: " + self.readable_time(self.start_time) + "\n") self.log_file.write("Start time: \n" + self.readable_time(self.start_time) + "\n\n")
sys.stdout = self.log_file sys.stdout = self.log_file
event_processor.watch_dir(self.work_dir) event_processor.watch_dir(self.work_dir, self.daemonize)
def stop(self): def stop(self):
event_processor.stop_watching() event_processor.stop_watching()
self.end_time = time.time() self.end_time = time.time()
@ -48,6 +49,7 @@ def Main():
def signal_handler(signum, frame): def signal_handler(signum, frame):
logger.stop() logger.stop()
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
global logger global logger
logger = SkoolOSDaemon("/tmp") logger = SkoolOSDaemon("/tmp")
logger.start() logger.start()

View File

@ -7,7 +7,7 @@ def readable_time(input_time):
class EventProcessor(pyinotify.ProcessEvent): class EventProcessor(pyinotify.ProcessEvent):
_methods = ["IN_ACCESS", _methods = ["IN_OPEN",
"IN_CREATE", "IN_CREATE",
"IN_CLOSE_WRITE", "IN_CLOSE_WRITE",
"IN_DELETE", "IN_DELETE",
@ -18,7 +18,7 @@ class EventProcessor(pyinotify.ProcessEvent):
def __method_format(method): def __method_format(method):
return { return {
"IN_ACCESS":"Accessed a file", "IN_OPEN":"Accessed a file",
"IN_CREATE":"Created a file", "IN_CREATE":"Created a file",
"IN_CLOSE_WRITE":"Wrote to a file", "IN_CLOSE_WRITE":"Wrote to a file",
"IN_DELETE":"Deleted a file", "IN_DELETE":"Deleted a file",
@ -51,14 +51,14 @@ def __process_generator(cls, method):
EVENT_NOTIFIER = None EVENT_NOTIFIER = None
def watch_dir(dir_to_watch): def watch_dir(dir_to_watch, daemonize=False):
global EVENT_NOTIFIER global EVENT_NOTIFIER
for method in EventProcessor._methods: for method in EventProcessor._methods:
__process_generator(EventProcessor, method) __process_generator(EventProcessor, method)
watch_manager = pyinotify.WatchManager() watch_manager = pyinotify.WatchManager()
EVENT_NOTIFIER = pyinotify.Notifier(watch_manager, EventProcessor()) EVENT_NOTIFIER = pyinotify.Notifier(watch_manager, EventProcessor())
watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS) watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS)
EVENT_NOTIFIER.loop()#daemonize=True) EVENT_NOTIFIER.loop(daemonize=daemonize)
def stop_watching(): def stop_watching():