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 directory structure was touched since last revision.
21
from __future__ import absolute_import
23
# TODO(jelmer): Add support for ignore files
27
from pyinotify import (
40
except ImportError as e:
41
from .errors import DependencyNotPresent
42
raise DependencyNotPresent(library='pyinotify', error=e)
46
IN_CLOSE_WRITE | IN_DELETE | IN_Q_OVERFLOW | IN_MOVED_TO | IN_MOVED_FROM |
50
class _Process(ProcessEvent):
56
def process_default(self, event):
57
path = os.path.join(event.path, event.name)
58
if event.mask & IN_CREATE:
59
self.created.add(path)
61
if event.mask & IN_DELETE and path in self.created:
62
self.paths.remove(path)
63
self.created.remove(path)
66
class DirtyTracker(object):
67
"""Track the changes to (part of) a working tree."""
69
def __init__(self, tree, subpath='.'):
71
self._wm = WatchManager()
72
self._process = _Process()
73
self._notifier = Notifier(self._wm, self._process)
74
self._notifier.coalesce_events(True)
76
def check_excluded(p):
77
return tree.is_control_filename(tree.relpath(p))
78
self._wdd = self._wm.add_watch(
79
tree.abspath(subpath), MASK, rec=True, auto_add=True,
80
exclude_filter=check_excluded)
82
def _process_pending(self):
83
if self._notifier.check_events(timeout=0):
84
self._notifier.read_events()
85
self._notifier.process_events()
91
"""Mark the subtree as not having any changes."""
92
self._process_pending()
93
self._process.paths.clear()
94
self._process.created.clear()
97
"""Check whether there are any changes."""
98
self._process_pending()
99
return bool(self._process.paths)
102
"""Return the paths that have changed."""
103
self._process_pending()
104
return self._process.paths
107
"""Return the paths relative to the tree root that changed."""
108
return set(self._tree.relpath(p) for p in self.paths())