[Zope-CMF] Another DirectoryView.py fix :-)

Chris Withers chrisw@nipltd.com
Wed, 6 Jun 2001 16:39:52 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_06A9_01C0EEA7.4F763570
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

This one solves the problem I mailed about eariler today:
DirectoryView's weren't finding new files or getting rid of them in debug
mode on Windows.
I think this is because the stat of a directory doesn't change when the
contents change, on windows at least.

The attached path fixes this. It's not pretty (to make up that that I
included a bit of refactoring too ;-) but it does work.

Hope people find it useful :-)

cheers,

Chris

PS: Tres: I'll file a Tracker issue on this tomorrow ;-)

------=_NextPart_000_06A9_01C0EEA7.4F763570
Content-Type: application/octet-stream;
	name="DirectoryView.py.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="DirectoryView.py.patch"

--- DirectoryView.py.3	Wed Jun 06 16:34:54 2001
+++ DirectoryView.py	Wed Jun 06 16:34:22 2001
@@ -133,20 +133,27 @@
         p =3D p[1:]
     return p
=20
+# Ignore version control subdirectories
+# and special names.
+def _filter_dirnames(names):
+    n =3D filter(lambda name: name not in ('CVS', 'SVN', '.', '..'),
+                  names)
+    return names
+
+def _walker (listdir, dirname, names):
+    names[:]=3D_filter_dirnames(names)
+    listdir.extend(names)
=20
 class DirectoryInformation:
     data =3D None
     _v_last_read =3D 0
+    _v_last_filelist =3D []
=20
     def __init__(self, expanded_fp, minimal_fp):
         self.filepath =3D minimal_fp
         l =3D listdir(expanded_fp)
         subdirs =3D []
-        for entry in l:
-            if entry in ('CVS', 'SVN', '.', '..'):
-                # Ignore version control subdirectories
-                # and special names.
-                continue
+        for entry in _filter_dirnames(l):
             e_fp =3D path.join(expanded_fp, entry)
             if path.isdir(e_fp):
                 subdirs.append(entry)
@@ -186,11 +193,30 @@
     def getContents(self, registry):
         changed =3D 0
         if Globals.DevelopmentMode:
-            try: mtime =3D stat(expandpath(self.filepath))[8]
-            except: mtime =3D 0
-            if mtime !=3D self._v_last_read:
+            mtime=3D0
+            filelist=3D[]
+            try:
+                fp =3D expandpath(self.filepath)
+                mtime =3D stat(fp)[8]
+                # Windows directories don't change mtime when a file
+                # in them changes :-(
+                # So keep a list of files as well, and see if that
+                # changes
+                path.walk(fp,_walker,filelist)
+                filelist.sort()
+            except:=20
+                from zLOG import LOG, ERROR
+                import sys
+                LOG('DirectoryView',
+                    ERROR,
+                    'Error checking for directory modification',
+                    error=3Dsys.exc_info())
+               =20
+            if mtime !=3D self._v_last_read or filelist !=3D =
self._v_last_filelist:
                 self._v_last_read =3D mtime
+                self._v_last_filelist =3D filelist
                 changed =3D 1
+               =20
         if self.data is None or changed:
             try:
                 self.data, self.objects =3D =
self.prepareContents(registry,
@@ -214,7 +240,7 @@
         objects =3D []
         l =3D listdir(fp)
         types =3D self._readTypesFile()
-        for entry in l:
+        for entry in _filter_dirnames(l):
             if not self._isAllowableFilename(entry):
                 continue
             e_filepath =3D path.join(self.filepath, entry)
@@ -225,9 +251,8 @@
                 info =3D registry.getDirectoryInfo(e_filepath)
                 if info is None and register_subdirs:
                     # Register unknown subdirs
-                    if entry not in ('CVS', 'SVN', '.', '..'):
-                        registry.registerDirectoryByPath(e_fp)
-                        info =3D registry.getDirectoryInfo(e_filepath)
+                    registry.registerDirectoryByPath(e_fp)
+                    info =3D registry.getDirectoryInfo(e_filepath)
                 if info is not None:
                     mt =3D types.get(entry)
                     t =3D None

------=_NextPart_000_06A9_01C0EEA7.4F763570--