Annotation of kupu/Extensions/Install.py, revision 1.1.1.1
1.1 dwinter 1: ##############################################################################
2: #
3: # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
4: #
5: # This software is distributed under the terms of the Kupu
6: # License. See LICENSE.txt for license text. For a list of Kupu
7: # Contributors see CREDITS.txt.
8: #
9: ##############################################################################
10: """Install kupu in CMF and, if available, Plone
11:
12: This is best executed using CMFQuickInstaller
13:
14: $Id: Install.py 14575 2005-07-12 20:18:12Z duncan $
15: """
16: import os.path
17: import sys
18: import re
19: from StringIO import StringIO
20:
21: from App.Common import package_home
22:
23: from Products.CMFCore.utils import getToolByName, minimalpath
24: from Products.CMFCore.DirectoryView import createDirectoryView
25: from Products.kupu import kupu_globals
26: from Products.kupu.config import TOOLNAME, PROJECTNAME, TOOLTITLE
27: from OFS.ObjectManager import BadRequestException
28: from zExceptions import BadRequest
29:
30: try:
31: from Products.MimetypesRegistry import MimeTypeItem
32: except ImportError:
33: pass # Plone not available
34:
35: kupu_package_dir = package_home(kupu_globals)
36:
37: def register_layer(self, relpath, name, out):
38: """Register a file system directory as skin layer
39: """
40: print >>out, "register skin layers"
41: skinstool = getToolByName(self, 'portal_skins')
42: if name not in skinstool.objectIds():
43: kupu_plone_skin_dir = minimalpath(os.path.join(kupu_package_dir, relpath))
44: createDirectoryView(skinstool, kupu_plone_skin_dir, name)
45: print >>out, "The layer '%s' was added to the skins tool" % name
46:
47: # put this layer into all known skins
48: for skinName in skinstool.getSkinSelections():
49: path = skinstool.getSkinPath(skinName)
50: path = [i.strip() for i in path.split(',')]
51: try:
52: if name not in path:
53: path.insert(path.index('custom')+1, name)
54: except ValueError:
55: if name not in path:
56: path.append(name)
57:
58: path = ','.join(path)
59: skinstool.addSkinSelection(skinName, path)
60:
61: def install_plone(self, out):
62: """Install with plone
63: """
64: # register the plone skin layer
65: register_layer(self, 'plone/kupu_plone_layer', 'kupu_plone', out)
66:
67: # register as editor
68: portal_props = getToolByName(self, 'portal_properties')
69: site_props = getattr(portal_props,'site_properties', None)
70: attrname = 'available_editors'
71: if site_props is not None:
72: editors = list(site_props.getProperty(attrname))
73: if 'Kupu' not in editors:
74: editors.append('Kupu')
75: site_props._updateProperty(attrname, editors)
76: print >>out, "Added 'Kupu' to available editors in Plone."
77: install_libraries(self, out)
78: install_configlet(self, out)
79: install_transform(self, out)
80: install_resources(self, out)
81: install_customisation(self, out)
82:
83: def _read_resources():
84: resourcefile = open(os.path.join(kupu_package_dir, 'plone', 'head.kupu'), 'r')
85: try:
86: data = resourcefile.read()
87: return data
88: finally:
89: resourcefile.close()
90:
91: def css_files(resources):
92: CSSPAT = re.compile(r'\<link [^>]*rel="stylesheet"[^>]*\${portal_url}/([^"]*)"')
93: for m in CSSPAT.finditer(resources):
94: id = m.group(1)
95: yield id
96:
97: def js_files(resources):
98: JSPAT = re.compile(r'\<script [^>]*\${portal_url}/([^"]*)"')
99: for m in JSPAT.finditer(resources):
100: id = m.group(1)
101: if id=='sarissa.js':
102: continue
103: yield id
104:
105: def install_resources(self, out):
106: """Add the js and css files to the resource registry so that
107: they can be merged for download.
108: """
109: try:
110: from Products.ResourceRegistries.config import CSSTOOLNAME, JSTOOLNAME
111: except ImportError:
112: print >>out, "Resource registry not found: kupu will load its own resources"
113: return
114:
115: data = _read_resources()
116:
117: CONDITION = '''python:portal.kupu_library_tool.isKupuEnabled(REQUEST=request)'''
118: csstool = getToolByName(self, CSSTOOLNAME)
119: jstool = getToolByName(self, JSTOOLNAME)
120:
121: for id in css_files(data):
122: print >>out, "CSS file", id
123: csstool.manage_removeStylesheet(id=id)
124: csstool.manage_addStylesheet(id=id,
125: expression=CONDITION,
126: rel='stylesheet',
127: enabled=True,
128: cookable=True)
129:
130: for id in js_files(data):
131: print >>out, "JS file", id
132: jstool.manage_removeScript(id=id)
133: jstool.manage_addScript(id=id,
134: expression=CONDITION,
135: enabled=True,
136: cookable=True)
137:
138: def uninstall_resources(self, out):
139: """Remove the js and css files from the resource registries"""
140: try:
141: from Products.ResourceRegistries.config import CSSTOOLNAME, JSTOOLNAME
142: except ImportError:
143: return
144:
145: data = _read_resources()
146:
147: csstool = getToolByName(self, CSSTOOLNAME)
148: jstool = getToolByName(self, JSTOOLNAME)
149:
150: for id in css_files(data):
151: csstool.manage_removeStylesheet(id=id)
152:
153: for id in js_files(data):
154: jstool.manage_removeScript(id=id)
155: print >>out, "Resource files removed"
156:
157: def install_libraries(self, out):
158: """Install everything necessary to support Kupu Libraries
159: """
160: # add the library tool
161: addTool = self.manage_addProduct['kupu'].manage_addTool
162: try:
163: addTool('Kupu Library Tool')
164: print >>out, "Added the Kupu Library Tool to the plone Site"
165: except BadRequest:
166: print >>out, "Kupu library Tool already added"
167: except: # Older Zopes
168: #heuristics for testing if an instance with the same name already exists
169: #only this error will be swallowed.
170: #Zope raises in an unelegant manner a 'Bad Request' error
171: e=sys.exc_info()
172: if e[0] != 'Bad Request':
173: raise
174: print >>out, "Kupu library Tool already added"
175:
176: def install_configlet(self, out):
177: try:
178: portal_conf=getToolByName(self,'portal_controlpanel')
179: except AttributeError:
180: print >>out, "Configlet could not be installed"
181: return
182: try:
183: portal_conf.registerConfiglet( 'kupu'
184: , TOOLTITLE
185: , 'string:${portal_url}/%s/kupu_config' % TOOLNAME
186: , '' # a condition
187: , 'Manage portal' # access permission
188: , 'Products' # section to which the configlet should be added:
189: #(Plone,Products,Members)
190: , 1 # visibility
191: , PROJECTNAME
192: , 'kupuimages/kupu_icon.gif' # icon in control_panel
193: , 'Kupu Library Tool'
194: , None
195: )
196: except KeyError:
197: pass # Get KeyError when registering duplicate configlet.
198:
199: def install_transform(self, out):
200: try:
201: print >>out, "Adding new mimetype"
202: mimetypes_tool = getToolByName(self, 'mimetypes_registry')
203: newtype = MimeTypeItem.MimeTypeItem('HTML with captioned images',
204: ('text/x-html-captioned',), ('html-captioned',), 0)
205: mimetypes_tool.register(newtype)
206:
207: print >>out,"Add transform"
208: transform_tool = getToolByName(self, 'portal_transforms')
209: try:
210: transform_tool.manage_delObjects(['html-to-captioned'])
211: except: # XXX: get rid of bare except
212: pass
213: transform_tool.manage_addTransform('html-to-captioned', 'Products.kupu.plone.html2captioned')
214: except (NameError,AttributeError):
215: print >>out, "No MimetypesRegistry, captioning not supported."
216:
217: def install_customisation(self, out):
218: """Default settings may be stored in a customisation policy script so
219: that the entire setup may be 'productised'"""
220:
221: # Skins are cached during the request so we (in case new skin
222: # folders have just been added) we need to force a refresh of the
223: # skin.
224: self.changeSkin(None)
225:
226: scriptname = '%s-customisation-policy' % PROJECTNAME.lower()
227: cpscript = getattr(self, scriptname, None)
228: if cpscript:
229: cpscript = cpscript.__of__(self)
230:
231: if cpscript:
232: print >>out,"Customising %s" % PROJECTNAME
233: print >>out,cpscript()
234: else:
235: print >>out,"No customisation policy"
236:
237: def install(self):
238: out = StringIO()
239:
240: # register the core layer
241: register_layer(self, 'common', 'kupu', out)
242:
243: # try for plone
244: try:
245: import Products.CMFPlone
246: except ImportError:
247: pass
248: else:
249: install_plone(self, out)
250:
251: print >>out, "kupu successfully installed"
252: return out.getvalue()
253:
254: def uninstall_transform(self, out):
255: transform_tool = getToolByName(self, 'portal_transforms')
256: try:
257: transform_tool.manage_delObjects(['html-to-captioned'])
258: except:
259: pass
260: else:
261: print >>out, "Transform removed"
262:
263: def uninstall_tool(self, out):
264: try:
265: self.manage_delObjects([TOOLNAME])
266: except:
267: pass
268: else:
269: print >>out, "Kupu tool removed"
270:
271: def uninstall(self):
272: out = StringIO()
273:
274: # remove the configlet from the portal control panel
275: configTool = getToolByName(self, 'portal_controlpanel', None)
276: if configTool:
277: configTool.unregisterConfiglet('kupu')
278: out.write('Removed kupu configlet\n')
279:
280: uninstall_transform(self, out)
281: uninstall_tool(self, out)
282: uninstall_resources(self, out)
283:
284: print >> out, "Successfully uninstalled %s." % PROJECTNAME
285: return out.getvalue()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>