-
Notifications
You must be signed in to change notification settings - Fork 3
/
watchdir
executable file
·43 lines (31 loc) · 1.21 KB
/
watchdir
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
#!/usr/bin/env python
import argparse
import logging
import os
import inotify.adapters
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='watchdir')
parser.add_argument('dir', help='directory to watch')
parser.add_argument('-o', '--output', help='filename of output log')
args = parser.parse_args()
notifier = inotify.adapters.Inotify()
notifier.add_watch(args.dir)
logger = logging.getLogger('watchdir')
logger.setLevel(logging.DEBUG)
if args.output is None:
handler = logging.StreamHandler()
else:
handler = logging.FileHandler(filename=args.output)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s: %(message)s',
'%Y%m%d.%H%M%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
for event in notifier.event_gen():
if event is not None:
#print(event) # uncomment to see all events generated
for e in event[1]:
logger.info(f'{e}: {event[3]}')
#print(f'{e}: {event[2]}')
#if 'IN_MOVED_TO' in event[1]:
# print("file '{0}' moved to '{1}'".format(event[3], event[2]))