From 73b0ce29085cca62b0d85f5d426c960130ab0bf7 Mon Sep 17 00:00:00 2001 From: Nathaniel Kenschaft Date: Fri, 12 Jun 2020 19:45:00 -0400 Subject: [PATCH] implemented pyinotify module in bg service --- BackgroundService/bgservice.py | 44 ++++++++++++++------------ BackgroundService/event_processor.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 BackgroundService/event_processor.py diff --git a/BackgroundService/bgservice.py b/BackgroundService/bgservice.py index a92ae9a..3ad361f 100644 --- a/BackgroundService/bgservice.py +++ b/BackgroundService/bgservice.py @@ -1,26 +1,30 @@ -import os, sys +import os +import sys +import signal import time -import pyinotify - -start_time = None -edit_times = [] -end_time = None +import event_processor -def write_pid_file(): - pid = str(os.getpid()) - f = open('/tmp/skoolos_work_logger', 'w') - f.write(pid) - f.close() - - -def readable_time(input_time): - return time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime(input_time)) - - -def start_service(dir_to_watch): - start_time = time.time() - +class SkoolOSDaemmon: + """Constructor""" + def __init__(self, work_dir): + self.work_dir = work_dir + self.start_time = None + self.end_time = None + """Stores the pid of the program to be terminated externally""" + def write_pid_file(self): + pid = str(os.getpid()) + file_ = open('/tmp/skoolosdaemonpid', 'w') + file_.write(pid) + file_.close() + def readable_time(self, input_time): + return time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime(input_time)) + def start_service(self): + start_time = time.time() + log_file = open('/tmp/skooloslog-' + start_time, 'w') + log_file.write("Started work: " + self.readable_time(start_time)) + sys.stdout = log_file + event_processor.watch_dir(self.work_dir) def Main(): diff --git a/BackgroundService/event_processor.py b/BackgroundService/event_processor.py new file mode 100644 index 0000000..e2b3f5b --- /dev/null +++ b/BackgroundService/event_processor.py @@ -0,0 +1,46 @@ +import time +import pyinotify + + +def readable_time(input_time): + return time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime(input_time)) + + +class EventProcessor(pyinotify.ProcessEvent): + _methods = ["IN_ACCESS", + "IN_CREATE", + "IN_CLOSE_WRITE", + "IN_DELETE" + "IN_MOVED_TO", + "IN_MOVED_FROM", + ] + + +def __method_format(method): + return { + "IN_ACCESS":"Accessed a file", + "IN_CREATE":"Created a file", + "IN_CLOSE_WRITE":"Wrote to a file", + "IN_DELETE":"Deleted a file", + "IN_MOVED_TO":"Moved a file or directory in from elsewhere", + "IN_MOVED_FROM":"Moved a file or directory elsewhere", + }[method] + + +def __process_generator(cls, method): + def _method_name(self, event): + print("Method name: {} ()\n" + "Path name: {}\n" + "Event Name: {}\n" + "Timestamp: {}\n".format(__method_format(method), event.pathname, event.maskname, readable_time(time.time()))) + _method_name.__name__ = "process_{}".format(method) + setattr(cls, _method_name.__name__, _method_name) + + +def watch_dir(dir_to_watch): + 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()