[Checkins] SVN: z3c.recipe.eggbasket/trunk/ When the download-directory has been set in buildout.cfg or in

Maurits van Rees m.van.rees at zestsoftware.nl
Thu Jan 15 15:01:31 EST 2009


Log message for revision 94763:
  When the download-directory has been set in buildout.cfg or in
  .buildout/default.cfg in the user's home directory, download the
  tarball there and do not throw it away afterwards.
  

Changed:
  U   z3c.recipe.eggbasket/trunk/CHANGES.txt
  U   z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/downloader.py
  U   z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/utils.py

-=-
Modified: z3c.recipe.eggbasket/trunk/CHANGES.txt
===================================================================
--- z3c.recipe.eggbasket/trunk/CHANGES.txt	2009-01-15 17:13:34 UTC (rev 94762)
+++ z3c.recipe.eggbasket/trunk/CHANGES.txt	2009-01-15 20:01:30 UTC (rev 94763)
@@ -6,7 +6,9 @@
 0.4.2 (unreleased)
 ==================
 
-* ...
+* When the download-directory has been set in buildout.cfg or in
+  .buildout/default.cfg in the user's home directory, download the
+  tarball there and do not throw it away afterwards.
 
 0.4.1 (2009-01-12)
 ==================

Modified: z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/downloader.py
===================================================================
--- z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/downloader.py	2009-01-15 17:13:34 UTC (rev 94762)
+++ z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/downloader.py	2009-01-15 20:01:30 UTC (rev 94763)
@@ -6,10 +6,10 @@
 import shutil
 import os
 import tarfile
-import urllib
 from zc.recipe.egg import Eggs
 from z3c.recipe.eggbasket.utils import distributions_are_installed_in_dir
 from z3c.recipe.eggbasket.utils import install_distributions
+from z3c.recipe.eggbasket.utils import download_tarball
 
 
 class Downloader(Eggs):
@@ -33,33 +33,59 @@
 
         if not distributions_are_installed_in_dir(distributions,
                                                   options['eggs-directory']):
-            log.info("Distributions are not installed. "
+            log.info("Not all distributions are installed. "
                      "A tarball will be downloaded.")
             tarball_name = url.split('/')[-1]
-            log.info("Downloading %s ..." % url)
 
-            # Make temporary files and directories.
+            # If the user has specified a download directory (in
+            # ~/.buildout/default.cfg probably) we will want to use
+            # it.
+            download_dir = self.buildout['buildout'].get('download-directory')
+            if download_dir:
+                if not os.path.exists(download_dir):
+                    os.mkdir(download_dir)
+                if not os.path.isdir(download_dir):
+                    # It exists but is not a file: we are not going to
+                    # use the download dir.
+                    download_dir = None
+
+            keep_tarball = False
+            if download_dir:
+                keep_tarball = True
+                tarball_location = os.path.join(download_dir, tarball_name)
+                if os.path.exists(tarball_location):
+                    log.info("Using already downloaded %s" % tarball_location)
+                else:
+                    result = download_tarball(tarball_location, url)
+                    if not result:
+                        # Give up.
+                        return tuple()
+
+            if not keep_tarball:
+                # Make temporary file.
+                try:
+                    filenum, tarball_location = tempfile.mkstemp()
+                except:
+                    log.error("Could not create temporary tarball file")
+                    # Reraise exception
+                    raise
+                result = download_tarball(tarball_location, url)
+                if not result:
+                    return tuple()
+
+            # We have the tarball.  Now we make a temporary extraction
+            # directory.
             try:
                 extraction_dir = tempfile.mkdtemp()
-                filenum, temp_tarball_name = tempfile.mkstemp()
             except:
-                log.error("Could not create temporary file or directory")
+                log.error("Could not create temporary extraction directory")
                 # Reraise exception
                 raise
+
             try:
-                # Note: 'b' mode needed for Windows.
-                tarball = open(temp_tarball_name, 'wb')
-                try:
-                    tarball.write(urllib.urlopen(url).read())
-                except IOError:
-                    log.error("Url not found: %s." % url)
-                    return tuple()
-                tarball.close()
-                log.info("Finished downloading.")
                 log.info("Extracting tarball contents...")
-
                 try:
-                    tf = tarfile.open(temp_tarball_name, 'r:gz')
+                    tf = tarfile.open(tarball_location, 'r:gz')
                 except tarfile.ReadError, e:
                     # Likely the download location is wrong and gives a 404.
                     # Or the tarball is not zipped.
@@ -82,7 +108,8 @@
                     log.error("Failed to install required eggs with the tar "
                               "ball.")
             finally:
-                os.unlink(temp_tarball_name)
+                if not keep_tarball:
+                    os.unlink(tarball_location)
                 shutil.rmtree(extraction_dir)
 
         # Return files that were created by the recipe. The buildout

Modified: z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/utils.py
===================================================================
--- z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/utils.py	2009-01-15 17:13:34 UTC (rev 94762)
+++ z3c.recipe.eggbasket/trunk/z3c/recipe/eggbasket/utils.py	2009-01-15 20:01:30 UTC (rev 94763)
@@ -10,6 +10,7 @@
 # XXX We may want to add command line argument handling.
 logging.basicConfig(level=logging.INFO,
                     format='%(levelname)-5s %(name)-12s %(message)s')
+log = logging.getLogger('eggbasket.utils')
 
 
 def install_distributions(distributions, target_dir, links=[],
@@ -191,3 +192,16 @@
         else:
             log.info("Finished downloading.")
             # TODO: check md5hash
+
+
+def download_tarball(location, url):
+    # Note: 'b' mode needed for Windows.
+    tarball = open(location, 'wb')
+    log.info("Downloading %s ..." % url)
+    try:
+        tarball.write(urllib.urlopen(url).read())
+    except IOError:
+        log.error("Url not found: %s." % url)
+        return False
+    tarball.close()
+    log.info("Finished downloading.")



More information about the Checkins mailing list