-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmon.py
More file actions
executable file
·63 lines (48 loc) · 1.86 KB
/
mon.py
File metadata and controls
executable file
·63 lines (48 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyLoggingEventHandler(FileSystemEventHandler):
"""Logs all the events captured."""
def __init__(self, logger=None):
super().__init__()
self.events = {}
self.logger = logger or logging.root
def on_moved(self, event):
super().on_moved(event)
what = 'directory' if event.is_directory else 'file'
self.logger.info("Moved %s: from %s to %s", what, event.src_path,
event.dest_path)
def on_created(self, event):
super().on_created(event)
what = 'directory' if event.is_directory else 'file'
self.events[event.src_path] = "created"
self.logger.info("Created %s: %s", what, event.src_path)
def on_deleted(self, event):
super().on_deleted(event)
what = 'directory' if event.is_directory else 'file'
self.events[event.src_path] = "deleted"
self.logger.info("Deleted %s: %s", what, event.src_path)
def on_modified(self, event):
super().on_modified(event)
what = 'directory' if event.is_directory else 'file'
self.events[event.src_path] = "modified"
self.logger.info("Modified %s: %s", what, event.src_path)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = MyLoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
print(event_handler.events)