implemented pyinotify module in bg service

This commit is contained in:
Nathaniel Kenschaft 2020-06-12 19:45:00 -04:00
parent 989d165494
commit 73b0ce2908
2 changed files with 70 additions and 20 deletions

View File

@ -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():

View File

@ -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()