0%

pyinotify testing result

Previously I was using pyinotify to do some event notify jobs on local files.

This blog(with the testing script attached) is simply a summary which lists the events type and happening orders when doing different operations.

Script

Downlaod the test script here

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
#!/usr/bin/env python2
import subprocess
import pyinotify
import os
import shutil
mask = pyinotify.ALL_EVENTS
rootdir = '/tmp/test-pyinotify'

def _process_watcher_events(event):
print("event = %s" % event)

def _(path):
return os.path.join(rootdir, path)

def commands():
commands = [
['ls', rootdir],
['touch', _('origin.txt')],
['cp', _('origin.txt'), _('cp.txt')],
['mv', _('cp.txt'), _('mv.txt')],
['mv', _('mv.txt'), '/tmp/mv.txt'],
['rsync', '-a', '/tmp/mv.txt', _('rsync.txt')],
['scp', '/tmp/mv.txt', _('scp.txt')],
['ln', '-s', _('origin.txt'), _('ln.txt')],
['rm', _('ln.txt')],
['mkdir', _('testdir')],
['ln', _('origin.txt'), _('lnhard.txt')],
['python', '-c', "import os; f = open(os.path.join('%s', 'python.txt'), 'w'); f.write('test_python'); f.close()" % rootdir]
]
for cmd in commands:
print cmd
output = subprocess.check_output(cmd)
yield


if __name__ == "__main__":
if os.path.exists(rootdir):
shutil.rmtree(rootdir)
os.makedirs(rootdir)
watcher = pyinotify.WatchManager()
watcher.add_watch(rootdir, mask=mask, rec=True, auto_add=True, quiet=False)
notifier = pyinotify.Notifier(watcher, _process_watcher_events)
commands = commands()
while True:
notifier.process_events()
try:
next(commands)
while notifier.check_events(timeout=3):
notifier.read_events()
notifier.process_events()
print('===========================================\n')
except StopIteration:
while notifier.check_events(timeout=3):
notifier.read_events()
notifier.process_events()
print('finished')
break
shutil.rmtree(rootdir)

Summary

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
>>> python2 test_pyinotify.py

>>>
['ls', '/tmp/test']
event = <Event dir=True mask=0x40000020 maskname=IN_OPEN|IN_ISDIR name='' path=/tmp/test pathname=/tmp/test wd=1 >
event = <Event dir=True mask=0x40000001 maskname=IN_ACCESS|IN_ISDIR name='' path=/tmp/test pathname=/tmp/test wd=1 >
event = <Event dir=True mask=0x40000010 maskname=IN_CLOSE_NOWRITE|IN_ISDIR name='' path=/tmp/test pathname=/tmp/test wd=1 >
===========================================

['touch', '/tmp/test/origin.txt']
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=origin.txt path=/tmp/test pathname=/tmp/test/origin.txt wd=1 >
event = <Event dir=False mask=0x20 maskname=IN_OPEN name=origin.txt path=/tmp/test pathname=/tmp/test/origin.txt wd=1 >
event = <Event dir=False mask=0x4 maskname=IN_ATTRIB name=origin.txt path=/tmp/test pathname=/tmp/test/origin.txt wd=1 >
event = <Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name=origin.txt path=/tmp/test pathname=/tmp/test/origin.txt wd=1 >
===========================================

['cp', '/tmp/test/origin.txt', '/tmp/test/cp.txt']
event = <Event dir=False mask=0x20 maskname=IN_OPEN name=origin.txt path=/tmp/test pathname=/tmp/test/origin.txt wd=1 >
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=cp.txt path=/tmp/test pathname=/tmp/test/cp.txt wd=1 >
event = <Event dir=False mask=0x20 maskname=IN_OPEN name=cp.txt path=/tmp/test pathname=/tmp/test/cp.txt wd=1 >
event = <Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name=cp.txt path=/tmp/test pathname=/tmp/test/cp.txt wd=1 >
event = <Event dir=False mask=0x10 maskname=IN_CLOSE_NOWRITE name=origin.txt path=/tmp/test pathname=/tmp/test/origin.txt wd=1 >
===========================================

['mv', '/tmp/test/cp.txt', '/tmp/test/mv.txt']
event = <Event cookie=416768 dir=False mask=0x40 maskname=IN_MOVED_FROM name=cp.txt path=/tmp/test pathname=/tmp/test/cp.txt wd=1 >
event = <Event cookie=416768 dir=False mask=0x80 maskname=IN_MOVED_TO name=mv.txt path=/tmp/test pathname=/tmp/test/mv.txt src_pathname=/tmp/test/cp.txt wd=1 >
===========================================

['mv', '/tmp/test/mv.txt', '/tmp/mv.txt']
event = <Event cookie=416769 dir=False mask=0x40 maskname=IN_MOVED_FROM name=mv.txt path=/tmp/test pathname=/tmp/test/mv.txt wd=1 >
===========================================

