Back to index

plone3  3.1.7
migrateToCMF21.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2007 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ migrateToCMF21.py: Migrate older instances to CMF 2.1
00014 
00015 This script must be executed using "zopectl run".
00016 
00017 $Id: migrateToCMF21.py 77760 2007-07-12 15:45:56Z wichert $
00018 """
00019 
00020 from zope.component import getSiteManager
00021 from zope.app.component.hooks import setSite
00022 from zope.dottedname.resolve import resolve
00023 from logging import getLogger
00024 import sys
00025 import transaction
00026 
00027 logger = getLogger('event.migrateToCMF21')
00028 
00029 # The following extensions have a componentregistry.xml import step, and
00030 # the existence of the ID they're mapped to in the portal means the import
00031 # step must be run
00032 AFFECTED_EXTENSIONS = { 
00033       'Products.CMFCalendar:default' : 'portal_calendar'
00034     , 'Products.CMFUid:default' : 'portal_uidhandler'
00035     , 'Products.CMFActionIcons:actionicons' : 'portal_actionicons'
00036     }
00037 
00038 # These are utilities that were registered in CMF 2.1 pre-releases but
00039 # are no longer present.
00040 BAD_UTILITIES = [
00041     'Products.CMFCalendar.interfaces.ICalendarTool',
00042     'Products.CMFCore.interfaces.IActionsTool',
00043     'Products.CMFCore.interfaces.ICatalogTool',
00044     'Products.CMFCore.interfaces.IContentTypeRegistry',
00045     'Products.CMFCore.interfaces.ISkinsTool',
00046     'Products.CMFCore.interfaces.ITypesTool',
00047     'Products.CMFCore.interfaces.IURLTool',
00048     'Products.CMFCore.interfaces.IConfigurableWorkflowTool',
00049     'Products.CMFCore.interfaces.IMembershipTool',
00050     'Products.CMFCore.interfaces.IMetadataTool',
00051     'Products.CMFCore.interfaces.IRegistrationTool',
00052     ]
00053 
00054 def _log(msg):
00055     logger.info(msg)
00056     print msg
00057 
00058 
00059 def migrate_site(site):
00060     """ Migrate a single site
00061     """
00062 
00063     site_path = '/'.join(site.getPhysicalPath())
00064     _log(' - converting site at %s' % site_path)
00065     ps = site.portal_setup
00066 
00067     # We have to call setSite to make sure we have a site with a proper
00068     # acquisition context.
00069     setSite(site)
00070 
00071     # First we remove utility registrations that are no longer
00072     # needed.
00073     sm = getSiteManager(site)
00074     for util in BAD_UTILITIES:
00075         iface = resolve(util)
00076         if sm.queryUtility(iface) is not None:
00077             sm.unregisterUtility(provided=iface)
00078 
00079     # Next we need to run items from the default CMF Site profile
00080 
00081     # Check if we have new-style action providers, if not we need to
00082     # run the action provider step from CMFDefault:default as well
00083     if not site.portal_actions.objectIds(['CMF Action Category']):
00084         steps = ('actions',) # Runs componentregistry as dependency
00085     else:
00086         steps = ('componentregistry',)
00087 
00088     for step in steps:
00089         ps.runImportStepFromProfile('profile-Products.CMFDefault:default',
00090                 step, run_dependencies=True)
00091 
00092     # Now we go through the extensions that may need to be run
00093     for extension_id, object_id in AFFECTED_EXTENSIONS.items():
00094         if object_id in site.objectIds():
00095             ps.runImportStepFromProfile('profile-' + extension_id,
00096                     'componentregistry', run_dependencies=True)
00097 
00098     _log(' - finished converting site at %s' % site_path)
00099 
00100 
00101 if __name__ == '__main__':
00102 
00103     _log('Starting CMF migration.')
00104     
00105     # First step: Find all instances of CMFSite
00106     sites = app.ZopeFind(app, obj_metatypes=['CMF Site'], search_sub=1)
00107 
00108     if len(sites) == 0:
00109         _log('No CMF Site objects found - aborting.')
00110         sys.exit(1)
00111 
00112     # For every site, grab the portal_setup tool and run the required steps
00113     for site_id, site in sites:
00114         migrate_site(site)
00115         transaction.get().note('Migrated CMF site "%s"' % site_id)
00116         transaction.commit()
00117 
00118     _log('CMF migration finished.')
00119