Back to index

plone3  3.1.7
setup.py
Go to the documentation of this file.
00001 #
00002 # CMFTestCase setup
00003 #
00004 
00005 # $Id: setup.py 49972 2007-09-23 14:41:30Z shh42 $
00006 
00007 from Testing import ZopeTestCase
00008 
00009 ZopeTestCase.installProduct('CMFCore')
00010 ZopeTestCase.installProduct('CMFDefault')
00011 ZopeTestCase.installProduct('CMFCalendar')
00012 ZopeTestCase.installProduct('CMFTopic')
00013 ZopeTestCase.installProduct('DCWorkflow')
00014 ZopeTestCase.installProduct('CMFUid', quiet=1)
00015 ZopeTestCase.installProduct('MailHost', quiet=1)
00016 ZopeTestCase.installProduct('ZCTextIndex', quiet=1)
00017 
00018 # Check for CMF 1.5 or above
00019 try:
00020     from Products.CMFCore import permissions
00021 except ImportError:
00022     CMF15 = 0
00023 else:
00024     CMF15 = 1
00025 
00026 # Check for CMF 1.6 or above
00027 try:
00028     from Products.CMFDefault import factory
00029 except ImportError:
00030     CMF16 = 0
00031 else:
00032     CMF16 = 1
00033     CMF15 = 1
00034 
00035 # Check for CMF 2.0 or above
00036 try:
00037     from Products.CMFDefault.utils import translate
00038 except ImportError:
00039     CMF20 = 0
00040 else:
00041     CMF20 = 1
00042     CMF16 = 1
00043     CMF15 = 1
00044 
00045 # Check for CMF 2.1 or above
00046 try:
00047     from Products.CMFDefault.utils import getBrowserCharset
00048 except ImportError:
00049     CMF21 = 0
00050 else:
00051     CMF21 = 1
00052     CMF20 = 1
00053     CMF16 = 1
00054     CMF15 = 1
00055 
00056 # Check for layer support
00057 try:
00058     import zope.testing.testrunner
00059 except ImportError:
00060     USELAYER = 0
00061 else:
00062     USELAYER = 1
00063 
00064 # Check for Zope3 interfaces
00065 try:
00066     from zope.interface.interfaces import IInterface
00067 except ImportError:
00068     Z3INTERFACES = 0
00069 else:
00070     from interfaces import ICMFTestCase
00071     Z3INTERFACES = IInterface.providedBy(ICMFTestCase)
00072 
00073 # BBB: Zope 2.8
00074 if CMF16 and not USELAYER:
00075     ZopeTestCase.installProduct('Five')
00076 
00077 from Testing.ZopeTestCase import transaction
00078 from AccessControl.SecurityManagement import newSecurityManager
00079 from AccessControl.SecurityManagement import noSecurityManager
00080 from Acquisition import aq_base
00081 from time import time
00082 from Globals import PersistentMapping
00083 
00084 portal_name = 'cmf'
00085 portal_owner = 'portal_owner'
00086 default_products = ()
00087 default_user = ZopeTestCase.user_name
00088 default_password = ZopeTestCase.user_password
00089 
00090 default_base_profile = 'CMFDefault:default'
00091 default_extension_profiles = ()
00092 
00093 if CMF21:
00094     default_base_profile = 'Products.CMFDefault:default'
00095 
00096 
00097 def setupCMFSite(id=portal_name,
00098                  products=default_products,
00099                  quiet=0,
00100                  base_profile=default_base_profile,
00101                  extension_profiles=default_extension_profiles):
00102     '''Creates a CMF site and/or installs products into it.'''
00103     if USELAYER:
00104         quiet = 1
00105         cleanupCMFSite(id)
00106     SiteSetup(id, products, quiet, base_profile, extension_profiles).run()
00107 
00108 if USELAYER:
00109     import layer
00110     setupCMFSite = layer.onsetup(setupCMFSite)
00111 
00112 
00113 def cleanupCMFSite(id):
00114     '''Removes a site.'''
00115     SiteCleanup(id).run()
00116 
00117 if USELAYER:
00118     import layer
00119     cleanupCMFSite = layer.onteardown(cleanupCMFSite)
00120 
00121 
00122 class SiteSetup:
00123     '''Creates a CMF site and/or installs products into it.'''
00124 
00125     def __init__(self, id, products, quiet, base_profile, extension_profiles):
00126         self.id = id
00127         self.products = products
00128         self.quiet = quiet
00129         self.base_profile = base_profile
00130         self.extension_profiles = tuple(extension_profiles)
00131 
00132     def run(self):
00133         self.app = self._app()
00134         try:
00135             uf = self.app.acl_users
00136             if uf.getUserById(portal_owner) is None:
00137                 # Add portal owner
00138                 uf.userFolderAddUser(portal_owner, default_password, ['Manager'], [])
00139             if not hasattr(aq_base(self.app), self.id):
00140                 # Add site
00141                 self._login(uf, portal_owner)
00142                 self._optimize()
00143                 self._setupCMFSite()
00144                 self._setupRegistries()
00145             if hasattr(aq_base(self.app), self.id):
00146                 # Configure site
00147                 self._login(uf, portal_owner)
00148                 self._placefulSetUp()
00149                 self._setupProfiles()
00150                 self._setupProducts()
00151         finally:
00152             self._abort()
00153             self._close()
00154             self._logout()
00155             self._placefulTearDown()
00156 
00157     def _setupCMFSite(self):
00158         '''Creates the CMF site.'''
00159         if CMF16:
00160             self._setupCMFSite_with_genericsetup()
00161         else:
00162             self._setupCMFSite_with_portalgenerator()
00163 
00164     def _setupCMFSite_with_genericsetup(self):
00165         '''Creates the site using GenericSetup.'''
00166         start = time()
00167         if self.base_profile != default_base_profile:
00168             self._print('Adding CMF Site (%s) ... ' % (self.base_profile,))
00169         else:
00170             self._print('Adding CMF Site ... ')
00171         factory.addConfiguredSite(self.app, self.id, snapshot=0,
00172                                   profile_id=self.base_profile)
00173         self._commit()
00174         self._print('done (%.3fs)\n' % (time()-start,))
00175 
00176     def _setupCMFSite_with_portalgenerator(self):
00177         '''Creates the site using PortalGenerator.'''
00178         start = time()
00179         self._print('Adding CMF Site ... ')
00180         from Products.CMFDefault.Portal import manage_addCMFSite
00181         manage_addCMFSite(self.app, self.id, create_userfolder=1)
00182         self._commit()
00183         self._print('done (%.3fs)\n' % (time()-start,))
00184 
00185     def _setupRegistries(self):
00186         '''Installs persistent registries.'''
00187         portal = getattr(self.app, self.id)
00188         if not hasattr(portal, '_installed_profiles'):
00189             portal._installed_profiles = PersistentMapping()
00190             self._commit()
00191         if not hasattr(portal, '_installed_products'):
00192             portal._installed_products = PersistentMapping()
00193             self._commit()
00194 
00195     def _setupProfiles(self):
00196         '''Imports extension profiles into the site.'''
00197         portal = getattr(self.app, self.id)
00198         setup = getattr(portal, 'portal_setup', None)
00199         if setup is not None:
00200             for profile in self.extension_profiles:
00201                 if not portal._installed_profiles.has_key(profile):
00202                     start = time()
00203                     self._print('Adding %s ... ' % (profile,))
00204                     profile_id = 'profile-%s' % (profile,)
00205                     if CMF21:
00206                         setup.runAllImportStepsFromProfile(profile_id)
00207                     else:
00208                         saved = setup.getImportContextID()
00209                         try:
00210                             setup.setImportContext(profile_id)
00211                             setup.runAllImportSteps()
00212                         finally:
00213                             setup.setImportContext(saved)
00214                     portal._installed_profiles[profile] = 1
00215                     self._commit()
00216                     self._print('done (%.3fs)\n' % (time()-start,))
00217 
00218     def _setupProducts(self):
00219         '''Installs products into the site.'''
00220         portal = getattr(self.app, self.id)
00221         for product in self.products:
00222             if not portal._installed_products.has_key(product):
00223                 start = time()
00224                 self._print('Adding %s ... ' % (product,))
00225                 exec 'from Products.%s.Extensions.Install import install' % (product,)
00226                 install(portal)
00227                 portal._installed_products[product] = 1
00228                 self._commit()
00229                 self._print('done (%.3fs)\n' % (time()-start,))
00230 
00231     def _placefulSetUp(self):
00232         '''Sets the local site/manager.'''
00233         if CMF21:
00234             portal = getattr(self.app, self.id)
00235             _placefulSetUp(portal)
00236 
00237     def _placefulTearDown(self):
00238         '''Resets the local site/manager.'''
00239         if CMF21:
00240             _placefulTearDown()
00241 
00242     def _optimize(self):
00243         '''Applies optimizations to the PortalGenerator.'''
00244         _optimize()
00245 
00246     def _app(self):
00247         '''Opens a ZODB connection and returns the app object.'''
00248         return ZopeTestCase.app()
00249 
00250     def _close(self):
00251         '''Closes the ZODB connection.'''
00252         ZopeTestCase.close(self.app)
00253 
00254     def _login(self, uf, name):
00255         '''Logs in as user 'name' from user folder 'uf'.'''
00256         user = uf.getUserById(name).__of__(uf)
00257         newSecurityManager(None, user)
00258 
00259     def _logout(self):
00260         '''Logs out.'''
00261         noSecurityManager()
00262 
00263     def _commit(self):
00264         '''Commits the transaction.'''
00265         transaction.commit()
00266 
00267     def _abort(self):
00268         '''Aborts the transaction.'''
00269         transaction.abort()
00270 
00271     def _print(self, msg):
00272         '''Prints msg to stderr.'''
00273         if not self.quiet:
00274             ZopeTestCase._print(msg)
00275 
00276 
00277 class SiteCleanup(SiteSetup):
00278     '''Removes a site.'''
00279 
00280     def __init__(self, id):
00281         self.id = id
00282 
00283     def run(self):
00284         self.app = self._app()
00285         try:
00286             if hasattr(aq_base(self.app), self.id):
00287                 self._placefulSetUp()
00288                 self.app._delObject(self.id)
00289                 self._commit()
00290         finally:
00291             self._abort()
00292             self._close()
00293             self._placefulTearDown()
00294 
00295 
00296 def _placefulSetUp(portal):
00297     '''Sets the local site/manager.'''
00298     from zope.app.component.hooks import setHooks, setSite
00299     from zope.component.interfaces import ComponentLookupError
00300     setHooks()
00301     try:
00302         setSite(portal)
00303     except ComponentLookupError:
00304         pass
00305 
00306 
00307 def _placefulTearDown():
00308     '''Resets the local site/manager.'''
00309     from zope.app.component.hooks import resetHooks, setSite
00310     resetHooks()
00311     setSite()
00312 
00313 
00314 def _optimize():
00315     '''Reduces portal creation time.'''
00316     # Don't compile expressions on creation
00317     def __init__(self, text):
00318         self.text = text
00319     from Products.CMFCore.Expression import Expression
00320     Expression.__init__ = __init__
00321     # Don't clone actions but convert to list only
00322     def _cloneActions(self):
00323         return list(self._actions)
00324     from Products.CMFCore.ActionProviderBase import ActionProviderBase
00325     ActionProviderBase._cloneActions = _cloneActions
00326     # The site creation code is not needed anymore in CMF >= 1.6
00327     # as it is now based on GenericSetup
00328     if not CMF16:
00329         # Don't setup 'index_html' in Members folder
00330         def setupMembersFolder(self, p):
00331             p.manage_addPortalFolder('Members')
00332         from Products.CMFDefault.Portal import PortalGenerator
00333         PortalGenerator.setupMembersFolder = setupMembersFolder
00334