['rsync', '-a', '/tmp/mv.txt', '/tmp/test/rsync.txt']
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=.rsync.txt.o0Quqe path=/tmp/test pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
event = <Event dir=False mask=0x20 maskname=IN_OPEN name=.rsync.txt.o0Quqe path=/tmp/test pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
event = <Event dir=False mask=0x4 maskname=IN_ATTRIB name=.rsync.txt.o0Quqe path=/tmp/test pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
event = <Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name=.rsync.txt.o0Quqe path=/tmp/test pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
event = <Event dir=False mask=0x4 maskname=IN_ATTRIB name=.rsync.txt.o0Quqe path=/tmp/test pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
event = <Event cookie=416770 dir=False mask=0x40 maskname=IN_MOVED_FROM name=.rsync.txt.o0Quqe path=/tmp/test pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
event = <Event cookie=416770 dir=False mask=0x80 maskname=IN_MOVED_TO name=rsync.txt path=/tmp/test pathname=/tmp/test/rsync.txt src_pathname=/tmp/test/.rsync.txt.o0Quqe wd=1 >
===========================================

['scp', '/tmp/mv.txt', '/tmp/test/scp.txt']
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=scp.txt path=/tmp/test pathname=/tmp/test/scp.txt wd=1 >
event = <Event dir=False mask=0x20 maskname=IN_OPEN name=scp.txt path=/tmp/test pathname=/tmp/test/scp.txt wd=1 >
event = <Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name=scp.txt path=/tmp/test pathname=/tmp/test/scp.txt wd=1 >
===========================================

['ln', '-s', '/tmp/test/origin.txt', '/tmp/test/ln.txt']
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=ln.txt path=/tmp/test pathname=/tmp/test/ln.txt wd=1 >
===========================================

['rm', '/tmp/test/ln.txt']
event = <Event dir=False mask=0x200 maskname=IN_DELETE name=ln.txt path=/tmp/test pathname=/tmp/test/ln.txt wd=1 >
===========================================

['mkdir', '/tmp/test/testdir']
event = <Event dir=True mask=0x40000100 maskname=IN_CREATE|IN_ISDIR name=testdir path=/tmp/test pathname=/tmp/test/testdir wd=1 >
===========================================

event = <Event dir=True mask=0x40000020 maskname=IN_OPEN|IN_ISDIR name=testdir path=/tmp/test pathname=/tmp/test/testdir wd=1 >
event = <Event dir=True mask=0x40000020 maskname=IN_OPEN|IN_ISDIR name='' path=/tmp/test/testdir pathname=/tmp/test/testdir wd=2 >
event = <Event dir=True mask=0x40000001 maskname=IN_ACCESS|IN_ISDIR name=testdir path=/tmp/test pathname=/tmp/test/testdir wd=1 >
event = <Event dir=True mask=0x40000001 maskname=IN_ACCESS|IN_ISDIR name='' path=/tmp/test/testdir pathname=/tmp/test/testdir wd=2 >
event = <Event dir=True mask=0x40000001 maskname=IN_ACCESS|IN_ISDIR name=testdir path=/tmp/test pathname=/tmp/test/testdir wd=1 >
event = <Event dir=True mask=0x40000001 maskname=IN_ACCESS|IN_ISDIR name='' path=/tmp/test/testdir pathname=/tmp/test/testdir wd=2 >
event = <Event dir=True mask=0x40000010 maskname=IN_CLOSE_NOWRITE|IN_ISDIR name=testdir path=/tmp/test pathname=/tmp/test/testdir wd=1 >
event = <Event dir=True mask=0x40000010 maskname=IN_CLOSE_NOWRITE|IN_ISDIR name='' path=/tmp/test/testdir pathname=/tmp/test/testdir wd=2 >
===========================================

['ln', '/tmp/test/origin.txt', '/tmp/test/lnhard.txt']
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=lnhard.txt path=/tmp/test pathname=/tmp/test/lnhard.txt wd=1 >
===========================================

['python', '-c', "import os; f = open(os.path.join('/tmp/test', 'python.txt'), 'w'); f.write('test_python'); f.close()"]
event = <Event dir=False mask=0x100 maskname=IN_CREATE name=python.txt path=/tmp/test pathname=/tmp/test/python.txt wd=1 >
event = <Event dir=False mask=0x20 maskname=IN_OPEN name=python.txt path=/tmp/test pathname=/tmp/test/python.txt wd=1 >
event = <Event dir=False mask=0x2 maskname=IN_MODIFY name=python.txt path=/tmp/test pathname=/tmp/test/python.txt wd=1 >
event = <Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name=python.txt path=/tmp/test pathname=/tmp/test/python.txt wd=1 >
===========================================

finished

pyinotify result table