[Checkins] SVN: zc.buildout/branches/tlotze-download-api/src/zc/buildout/download. implemented falling back on cached files only if a resource is unavailable

Thomas Lotze tl at gocept.com
Wed May 27 14:11:02 EDT 2009


Log message for revision 100486:
  implemented falling back on cached files only if a resource is unavailable
  

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-05-27 18:01:41 UTC (rev 100485)
+++ zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.py	2009-05-27 18:11:01 UTC (rev 100486)
@@ -29,9 +29,16 @@
     http_error_default = urllib.URLopener.http_error_default
 
 
+class ChecksumError(zc.buildout.UserError):
+    pass
+
+
 url_opener = URLOpener()
 
 
+FALLBACK = object()
+
+
 class Download(object):
     """Configurable download utility.
 
@@ -96,16 +103,26 @@
         See __call__.
 
         """
+        if not os.path.exists(self.cache):
+            os.makedirs(self.cache)
         cached_path = os.path.join(self.cache, self.filename(url))
+
         if os.path.isfile(cached_path):
+            if self.use_cache is FALLBACK:
+                try:
+                    self.download(url, md5sum, cached_path)
+                except ChecksumError:
+                    raise
+                except Exception:
+                    pass
+
             if not check_md5sum(cached_path, md5sum):
-                raise zc.buildout.UserError(
+                raise ChecksumError(
                     'MD5 checksum mismatch for cached download '
                     'from %r at %r' % (url, cached_path))
         else:
-            if not os.path.exists(self.cache):
-                os.makedirs(self.cache)
             self.download(url, md5sum, cached_path)
+
         return cached_path
 
     def download(self, url, md5sum=None, path=None):
@@ -124,7 +141,7 @@
         urllib._urlopener = url_opener
         path, headers = urllib.urlretrieve(url, path)
         if not check_md5sum(path, md5sum):
-            raise zc.buildout.UserError(
+            raise ChecksumError(
                 'MD5 checksum mismatch downloading %r' % url)
         return path
 

Modified: zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.txt
===================================================================
--- zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.txt	2009-05-27 18:01:41 UTC (rev 100485)
+++ zc.buildout/branches/tlotze-download-api/src/zc/buildout/download.txt	2009-05-27 18:11:01 UTC (rev 100486)
@@ -54,7 +54,7 @@
 >>> path = download(server+'foo.txt', md5('This is a foo text.').hexdigest())
 >>> path = download(server+'foo.txt', md5('The wrong text.').hexdigest())
 Traceback (most recent call last):
-UserError: MD5 checksum mismatch downloading 'http://localhost/foo.txt'
+ChecksumError: MD5 checksum mismatch downloading 'http://localhost/foo.txt'
 
 Finally, we can download the file to a specified place in the file system:
 
@@ -138,8 +138,8 @@
 
 >>> path = download(server+'foo.txt', md5('The wrong text.').hexdigest())
 Traceback (most recent call last):
-UserError: MD5 checksum mismatch for cached download
-           from 'http://localhost/foo.txt' at '/download-cache/foo.txt'
+ChecksumError: MD5 checksum mismatch for cached download
+               from 'http://localhost/foo.txt' at '/download-cache/foo.txt'
 
 Trying to access another file at a different URL which has the same base name
 will result in the cached copy being used:
@@ -300,3 +300,67 @@
 >>> ls(cache)
 - 09f5793fcdc1716727f72d49519c688d
 - 537b6d73267f8f4447586989af8c470e
+
+>>> remove(path)
+>>> remove(path2)
+>>> write(join(root, 'foo.txt'), 'This is a foo text.')
+
+
+Downloading with the cache being used when offline
+==================================================
+
+Another way the download cache can be used is purely as a fall-back option in
+the case that a file cannot be accessed on the net, such as when a server is
+down or if we are in offline mode. This mode is only in effect if a download
+cache is configured in the first place:
+
+>>> from zc.buildout.download import FALLBACK
+>>> download = Download({}, use_cache=FALLBACK)
+>>> print download.cache
+None
+
+>>> download = Download({'download-cache': cache}, use_cache=FALLBACK)
+>>> print download.cache
+/download-cache/
+
+A downloaded file will be cached:
+
+>>> ls(cache)
+>>> path = download(server+'foo.txt')
+>>> ls(cache)
+- foo.txt
+>>> cat(join(cache, 'foo.txt'))
+This is a foo text.
+
+If the file cannot be served, the cached copy will be used:
+
+>>> remove(join(root, 'foo.txt'))
+>>> Download({})(server+'foo.txt')
+Traceback (most recent call last):
+IOError: ('http error', 404, 'Not Found',
+          <httplib.HTTPMessage instance at 0xa35d36c>)
+>>> path = download(server+'foo.txt')
+>>> cat(path)
+This is a foo text.
+
+Similarly, if the file is served but we're in offline mode, we'll fall back to
+using the cache:
+
+>>> write(join(root, 'foo.txt'), 'The wrong text.')
+>>> cat(Download({})(server+'foo.txt'))
+The wrong text.
+>>> offline_download = Download({'download-cache': cache, 'offline': True},
+...                             use_cache=FALLBACK)
+>>> path = offline_download(server+'foo.txt')
+>>> cat(path)
+This is a foo text.
+
+However, when downloading the file normally with the cache being used in
+fall-back mode, the file will be downloaded from the net and the cached copy
+will be replaced with the new content:
+
+>>> path = download(server+'foo.txt')
+>>> cat(path)
+The wrong text.
+>>> cat(join(cache, 'foo.txt'))
+The wrong text.



More information about the Checkins mailing list