Skip to content

Commit

Permalink
use os.walk for recursing
Browse files Browse the repository at this point in the history
  • Loading branch information
xlotlu committed Mar 7, 2018
1 parent 78670d8 commit d512a2c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 59 deletions.
44 changes: 5 additions & 39 deletions inotify/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,13 @@ def __init__(self, path, mask=inotify.constants.IN_ALL_EVENTS,
block_duration_s=_DEFAULT_EPOLL_BLOCK_DURATION_S):
super(InotifyTree, self).__init__(mask=mask, block_duration_s=block_duration_s)

self.__root_path = path

self.__load_tree(path)

def __load_tree(self, path):
_LOGGER.debug("Adding initial watches on tree: [%s]", path)

paths = []

q = [path]
while q:
current_path = q[0]
del q[0]

paths.append(current_path)

for filename in os.listdir(current_path):
entry_filepath = os.path.join(current_path, filename)
if os.path.isdir(entry_filepath) is False:
continue

q.append(entry_filepath)

for path in paths:
self._i.add_watch(path, self._mask)
for dirpath, _d, _f in os.walk(path):
self._i.add_watch(dirpath, self._mask)


class InotifyTrees(_BaseTree):
Expand All @@ -359,22 +341,6 @@ def __init__(self, paths, mask=inotify.constants.IN_ALL_EVENTS,
def __load_trees(self, paths):
_LOGGER.debug("Adding initial watches on trees: [%s]", ",".join(map(str, paths)))

found = []

q = paths
while q:
current_path = q[0]
del q[0]

found.append(current_path)

for filename in os.listdir(current_path):
entry_filepath = os.path.join(current_path, filename)
if os.path.isdir(entry_filepath) is False:
continue

q.append(entry_filepath)


for path in found:
self._i.add_watch(path, self._mask)
for path in paths:
for dirpath, _d, _f in os.walk(path):
self._i.add_watch(dirpath, self._mask)
3 changes: 0 additions & 3 deletions inotify/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ def temp_path():
path = tempfile.mkdtemp()

original_wd = os.getcwd()
os.chdir(path)

try:
yield path
finally:
os.chdir(original_wd)

if os.path.exists(path) is True:
shutil.rmtree(path)
60 changes: 43 additions & 17 deletions tests/test_inotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def test__cycle(self):

i = inotify.adapters.InotifyTree(path)

with open('seen_new_file1', 'w'):
watches = i._i._Inotify__watches
w2, w3 = watches[path1], watches[path2]

with open(os.path.join(path, 'seen_new_file1'), 'w'):
pass

with open(os.path.join(path1, 'seen_new_file2'), 'w'):
Expand All @@ -184,29 +187,45 @@ def test__cycle(self):

events = self.__read_all_events(i)

expected = [
_access_dir_a = [
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741856, cookie=0, len=16), ['IN_ISDIR', 'IN_OPEN'], path, 'aa'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741825, cookie=0, len=16), ['IN_ACCESS', 'IN_ISDIR'], path, 'aa'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741840, cookie=0, len=16), ['IN_ISDIR', 'IN_CLOSE_NOWRITE'], path, 'aa'),
]

_access_dir_b = [
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741856, cookie=0, len=16), ['IN_ISDIR', 'IN_OPEN'], path, 'bb'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741825, cookie=0, len=16), ['IN_ACCESS', 'IN_ISDIR'], path, 'bb'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741840, cookie=0, len=16), ['IN_ISDIR', 'IN_CLOSE_NOWRITE'], path, 'bb'),
]

# we can't be sure about the order the watches were registered
expected = (_access_dir_a + _access_dir_b if w2 < w3
else _access_dir_b + _access_dir_a)

expected += [
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=256, cookie=0, len=16), ['IN_CREATE'], path, 'seen_new_file1'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=32, cookie=0, len=16), ['IN_OPEN'], path, 'seen_new_file1'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], path, 'seen_new_file1'),

(inotify.adapters._INOTIFY_EVENT(wd=2, mask=256, cookie=0, len=16), ['IN_CREATE'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=2, mask=32, cookie=0, len=16), ['IN_OPEN'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=2, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=w2, mask=256, cookie=0, len=16), ['IN_CREATE'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=w2, mask=32, cookie=0, len=16), ['IN_OPEN'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=w2, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], path1, 'seen_new_file2'),

(inotify.adapters._INOTIFY_EVENT(wd=3, mask=256, cookie=0, len=16), ['IN_CREATE'], path2, 'seen_new_file3'),
(inotify.adapters._INOTIFY_EVENT(wd=3, mask=32, cookie=0, len=16), ['IN_OPEN'], path2, 'seen_new_file3'),
(inotify.adapters._INOTIFY_EVENT(wd=3, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], path2, 'seen_new_file3'),
(inotify.adapters._INOTIFY_EVENT(wd=w3, mask=256, cookie=0, len=16), ['IN_CREATE'], path2, 'seen_new_file3'),
(inotify.adapters._INOTIFY_EVENT(wd=w3, mask=32, cookie=0, len=16), ['IN_OPEN'], path2, 'seen_new_file3'),
(inotify.adapters._INOTIFY_EVENT(wd=w3, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], path2, 'seen_new_file3'),

