Back to index

plone3  3.1.7
Install.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
00004 #
00005 # This software is distributed under the terms of the Kupu
00006 # License. See LICENSE.txt for license text. For a list of Kupu
00007 # Contributors see CREDITS.txt.
00008 #
00009 ##############################################################################
00010 """Install kupu in CMF and, if available, Plone
00011 
00012 This is best executed using CMFQuickInstaller
00013 
00014 $Id: Install.py 39571 2007-02-28 13:34:03Z fschulze $
00015 """
00016 import os.path
00017 import sys
00018 import re
00019 from StringIO import StringIO
00020 
00021 from App.Common import package_home
00022 
00023 from Products.CMFCore.utils import getToolByName, minimalpath
00024 from Products.CMFCore.DirectoryView import createDirectoryView
00025 from Products.kupu import kupu_globals
00026 from Products.kupu.plone.util import register_layer, unregister_layers
00027 from Products.kupu.plone import util
00028 from Products.kupu.config import TOOLNAME, PROJECTNAME, TOOLTITLE
00029 from OFS.ObjectManager import BadRequestException
00030 from zExceptions import BadRequest
00031 
00032 try:
00033     from Products.MimetypesRegistry import MimeTypeItem
00034 except ImportError:
00035     pass # Plone not available
00036 
00037 kupu_package_dir = package_home(kupu_globals)
00038 
00039 def install_plone(self, out):
00040     """Install with plone
00041     """
00042     # register the plone skin layers
00043     register_layer(self, 'plone/kupu_plone_layer', 'kupu_plone', out)
00044     # By default, add the directory view but not the skin layer for the reference browser
00045     register_layer(self, 'plone/kupu_references', 'kupu_references', out, add=False)
00046     register_layer(self, 'tests', 'kupu_tests', out)
00047 
00048     # register as editor
00049     portal_props = getToolByName(self, 'portal_properties')
00050     site_props = getattr(portal_props,'site_properties', None)
00051     attrname = 'available_editors'
00052     if site_props is not None:
00053         editors = list(site_props.getProperty(attrname)) 
00054         if 'Kupu' not in editors:
00055             editors.append('Kupu')
00056             site_props._updateProperty(attrname, editors)        
00057             print >>out, "Added 'Kupu' to available editors in Plone."
00058     install_libraries(self, out)
00059     install_configlet(self, out)
00060     install_transform(self, out)
00061     install_resources(self, out)
00062     install_customisation(self, out)
00063 
00064 def _read_resources():
00065     resourcefile = open(os.path.join(kupu_package_dir, 'plone', 'head.kupu'), 'r')
00066     try:
00067         data = resourcefile.read()
00068         return data
00069     finally:
00070         resourcefile.close()
00071 
00072 def css_files(resources):
00073     CSSPAT = re.compile(r'<link [^>]*rel="stylesheet"[^>]*\${portal_url}/([^"]*)"')
00074     for m in CSSPAT.finditer(resources):
00075         id = m.group(1)
00076         yield id
00077 
00078 def js_files(resources):
00079     JSPAT = re.compile(r'<script [^>]*\${portal_url}/([^"]*)"')
00080     for m in JSPAT.finditer(resources):
00081         id = m.group(1)
00082         if id=='sarissa.js':
00083             continue
00084         yield id
00085 
00086 def install_resources(self, out):
00087     """Add the js and css files to the resource registry so that
00088     they can be merged for download.
00089     """
00090     try:
00091         from Products.ResourceRegistries.config import CSSTOOLNAME, JSTOOLNAME
00092     except ImportError:
00093         print >>out, "Resource registry not found: kupu will load its own resources"
00094         return
00095 
00096     data = _read_resources()
00097     
00098     CONDITION = '''python:portal.kupu_library_tool.isKupuEnabled(REQUEST=request)'''
00099     csstool = getToolByName(self, CSSTOOLNAME, None)
00100     jstool = getToolByName(self, JSTOOLNAME, None)
00101     if csstool is None or jstool is None:
00102         return
00103 
00104     for id in css_files(data):
00105         print >>out, "CSS file", id
00106         cookable = True
00107         csstool.manage_removeStylesheet(id=id)
00108         csstool.manage_addStylesheet(id=id,
00109             expression=CONDITION,
00110             rel='stylesheet',
00111             enabled=True,
00112             cookable=cookable)
00113 
00114     existing = [ sheet.getId() for sheet in jstool.getResources()]
00115     if 'kupucontextmenu.js' in existing:
00116         jstool.manage_removeScript('kupucontextmenu.js');
00117     # Insert sarissa.js into the scripts but only if it isn't already
00118     # there.
00119     SARISSA = 'sarissa.js'
00120     if SARISSA not in existing:
00121         jstool.manage_addScript(id=SARISSA, enabled=True,
00122             cookable=True,
00123             compression='safe',
00124             cacheable=True)
00125         if 'plone_javascripts.js' in existing:
00126             jstool.moveResourceAfter(SARISSA, 'plone_javascripts.js')
00127         else:
00128             jstool.moveResourceToBottom(SARISSA)
00129         print >>out, "JS file", SARISSA
00130 
00131     for id in js_files(data):
00132         print >>out, "JS file", id
00133         jstool.manage_removeScript(id=id)
00134         jstool.manage_addScript(id=id,
00135             expression=CONDITION,
00136             enabled=True,
00137             compression='safe',
00138             cookable=True)
00139 
00140 def uninstall_resources(self, out):
00141     """Remove the js and css files from the resource registries"""
00142     try:
00143         from Products.ResourceRegistries.config import CSSTOOLNAME, JSTOOLNAME
00144     except ImportError:
00145         return
00146 
00147     data = _read_resources()
00148     
00149     csstool = getToolByName(self, CSSTOOLNAME)
00150     jstool = getToolByName(self, JSTOOLNAME)
00151 
00152     for id in css_files(data):
00153         csstool.manage_removeStylesheet(id=id)
00154 
00155     for id in js_files(data):
00156         jstool.manage_removeScript(id=id)
00157     print >>out, "Resource files removed"
00158     
00159 def install_libraries(self, out):
00160     """Install everything necessary to support Kupu Libraries
00161     """
00162     # add the library tool
00163     addTool = self.manage_addProduct['kupu'].manage_addTool
00164     try:
00165         addTool('Kupu Library Tool')
00166         print >>out, "Added the Kupu Library Tool to the plone Site"
00167     except BadRequest:
00168         print >>out, "Kupu library Tool already added"    
00169     except: # Older Zopes
00170         #heuristics for testing if an instance with the same name already exists
00171         #only this error will be swallowed.
00172         #Zope raises in an unelegant manner a 'Bad Request' error
00173         e=sys.exc_info()
00174         if e[0] != 'Bad Request':
00175             raise
00176         print >>out, "Kupu library Tool already added"    
00177 
00178 def install_configlet(self, out):
00179     try:
00180         portal_conf=getToolByName(self,'portal_controlpanel')
00181     except AttributeError:
00182         print >>out, "Configlet could not be installed"
00183         return
00184     try:
00185         portal_conf.registerConfiglet( 'kupu'
00186                , TOOLTITLE
00187                , 'string:${portal_url}/%s/kupu_config' % TOOLNAME
00188                , ''                 # a condition   
00189                , 'Manage portal'    # access permission
00190                , 'Plone'            # category to which kupu should be added: 
00191                                     # (Plone,Products,Members) 
00192                , 1                  # visibility
00193                , PROJECTNAME
00194                , 'kupuimages/kupu_icon.gif' # icon in control_panel
00195                , 'Kupu Library Tool'
00196                , None
00197                )
00198     except KeyError:
00199         pass # Get KeyError when registering duplicate configlet.
00200 
00201 def install_transform(self, out):
00202     try:
00203         util.install_transform(self)
00204         util.remove_transform(self) # Transform is installed but disabled by default.
00205     except (NameError,AttributeError):
00206         print >>out, "Transform not installed."
00207 
00208 def install_customisation(self, out):
00209     """Default settings may be stored in a customisation policy script so
00210     that the entire setup may be 'productised'"""
00211 
00212     # Skins are cached during the request so (in case new skin
00213     # folders have just been added) we need to force a refresh of the
00214     # skin.
00215     self.changeSkin(None)
00216 
00217     scriptname = '%s-customisation-policy' % PROJECTNAME.lower()
00218     cpscript = getattr(self, scriptname, None)
00219     # If the user hasn't created a CP then use the sample.
00220     if not cpscript:
00221         cpscript = getattr(self, 'sample-kupu-customisation-policy', None)
00222     if cpscript:
00223         cpscript = cpscript.__of__(self)
00224 
00225     if cpscript:
00226         print >>out,"Customising %s" % PROJECTNAME
00227         print >>out,cpscript()
00228     else:
00229         print >>out,"No customisation policy"
00230 
00231 def install(self):
00232     out = StringIO()
00233 
00234     # register the core layer
00235     register_layer(self, 'common', 'kupu', out)
00236 
00237     # try for plone
00238     try:
00239         import Products.CMFPlone
00240     except ImportError:
00241         pass
00242     else:
00243         install_plone(self, out)
00244 
00245     print >>out, "kupu successfully installed"
00246     return out.getvalue()
00247 
00248 def uninstall_transform(self, out):
00249     transform_tool = getToolByName(self, 'portal_transforms')
00250     try:
00251         util.remove_transform(self)
00252         transform_tool.manage_delObjects(['html-to-captioned', 'captioned-to-html'])
00253     except:
00254         print >>out, "Transform not removed"
00255         pass
00256     else:
00257         print >>out, "Transform removed"
00258 
00259 def uninstall_tool(self, out):
00260     try:
00261         self.manage_delObjects([TOOLNAME])
00262     except:
00263         pass
00264     else:
00265         print >>out, "Kupu tool removed"
00266 
00267 def uninstall(self):
00268     out = StringIO()
00269 
00270     # remove the configlet from the portal control panel
00271     configTool = getToolByName(self, 'portal_controlpanel', None)
00272     if configTool:
00273         configTool.unregisterConfiglet('kupu')
00274         out.write('Removed kupu configlet\n')
00275 
00276     uninstall_transform(self, out)
00277     uninstall_tool(self, out)
00278     uninstall_resources(self, out)
00279     unregister_layers(self, ['kupu_plone', 'kupu_references', 'kupu'], out)
00280 
00281     print >> out, "Successfully uninstalled %s." % PROJECTNAME
00282     return out.getvalue()