From fa2e3f6275587c32e51ebb2d28ad60e9f3ffad3b Mon Sep 17 00:00:00 2001 From: Nathaniel Kenschaft Date: Sun, 14 Jun 2020 14:29:27 -0400 Subject: [PATCH] began to implement background service with ThreadedNotifier class --- BackgroundService/bgservice.py | 7 +++---- BackgroundService/event_processor.py | 16 ++++++++-------- BackgroundService/test.py | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 BackgroundService/test.py diff --git a/BackgroundService/bgservice.py b/BackgroundService/bgservice.py index 61e7d15..ed26c76 100644 --- a/BackgroundService/bgservice.py +++ b/BackgroundService/bgservice.py @@ -7,9 +7,8 @@ import event_processor class SkoolOSDaemon: """Constructor""" - def __init__(self, work_dir, daemonize): + def __init__(self, work_dir='/tmp'): self.work_dir = work_dir - self.daemonize = daemonize self.start_time = None self.end_time = None self.log_file = None @@ -32,7 +31,7 @@ class SkoolOSDaemon: self.log_file = open('/tmp/skooloslogs/' + str(self.start_time), 'w') self.log_file.write("Start time: \n" + self.readable_time(self.start_time) + "\n\n") sys.stdout = self.log_file - event_processor.watch_dir(self.work_dir, self.daemonize) + event_processor.watch_dir(self.work_dir) def stop(self): event_processor.stop_watching() self.end_time = time.time() @@ -49,7 +48,7 @@ def Main(): def signal_handler(signum, frame): logger.stop() signal.signal(signal.SIGINT, signal_handler) - signal.signal(signal.SIGTERM, signal_handler) + # signal.signal(signal.SIGTERM, signal_handler) global logger logger = SkoolOSDaemon("/tmp") logger.start() diff --git a/BackgroundService/event_processor.py b/BackgroundService/event_processor.py index bed7b70..55c5e15 100644 --- a/BackgroundService/event_processor.py +++ b/BackgroundService/event_processor.py @@ -18,7 +18,7 @@ class EventProcessor(pyinotify.ProcessEvent): def __method_format(method): return { - "IN_OPEN":"Accessed a file", + "IN_OPEN":"Opened a file", "IN_CREATE":"Created a file", "IN_CLOSE_WRITE":"Wrote to a file", "IN_DELETE":"Deleted a file", @@ -38,11 +38,11 @@ def __process_generator(cls, method): readable_time(time.time()) ) if "IN_DELETE" in description: - description += "Warning: Unexpected file deletion\n" + description += "WARNING: Unexpected file deletion\n" if "IN_MOVED_TO" in description: - description += "Warning: Unexpected file add to work\n" + description += "WARNING: Unexpected file add to work\n" if "IN_MOVED_FROM" in description: - description += "Warning: Unexpected file moved out of directory\n" + description += "WARNING: Unexpected file moved out of directory\n" print(description) _method_name.__name__ = "process_{}".format(method) setattr(cls, _method_name.__name__, _method_name) @@ -51,14 +51,14 @@ def __process_generator(cls, method): EVENT_NOTIFIER = None -def watch_dir(dir_to_watch, daemonize=False): +def watch_dir(dir_to_watch): global EVENT_NOTIFIER for method in EventProcessor._methods: __process_generator(EventProcessor, method) watch_manager = pyinotify.WatchManager() - EVENT_NOTIFIER = pyinotify.Notifier(watch_manager, EventProcessor()) - watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS) - EVENT_NOTIFIER.loop(daemonize=daemonize) + EVENT_NOTIFIER = pyinotify.ThreadedNotifier(watch_manager, EventProcessor()) + watch_manager.add_watch(dir_to_watch, pyinotify.ALL_EVENTS, rec=True) + EVENT_NOTIFIER.loop() def stop_watching(): diff --git a/BackgroundService/test.py b/BackgroundService/test.py new file mode 100644 index 0000000..b797228 --- /dev/null +++ b/BackgroundService/test.py @@ -0,0 +1,19 @@ +from bgservice import SkoolOSDaemon as sod +import threading + +logger = sod() + + +if __name__ == "__main__": + line=1 + print(line) + line+=1 + logger.start() + print(line) + line+=1 + input("Enter any key when you are done modifyng the /tmp/ directory") + print(line) + line+=1 + logger.stop() + print(line) + line+=1