(inotify.adapters._INOTIFY_EVENT(wd=1, mask=512, cookie=0, len=16), ['IN_DELETE'], path, 'seen_new_file1'),
(inotify.adapters._INOTIFY_EVENT(wd=2, mask=512, cookie=0, len=16), ['IN_DELETE'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=3, mask=512, cookie=0, len=16), ['IN_DELETE'], path2, 'seen_new_file3'),
(inotify.adapters._INOTIFY_EVENT(wd=w2, mask=512, cookie=0, len=16), ['IN_DELETE'], path1, 'seen_new_file2'),
(inotify.adapters._INOTIFY_EVENT(wd=w3, mask=512, cookie=0, len=16), ['IN_DELETE'], path2, 'seen_new_file3'),

(inotify.adapters._INOTIFY_EVENT(wd=2, mask=1024, cookie=0, len=0), ['IN_DELETE_SELF'], path1, ''),
(inotify.adapters._INOTIFY_EVENT(wd=2, mask=32768, cookie=0, len=0), ['IN_IGNORED'], path1, ''),
(inotify.adapters._INOTIFY_EVENT(wd=w2, mask=1024, cookie=0, len=0), ['IN_DELETE_SELF'], path1, ''),
(inotify.adapters._INOTIFY_EVENT(wd=w2, mask=32768, cookie=0, len=0), ['IN_IGNORED'], path1, ''),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073742336, cookie=0, len=16), ['IN_ISDIR', 'IN_DELETE'], path, 'aa'),

(inotify.adapters._INOTIFY_EVENT(wd=3, mask=1024, cookie=0, len=0), ['IN_DELETE_SELF'], path2, ''),
(inotify.adapters._INOTIFY_EVENT(wd=3, mask=32768, cookie=0, len=0), ['IN_IGNORED'], path2, ''),
(inotify.adapters._INOTIFY_EVENT(wd=w3, mask=1024, cookie=0, len=0), ['IN_DELETE_SELF'], path2, ''),
(inotify.adapters._INOTIFY_EVENT(wd=w3, mask=32768, cookie=0, len=0), ['IN_IGNORED'], path2, ''),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073742336, cookie=0, len=16), ['IN_ISDIR', 'IN_DELETE'], path, 'bb'),
]

Expand Down Expand Up @@ -254,8 +273,8 @@ def test__renames(self):
os.path.join(new_path, 'old_filename'),
os.path.join(new_path, 'new_filename'))

os.remove(os.path.join('new_folder', 'new_filename'))
os.rmdir('new_folder')
os.remove(os.path.join(path, 'new_folder', 'new_filename'))
os.rmdir(os.path.join(path, 'new_folder'))

events3 = self.__read_all_events(i)

Expand Down Expand Up @@ -297,7 +316,6 @@ def test__automatic_new_watches_on_new_paths(self):

self.assertEquals(events, expected)


os.mkdir(path2)

events = self.__read_all_events(i)
Expand Down Expand Up @@ -342,6 +360,14 @@ def test__automatic_new_watches_on_existing_paths(self):
events = self.__read_all_events(i)

expected = [
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741856, cookie=0, len=16), ['IN_ISDIR', 'IN_OPEN'], path, 'folder1'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741825, cookie=0, len=16), ['IN_ACCESS', 'IN_ISDIR'], path, 'folder1'),
(inotify.adapters._INOTIFY_EVENT(wd=1, mask=1073741840, cookie=0, len=16), ['IN_ISDIR', 'IN_CLOSE_NOWRITE'], path, 'folder1'),

(inotify.adapters._INOTIFY_EVENT(wd=2, mask=1073741856, cookie=0, len=16), ['IN_ISDIR', 'IN_OPEN'], path1, 'folder2'),
(inotify.adapters._INOTIFY_EVENT(wd=2, mask=1073741825, cookie=0, len=16), ['IN_ACCESS', 'IN_ISDIR'], path1, 'folder2'),
(inotify.adapters._INOTIFY_EVENT(wd=2, mask=1073741840, cookie=0, len=16), ['IN_ISDIR', 'IN_CLOSE_NOWRITE'], path1, 'folder2'),

(inotify.adapters._INOTIFY_EVENT(wd=3, mask=256, cookie=0, len=16), ['IN_CREATE'], path2, 'filename'),
(inotify.adapters._INOTIFY_EVENT(wd=3, mask=32, cookie=0, len=16), ['IN_OPEN'], path2, 'filename'),
(inotify.adapters._INOTIFY_EVENT(wd=3, mask=8, cookie=0, len=16), ['IN_CLOSE_WRITE'], path2, 'filename'),
Expand Down

0 comments on commit d512a2c

Please sign in to comment.