Back to index

plone3  3.1.7
migration_util.py
Go to the documentation of this file.
00001 from types import ListType, TupleType
00002 from Acquisition import aq_base
00003 import logging
00004 from Products.CMFCore.utils import getToolByName
00005 from Products.CMFPlone.MemberDataTool import MemberDataTool
00006 
00007 from Products.PlonePAS.tools.memberdata \
00008         import MemberDataTool as PASMemberDataTool
00009 
00010 _marker = []
00011 
00012 def safeEditProperty(obj, key, value, data_type='string'):
00013     """ An add or edit function, surprisingly useful :) """
00014     if obj.hasProperty(key):
00015         obj._updateProperty(key, value)
00016     else:
00017         obj._setProperty(key, value, data_type)
00018 
00019 def safeGetMemberDataTool(portal):
00020     memberdata = getToolByName(portal, 'portal_memberdata', None)
00021     if memberdata is not None:
00022         if memberdata.__class__ == MemberDataTool or \
00023                 memberdata.__class__ == PASMemberDataTool:
00024             return memberdata
00025 
00026 def addLinesToProperty(obj, key, values):
00027     if obj.hasProperty(key):
00028         data = getattr(obj, key)
00029         if type(data) is TupleType:
00030             data = list(data)
00031         if type(values) is ListType:
00032             data.extend(values)
00033         else:
00034             data.append(values)
00035         obj._updateProperty(key, data)
00036     else:
00037         if type(values) is not ListType:
00038             values = [values]
00039         obj._setProperty(key, values, 'lines')
00040 
00041 def saveCloneActions(actionprovider):
00042     try:
00043         return True, actionprovider._cloneActions()
00044     except AttributeError:
00045         # Stumbled across ancient dictionary actions
00046         if not hasattr(aq_base(actionprovider), '_convertActions'):
00047             return False, ('Can\'t convert actions of %s! Jumping to next action.' % actionprovider.getId(), logging.ERROR)
00048         else:
00049             actionprovider._convertActions()
00050             return True, actionprovider._cloneActions()
00051 
00052 def testSkinLayer(skinsTool, layer):
00053     """Make sure a skin layer exists"""
00054     # code adapted from CMFCore.SkinsContainer.getSkinByPath
00055     ob = aq_base(skinsTool)
00056     for name in layer.strip().split('/'):
00057         if not name:
00058             continue
00059         if name.startswith('_'):
00060             return 0
00061         ob = getattr(ob, name, None)
00062         if ob is None:
00063             return 0
00064     return 1
00065 
00066 def cleanupSkinPath(portal, skinName, test=1):
00067    """Remove invalid skin layers from skins"""
00068    skinstool = getToolByName(portal, 'portal_skins')
00069    selections = skinstool._getSelections()
00070    old_path = selections[skinName].split(',')
00071    new_path = []
00072    for layer in old_path:
00073       if layer and testSkinLayer(skinstool, layer):
00074          new_path.append(layer)
00075    skinstool.addSkinSelection(skinName, ','.join(new_path), test=test)
00076 
00077 def installOrReinstallProduct(portal, product_name, out, hidden=False):
00078     """Installs a product
00079 
00080     If product is already installed test if it needs to be reinstalled. Also
00081     fix skins after reinstalling
00082     """
00083     qi = getToolByName(portal, 'portal_quickinstaller')
00084     if not qi.isProductInstalled(product_name):
00085         qi.installProduct(product_name, hidden=hidden)
00086         # Refresh skins
00087         if getattr(aq_base(portal), '_v_skindata', None) is not None:
00088             portal._v_skindata = None
00089         if getattr(aq_base(portal), 'setupCurrentSkin', None) is not None:
00090             portal.setupCurrentSkin()
00091         out.append('Installed %s.' % product_name)
00092     else:
00093         info = qi._getOb(product_name)
00094         installed_version = info.getInstalledVersion()
00095         product_version = qi.getProductVersion(product_name)
00096         if installed_version != product_version:
00097             qi.reinstallProducts([product_name])
00098             out.append('%s is out of date (installed: %s/ filesystem: %s), '\
00099                 'reinstalled.' % (product_name, installed_version, product_version))
00100         else:
00101             out.append('%s already installed.' % product_name)
00102 
00103 
00104 def loadMigrationProfile(portal, profile, steps=_marker):
00105     tool = getToolByName(portal, "portal_setup")
00106     if steps is _marker:
00107         tool.runAllImportStepsFromProfile(profile, purge_old=False)
00108     else:
00109         for step in steps:
00110             tool.runImportStepFromProfile(profile,
00111                                           step,
00112                                           run_dependencies=False,
00113                                           purge_old=False)