1
2
3
4
5
6
7
8
9
10
11
12
13 import zope.interface
14
15 from Globals import InitializeClass
16 from Products.ZenModel.ZenModelRM import ZenModelRM
17 from Products.ZenModel import interfaces
18 from Products.ZenRelations.RelSchema import *
19 from Products.ZenUtils.Utils import importClass, zenPath
20 from Products.ZenModel.migrate import Migrate
21 from Products.ZenUtils.Version import getVersionTupleFromString
22 from Products.ZenModel.migrate.Migrate import Version
23 from Products.ZenModel.ZenPackLoader import *
24
25 import transaction
26
27 import os
28
29 __doc__="ZenPacks base definitions"
30
34 objs.sort(compare)
35 result = []
36 for obj in objs:
37 for alreadyInList in result:
38 path = alreadyInList.getPrimaryPath()
39 if obj.getPrimaryPath()[:len(path)] == path:
40 break
41 else:
42 result.append(obj)
43 return result
44
45
52
53
55 '''The root of all ZenPacks: has no implementation,
56 but sits here to be the target of the Relation'''
57
58 objectPaths = None
59 author = ''
60 organization = ''
61 url = ''
62 version = '0.1'
63
64 requires = ()
65
66 loaders = (ZPLObject(), ZPLReport(), ZPLDaemons(), ZPLBin(), ZPLLibExec(),
67 ZPLSkins(), ZPLDataSources(), ZPLLibraries(), ZPLAbout())
68
69 _properties = ZenModelRM._properties + (
70 {'id':'objectPaths','type':'lines','mode':'w'},
71 {'id':'author', 'type':'string', 'mode':'w'},
72 {'id':'organization', 'type':'string', 'mode':'w'},
73 {'id':'version', 'type':'string', 'mode':'w'},
74 {'id':'url', 'type':'string', 'mode':'w'},
75 )
76
77 _relations = (
78 ('root', ToOne(ToManyCont, 'Products.ZenModel.DataRoot', 'packs')),
79 ("packables", ToMany(ToOne, "Products.ZenModel.ZenPackable", "pack")),
80 )
81
82 factory_type_information = (
83 { 'immediate_view' : 'viewPackDetail',
84 'factory' : 'manage_addZenPack',
85 'actions' :
86 (
87 { 'id' : 'viewPackDetail'
88 , 'name' : 'Detail'
89 , 'action' : 'viewPackDetail'
90 , 'permissions' : ( "Manage DMD", )
91 },
92 )
93 },
94 )
95
96 packZProperties = [
97 ]
98
99
100 - def path(self, *args):
102
103
108
109
115
116
121
122
158
159
160 - def list(self, app):
166
167
172
173
177
178
189
191 "Export the ZenPack to the /export directory"
192 from StringIO import StringIO
193 from Acquisition import aq_base
194 xml = StringIO()
195
196
197 xml.write("""<?xml version="1.0"?>\n""")
198 xml.write("<objects>\n")
199 packables = eliminateDuplicates(self.packables())
200 for obj in packables:
201 obj = aq_base(obj)
202 xml.write('<!-- %r -->\n' % (obj.getPrimaryPath(),))
203 obj.exportXml(xml,['devices','networks', 'pack'],True)
204 xml.write("</objects>\n")
205 path = zenPackPath(self.id, 'objects')
206 if not os.path.isdir(path):
207 os.mkdir(path, 0750)
208 objects = file(os.path.join(path, 'objects.xml'), 'w')
209 objects.write(xml.getvalue())
210 objects.close()
211
212
213 path = zenPackPath(self.id, 'skins')
214 if not os.path.isdir(path):
215 os.makeDirs(path, 0750)
216
217
218 init = zenPackPath(self.id, '__init__.py')
219 if not os.path.isfile(init):
220 fp = file(init, 'w')
221 fp.write(
222 '''
223 import Globals
224 from Products.CMFCore.DirectoryView import registerDirectory
225 registerDirectory("skins", globals())
226 ''')
227 fp.close()
228
229
230 about = zenPackPath(self.id, CONFIG_FILE)
231 values = {}
232 parser = ConfigParser.SafeConfigParser()
233 if os.path.isfile(about):
234 try:
235 parser.read(about)
236 values = dict(parser.items(CONFIG_SECTION_ABOUT))
237 except ConfigParser.Error:
238 pass
239 current = [(p['id'], str(getattr(self, p['id'], '') or ''))
240 for p in self._properties]
241 values.update(dict(current))
242 if not parser.has_section(CONFIG_SECTION_ABOUT):
243 parser.add_section(CONFIG_SECTION_ABOUT)
244 for key, value in values.items():
245 parser.set(CONFIG_SECTION_ABOUT, key, value)
246 fp = file(about, 'w')
247 try:
248 parser.write(fp)
249 finally:
250 fp.close()
251
252
253 path = zenPath('export')
254 if not os.path.isdir(path):
255 os.makeDirs(path, 0750)
256 from zipfile import ZipFile, ZIP_DEFLATED
257 zf = ZipFile(os.path.join(path, '%s.zip' % self.id), 'w', ZIP_DEFLATED)
258 base = zenPackPath()
259 for p, ds, fd in os.walk(zenPackPath(self.id)):
260 if p.split('/')[-1].startswith('.'): continue
261 for f in fd:
262 if f.startswith('.'): continue
263 if f.endswith('.pyc'): continue
264 filename = os.path.join(p, f)
265 zf.write(filename, filename[len(base)+1:])
266 zf.close()
267 if REQUEST:
268 REQUEST['message'] = '%s has been exported' % self.id
269 return self.callZenScreen(REQUEST)
270
272 dsClasses = []
273 for path, dirs, files in os.walk(self.path(name)):
274 dirs[:] = [d for d in dirs if not d.startswith('.')]
275 for f in files:
276 if not f.startswith('.') \
277 and f.endswith('.py') \
278 and not f == '__init__.py':
279 parts = path.split('/') + [f[:-3]]
280 parts = parts[parts.index('Products'):]
281 dsClasses.append(importClass('.'.join(parts)))
282 return dsClasses
283
286
289
290
292 return zenPath('Products', *parts)
293
294
295
296
297
298
299 ZenPackBase = ZenPack
300
301 InitializeClass(ZenPack)
302