[Checkins] SVN: zc.buildout/branches/tlotze-download-api/src/zc/buildout/download. stop treating local resources specially, just put them in the cache

Thomas Lotze tl at gocept.com
Wed Jun 17 00:43:55 EDT 2009


Log message for revision 101087:
  stop treating local resources specially, just put them in the cache

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

-=-
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-16 20:58:24 UTC (rev 101086)
+++ zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py	2009-06-17 04:43:54 UTC (rev 101087)
@@ -85,31 +85,13 @@
         Returns the path to the downloaded file.
 
         """
-        if urlparse.urlparse(url, 'file').scheme == 'file':
-            local_path = self.use_local(url, md5sum)
-        elif self.cache:
+        if self.cache:
             local_path = self.download_cached(url, md5sum)
         else:
             local_path = self.download(url, md5sum, path)
 
-        if path is None or realpath(path) == realpath(local_path):
-            return local_path
+        return locate_at(local_path, path)
 
-        try:
-            os.link(local_path, path)
-        except (AttributeError, OSError):
-            shutil.copyfile(local_path, path)
-        return path
-
-    def use_local(self, url, md5sum=None):
-        """Locate and verify the MD5 checksum of a local resource.
-        """
-        path = urlparse.urlparse(url).path
-        if not check_md5sum(path, md5sum):
-            raise ChecksumError(
-                'MD5 checksum mismatch for local resource at %r.' % path)
-        return path
-
     def download_cached(self, url, md5sum=None):
         """Download a file from a URL using the cache.
 
@@ -143,15 +125,20 @@
     def download(self, url, md5sum=None, path=None):
         """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
-        might try to move or remove local resources.
+        An online 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 and scheduled for deletion at process exit.
 
-        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
-        and scheduled for deletion at process exit.
-
         """
+        parsed_url = urlparse.urlparse(url, 'file')
+        if parsed_url.scheme == 'file':
+            if not check_md5sum(path, md5sum):
+                raise ChecksumError(
+                    'MD5 checksum mismatch for local resource at %r.' %
+                    parsed_url.path)
+            return locate_at(parsed_url.path, path)
+
         if (self.buildout.get('offline') == 'true'
             or self.buildout.get('install-from-cache') == 'true'):
             raise zc.buildout.UserError(
@@ -210,3 +197,14 @@
 def remove(path):
     if os.path.exists(path):
         os.remove(path)
+
+
+def locate_at(source, dest):
+    if dest is None or realpath(dest) == realpath(source):
+        return source
+
+    try:
+        os.link(source, dest)
+    except (AttributeError, OSError):
+        shutil.copyfile(source, dest)
+    return dest

Modified: zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.txt
===================================================================
--- zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.txt	2009-06-16 20:58:24 UTC (rev 101086)
+++ zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.txt	2009-06-17 04:43:54 UTC (rev 101087)
@@ -200,7 +200,8 @@
 >>> cat(download(server_url+'foo.txt'))
 This is a foo text.
 
-On the other hand, local resources will never be cached:
+Local resources will be cached just like any others since download caches are
+sometimes used to create source distributions:
 
 >>> remove(cache, 'foo.txt')
 >>> ls(cache)
@@ -211,13 +212,19 @@
 >>> cat(download('file://' + join(server_data, 'foo.txt'), path=path))
 This is a foo text.
 >>> ls(cache)
+- foo.txt
 
+>>> remove(cache, 'foo.txt')
+
 >>> cat(download(join(server_data, 'foo.txt'), path=path))
 This is a foo text.
 >>> ls(cache)
+- foo.txt
 
-Neither will a cached copy of a resource with a checksum mismatch be created:
+>>> remove(cache, 'foo.txt')
 
+However, resources with checksum mismatches will not be copied to the cache:
+
 >>> download(server_url+'foo.txt', md5('The wrong text.').hexdigest())
 Traceback (most recent call last):
 ChecksumError: MD5 checksum mismatch downloading 'http://localhost/foo.txt'



More information about the Checkins mailing list