2
# Copyright (C) 2019 Jelmer Vernooij
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Track whether a particular directory structure is dirty."""
21
from pyinotify import (
37
IN_CLOSE_WRITE | IN_DELETE | IN_Q_OVERFLOW | IN_MOVED_TO | IN_MOVED_FROM |
41
class _Process(ProcessEvent):
47
def process_default(self, event):
48
path = os.path.join(event.path, event.name)
49
if event.mask & IN_CREATE:
50
self.created.add(path)
52
if event.mask & IN_DELETE and path in self.created:
53
self.paths.remove(path)
54
self.created.remove(path)
57
class DirtyTracker(object):
59
def __init__(self, tree, subpath='.'):
61
self._wm = WatchManager()
62
self._process = _Process()
63
self._notifier = Notifier(self._wm, self._process)
64
self._notifier.coalesce_events(True)
66
def check_excluded(p):
67
return tree.is_control_filename(tree.relpath(p))
68
self._wdd = self._wm.add_watch(
69
tree.abspath(subpath), MASK, rec=True, auto_add=True,
70
exclude_filter=check_excluded)
72
def _process_pending(self):
73
if self._notifier.check_events(timeout=0):
74
self._notifier.read_events()
75
self._notifier.process_events()
81
self._process_pending()
82
self._process.paths.clear()
83
self._process.created.clear()
86
self._process_pending()
87
return bool(self._process.paths)
90
self._process_pending()
91
return self._process.paths
94
return set(self._tree.relpath(p) for p in self.paths())