[PATCH] util: add fswatcher class

Benoit Boissinot bboissin at gmail.com
Wed May 25 07:49:57 CDT 2011


On Wed, May 25, 2011 at 2:30 PM, Sune Foldager <cryo at cyanite.org> wrote:
> # 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:

if mtime == ctime and p not in oldmtimes
(and just curious, why did you remove the size check?)

> +                    mtime -= 1
> +                mtimes[p] = mtime
> +                if oldmtimes.get(p) != mtime:
> +                    changes.append(p)
> +            except OSError:

check errno == ENOENT, raise otherwise?

cheers,

Benoit


More information about the Mercurial-devel mailing list