[PATCH] util: add fswatcher class

Sune Foldager cryo at cyanite.org
Wed May 25 07:30:03 CDT 2011


# HG changeset patch
# User Sune Foldager <cryo at cyanite.org>
# Date 1306326237 -7200
# Node ID 0dedf75d80c4bf4f38ae240e7bba747f5a2d6f6f
# Parent  a6b543e053058c39b52e2b7c1e9a4b7c14c66a56
util: add fswatcher class

Used to watch files and directories for modifications.

diff -r a6b543e05305 -r 0dedf75d80c4 mercurial/util.py
--- a/mercurial/util.py	Wed May 25 10:06:17 2011 +0200
+++ b/mercurial/util.py	Wed May 25 14:23:57 2011 +0200
@@ -861,6 +861,36 @@
             limit -= len(s)
         yield s
 
+class fswatcher(object):
+    """Watches files and directories for changes."""
+
+    def __init__(self, *paths):
+        """create an fswatcher for the given paths."""
+        self.paths = paths
+        self.mtimes = {}
+        self.check()
+
+    def check(self):
+        """return a list of changes since last call."""
+        changes = []
+        oldmtimes = self.mtimes
+        mtimes = {}
+        ctime = int(time.time())
+        for p in self.paths:
+            try:
+                st = os.stat(p)
+                mtime = st.st_mtime
+                if mtime == ctime:
+                    mtime -= 1
+                mtimes[p] = mtime
+                if oldmtimes.get(p) != mtime:
+                    changes.append(p)
+            except OSError:
+                if p in oldmtimes:
+                    changes.append(p)
+        self.mtimes = mtimes
+        return changes
+
 def makedate():
     lt = time.localtime()
     if lt[8] == 1 and time.daylight:


More information about the Mercurial-devel mailing list