[Zope-CVS] CVS: Products/Basket - basket.py:1.2

Chris McDonough chrism at plope.com
Thu Nov 10 13:59:01 EST 2005


Update of /cvs-repository/Products/Basket
In directory cvs.zope.org:/tmp/cvs-serv26604

Modified Files:
	basket.py 
Log Message:
Make blast-out-to-tempfile behavior work even when dwim mode is used to go find products.


=== Products/Basket/basket.py 1.1 => 1.2 ===
--- Products/Basket/basket.py:1.1	Thu Nov 10 13:21:28 2005
+++ Products/Basket/basket.py	Thu Nov 10 13:58:30 2005
@@ -8,7 +8,7 @@
 import unzip
 
 import zLOG
-import App
+from App.config import getConfiguration
 
 from utils import EggProductContext
 from utils import EggProduct
@@ -21,7 +21,7 @@
     def __init__(self):
         self.pre_initialized = False
         self.tempdirs = []
-        #atexit.register(self.cleanup)
+        atexit.register(self.cleanup)
         try:
             etc = os.path.join(INSTANCE_HOME, 'etc')
         except NameError: # INSTANCE_HOME may not be available?
@@ -46,7 +46,8 @@
         # Grab app from Zope product context
         # It's a "protected" attribute, hence the name mangling
         app = context._ProductContext__app
-        debug_mode = App.config.getConfiguration().debug_mode
+
+        debug_mode = getConfiguration().debug_mode
 
         if not self.pre_initialized: # this services unit testing
             self.preinitialize()
@@ -64,7 +65,8 @@
             except:
                 zLOG.LOG('Egg Product Init', zLOG.ERROR,
                          'Problem initializing product with entry point '
-                         '"%s" in module "%s"' % (point.name,point.module_name),
+                         '"%s" in module "%s"' % (point.name,
+                                                  point.module_name),
                          error=sys.exc_info())
                 if debug_mode:
                     raise
@@ -77,6 +79,7 @@
                                         product_pkg, eggname)
             returned = context.install(debug_mode)
             data.append(returned)
+
         return data
 
     def product_distributions_by_dwim(self):
@@ -109,20 +112,23 @@
         return product_distros
 
     def preinitialize(self):
-        pdist_fname = self.pdist_fname
-        if pdist_fname and os.path.exists(pdist_fname):
+        by_require = self.pdist_fname and os.path.exists(self.pdist_fname)
+        if by_require:
             distributions = self.product_distributions_by_require()
         else:
             distributions = self.product_distributions_by_dwim()
 
+        working_set = pkg_resources.working_set
 
         for distribution in distributions:
             if is_zip_safe_distribution(distribution):
-                pkg_resources.working_set.add(distribution)
+                working_set.add(distribution)
             else:
                 # if it's not zip-safe, blast it out to a tempdir and create
                 # new distro out of the file-based egg; we delete the
                 # tempdir at system exit
+                if by_require: # these get added to the working set above
+                    remove_distribution_from_working_set(distribution)
                 tempdir = tempfile.mkdtemp()
                 eggname = os.path.basename(distribution.location)
                 eggdir = os.path.join(tempdir, eggname)
@@ -131,12 +137,6 @@
                 un = unzip.unzip()
                 un.extract(distribution.location, eggdir)
                 new_distro = pkg_resources.Distribution.from_filename(eggdir)
-                # XXX this is nasty... create an API for this
-                working_set = pkg_resources.working_set
-                working_set.entries.remove(distribution.location)
-                del working_set.by_key[distribution.key]
-                working_set.entry_keys[distribution.location] = []
-                sys.path.remove(distribution.location)
                 working_set.add(new_distro)
 
         self.pre_initialized = True
@@ -162,14 +162,13 @@
         # be imported (presumably because of a module-scope error)
         initializer = point.load()
     except:
-        exc = sys.exc_info()
         zLOG.LOG('Zope', zLOG.ERROR, 'Could not import %s' % productname,
-                 error=exc)
+                 error = sys.exc_info())
         f = StringIO()
-        traceback.print_exc(100, f)
-        product_pkg.__import_error__ = f.getvalue()
+        limit = 100 # limit to 100 stack trace entries
+        product_pkg.__import_error__ = traceback.print_exc(limit, f).getvalue()
         if debug_mode:
-            raise exc[0], exc[1], exc[2]
+            raise
     return initializer
 
 def is_product_distribution(distribution): 
@@ -184,4 +183,11 @@
 def is_zip_safe_distribution(distribution):
     return not distribution.has_metadata('not-zip-safe')
 
-
+def remove_distribution_from_working_set(distribution):
+    # XXX this is nasty... there maybe should be a pkg_resources API for this
+    # or perhaps it's just never supposed to be done
+    working_set = pkg_resources.working_set
+    working_set.entries.remove(distribution.location)
+    del working_set.by_key[distribution.key]
+    working_set.entry_keys[distribution.location] = []
+    sys.path.remove(distribution.location)



More information about the Zope-CVS mailing list