[Checkins] SVN: zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py have temporary files created by downloads removed at exit, updated and improved doc strings

Thomas Lotze tl at gocept.com
Tue Jun 9 03:13:00 EDT 2009


Log message for revision 100753:
  have temporary files created by downloads removed at exit, updated and improved doc strings

Changed:
  U   zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py

-=-
Modified: zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py
===================================================================
--- zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py	2009-06-09 02:41:48 UTC (rev 100752)
+++ zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py	2009-06-09 07:13:00 UTC (rev 100753)
@@ -17,6 +17,7 @@
     from hashlib import md5
 except ImportError:
     from md5 import new as md5
+import atexit
 import os
 import os.path
 import shutil
@@ -102,9 +103,6 @@
 
     def use_local(self, url, md5sum=None):
         """Locate and verify the MD5 checksum of a local resource.
-
-        See __call__.
-
         """
         path = urlparse.urlparse(url).path
         if not check_md5sum(path, md5sum):
@@ -113,9 +111,11 @@
         return path
 
     def download_cached(self, url, md5sum=None):
-        """Download a file to the cache, assuming the cache was configured.
+        """Download a file from a URL using the cache.
 
-        See __call__.
+        This method assumes that the cache has been configured. Optionally, it
+        raises a ChecksumError if a cached copy of a file has an MD5 mismatch,
+        but will not remove the copy in that case.
 
         """
         if not os.path.exists(self.cache):
@@ -141,17 +141,16 @@
         return cached_path
 
     def download(self, url, md5sum=None, path=None):
-        """Download a file to a given path.
+        """Download a file from a URL to a given or temporary path.
 
         Note: The url is assumed to point to an online resource; this method
-        would try to move local resources if a destination path is given.
+        might try to move or remove local resources.
 
         The resource is always downloaded to a temporary file and moved to the
         specified path only after the download is complete and the checksum
-        (if given) matches. If path is None, the temporary file is returned.
+        (if given) matches. If path is None, the temporary file is returned
+        and scheduled for deletion at process exit.
 
-        See __call__.
-
         """
         if self.buildout.get('offline'):
             raise zc.buildout.UserError(
@@ -168,6 +167,7 @@
             shutil.move(tmp_path, path)
             return path
         else:
+            atexit.register(remove, tmp_path)
             return tmp_path
 
     def filename(self, url):
@@ -203,3 +203,8 @@
         return checksum.hexdigest() == md5sum
     finally:
         f.close()
+
+
+def remove(path):
+    if os.path.exists(path):
+        os.remove(path)



More information about the Checkins mailing list