[Zope-CVS] CVS: Packages/zpkgtools/zpkgtools - cvsloader.py:1.5

Fred L. Drake, Jr. fred at zope.com
Wed Mar 10 13:17:39 EST 2004


Update of /cvs-repository/Packages/zpkgtools/zpkgtools
In directory cvs.zope.org:/tmp/cvs-serv20394

Modified Files:
	cvsloader.py 
Log Message:
add a file-like proxy to make a single file pulled down by a CvsLoader
get cleaned up when closed


=== Packages/zpkgtools/zpkgtools/cvsloader.py 1.4 => 1.5 ===
--- Packages/zpkgtools/zpkgtools/cvsloader.py:1.4	Tue Mar  9 15:38:44 2004
+++ Packages/zpkgtools/zpkgtools/cvsloader.py	Wed Mar 10 13:17:08 2004
@@ -89,7 +89,7 @@
         # The tag may be overridden for specific files; check:
         entries = os.path.join(cvsdir, "Entries")
         if os.path.isfile(entries):
-            entries = open(entries, "rU")
+            entries = file(entries, "rU")
             for line in entries:
                 parts = line.split("/")
                 if (len(parts) >= 6 and
@@ -115,7 +115,7 @@
 
 
 def _read_one_line(filename):
-    f = open(filename, "rU")
+    f = file(filename, "rU")
     try:
         line = f.readline()
     finally:
@@ -186,6 +186,19 @@
         return url
 
 
+def open(url, mode="r"):
+    if mode[:1] != "r" or "+" in mode:
+        raise ValueError("CVS resources can only be opened in read-only mode")
+    loader = CvsLoader(None)
+    path = loader.load(url)
+    if os.path.isfile(path):
+        return FileProxy(path, mode)
+    # Only files and directories come from CVS, so no need to check
+    # for magical directory entries here:
+    loader.cleanup()
+    raise IOError(errno.EISDIR, "Is a directory", url)
+
+
 class CvsLoader:
 
     def __init__(self, cvsurl, tag=None):
@@ -301,3 +314,32 @@
     def openCvsRLog(self, cvsroot, path):
         return os.popen(
             "cvs -f -d '%s' rlog -R -l '%s'" % (cvsroot, path), "r")
+
+
+class FileProxy(object):
+
+    def __init__(self, path, mode, loader):
+        self.name = path
+        self._file = file(path, mode)
+        self._cleanup = loader.cleanup
+
+    def __getattr__(self, name):
+        return getattr(self._file, name)
+
+    def close(self):
+        if not self._file.closed:
+            self._file.close()
+            self._cleanup()
+            self._cleanup = None
+
+    # We shouldn't ever actually need to deal with softspace since
+    # we're read-only, but... real files still behave this way, so we
+    # emulate it.
+
+    def _get_softspace(self):
+        return self._file.softspace
+
+    def _set_softspace(self, value):
+        self._file.softspace = value
+
+    softspace = property(_get_softspace, _set_softspace)




More information about the Zope-CVS mailing list