Package ZenModel :: Module ZenPackLoader
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.ZenPackLoader

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  __doc__='Base Classes for loading gunk in a ZenPack' 
 15   
 16  import Globals 
 17  from Products.ZenReports.ReportLoader import ReportLoader 
 18  from Products.ZenUtils.Utils import getObjByPath, zenPath 
 19   
 20  import os 
 21  import ConfigParser 
 22  import logging 
 23  log = logging.getLogger('zen.ZPLoader') 
 24   
 25  CONFIG_FILE = 'about.txt' 
 26  CONFIG_SECTION_ABOUT = 'about' 
 27   
28 -def findFiles(pack, directory, filter=None):
29 result = [] 30 for p, ds, fs in os.walk(pack.path(directory)): 31 if not os.path.split(p)[-1].startswith('.'): 32 for f in fs: 33 if filter is None or filter(f): 34 result.append(os.path.join(p, f)) 35 return result
36
37 -def findDirectories(pack, directory):
38 result = [] 39 for p, ds, fs in os.walk(pack.path(directory)): 40 if not os.path.split(p)[-1].startswith('.'): 41 for d in ds: 42 result.append(os.path.join(p, d)) 43 return result
44
45 -def branchAfter(filename, directory, prefix = ""):
46 "return the branch after the given directory name" 47 path = filename.split('/') 48 return prefix + '/'.join(path[path.index(directory)+1:])
49 50
51 -class ZenPackLoader:
52 53 name = "Set This Name" 54
55 - def load(self, pack, app):
56 """Load things from the ZenPack and put it 57 into the app"""
58
59 - def unload(self, pack, app):
60 """Remove things from Zenoss defined in the ZenPack"""
61
62 - def list(self, pack, app):
63 "List the items that would be loaded from the given (unpacked) ZenPack"
64
65 - def upgrade(self, pack, app):
66 "Run an upgrade on an existing pack"
67 68 from xml.sax import saxutils, make_parser 69 from xml.sax.handler import ContentHandler 70
71 -class ZPLObject(ZenPackLoader):
72 73 name = "Objects" 74
75 - def load(self, pack, app):
76 from Products.ZenRelations.ImportRM import ImportRM 77 class AddToPack(ImportRM): 78 def endElement(self, name): 79 if name == 'object': 80 obj = self.objstack[-1] 81 log.debug('Now adding %s', obj.getPrimaryUrlPath()) 82 try: 83 obj.buildRelations() 84 obj.removeRelation('pack') 85 obj.addRelation('pack', pack) 86 except Exception, ex: 87 log.exception("Error adding pack to %s", 88 obj.getPrimaryUrlPath()) 89 90 ImportRM.endElement(self, name)
91 importer = AddToPack(noopts=True, app=app) 92 importer.options.noindex = True 93 for f in self.objectFiles(pack): 94 log.info("Loading %s", f) 95 importer.loadObjectFromXML(xmlfile=f) 96 97
98 - def parse(self, filename, handler):
99 parser = make_parser() 100 parser.setContentHandler(handler) 101 parser.parse(open(filename))
102 103
104 - def unload(self, pack, app):
105 from Products.ZenRelations.Exceptions import ObjectNotFound 106 dmd = app.zport.dmd 107 objs = pack.packables() 108 objs.sort(lambda x, y: cmp(x.getPrimaryPath(), y.getPrimaryPath())) 109 objs.reverse() 110 for obj in objs: 111 path = obj.getPrimaryPath() 112 path, id = path[:-1], path[-1] 113 obj = dmd.getObjByPath(path) 114 if len(path) > 3: # leave /Services, /Devices, etc. 115 try: 116 try: 117 obj._delObject(id) 118 except ObjectNotFound: 119 obj._delOb(id) 120 except (AttributeError, KeyError), ex: 121 log.warning("Unable to remove %s on %s", id, 122 '/'.join(path))
123
124 - def list(self, pack, app):
125 return [obj.getPrimaryUrlPath() for obj in pack.packables()]
126 127
128 - def objectFiles(self, pack):
129 def isXml(f): return f.endswith('.xml') 130 return findFiles(pack, 'objects', isXml)
131 132
133 -class ZPLReport(ZPLObject):
134 135 name = "Reports" 136
137 - def load(self, pack, app):
138 class HookReportLoader(ReportLoader): 139 def loadFile(self, root, id, fullname): 140 rpt = ReportLoader.loadFile(self, root, id, fullname) 141 rpt.addRelation('pack', pack) 142 return rpt
143 rl = HookReportLoader(noopts=True, app=app) 144 rl.options.force = True 145 rl.loadDirectory(pack.path('reports')) 146
147 - def upgrade(self, pack, app):
148 self.load(pack, app)
149
150 - def list(self, pack, app):
151 return [branchAfter(r, 'reports') for r in findFiles(pack, 'reports')]
152 153
154 -class ZPLDaemons(ZenPackLoader):
155 156 name = "Daemons" 157 158 extensionsToIgnore = ('.svn-base', '.pyc' '~')
159 - def filter(self, f):
160 for ext in self.extensionsToIgnore: 161 if f.endswith(ext): 162 return False 163 return True
164 165
166 - def binPath(self, daemon):
167 return zenPath('bin', os.path.basename(daemon))
168
169 - def load(self, pack, app):
170 for fs in findFiles(pack, 'daemons', filter=self.filter): 171 os.chmod(fs, 0755) 172 path = self.binPath(fs) 173 if os.path.exists(path): 174 os.remove(path) 175 os.symlink(fs, self.binPath(fs))
176
177 - def upgrade(self, pack, app):
178 self.load(pack, app)
179
180 - def unload(self, pack, app):
181 for fs in findFiles(pack, 'daemons', filter=self.filter): 182 try: 183 os.remove(self.binPath(fs)) 184 except OSError: 185 pass
186
187 - def list(self, pack, app):
188 return [branchAfter(d, 'daemons') 189 for d in findFiles(pack, 'daemons', filter=self.filter)]
190 191
192 -class ZPLBin(ZenPackLoader):
193 194 name = "Bin" 195 196 extensionsToIgnore = ('.svn-base', '.pyc' '~')
197 - def filter(self, f):
198 for ext in self.extensionsToIgnore: 199 if f.endswith(ext): 200 return False 201 return True
202
203 - def load(self, pack, app):
204 for fs in findFiles(pack, 'bin', filter=self.filter): 205 os.chmod(fs, 0755)
206
207 - def upgrade(self, pack, app):
208 self.load(pack, app)
209
210 - def list(self, pack, app):
211 return [branchAfter(d, 'bin') 212 for d in findFiles(pack, 'bin', filter=self.filter)]
213 214
215 -class ZPLLibExec(ZenPackLoader):
216 217 name = "LibExec" 218 219 extensionsToIgnore = ('.svn-base', '.pyc' '~')
220 - def filter(self, f):
221 for ext in self.extensionsToIgnore: 222 if f.endswith(ext): 223 return False 224 return True
225
226 - def load(self, pack, app):
227 for fs in findFiles(pack, 'libexec', filter=self.filter): 228 os.chmod(fs, 0755)
229
230 - def upgrade(self, pack, app):
231 self.load(pack, app)
232
233 - def list(self, pack, app):
234 return [branchAfter(d, 'libexec') 235 for d in findFiles(pack, 'libexec', filter=self.filter)]
236 237
238 -class ZPLModelers(ZenPackLoader):
239 240 name = "Modeler Plugins" 241 242
243 - def list(self, pack, app):
244 return [branchAfter(d, 'plugins') 245 for d in findFiles(pack, 'modeler/plugins')]
246 247
248 -class ZPLSkins(ZenPackLoader):
249 250 name = "Skins" 251 252
253 - def load(self, pack, app):
254 from Products.ZenUtils.Skins import registerSkin 255 from Products.ZenUtils.Utils import getObjByPath 256 registerSkin(app.zport.dmd, pack.path(''))
257 258
259 - def unload(self, pack, app):
260 from Products.ZenUtils.Skins import unregisterSkin 261 unregisterSkin(app.zport.dmd, pack.path(''))
262 263
264 - def list(self, pack, app):
265 return [branchAfter(d, 'skins') for d in findDirectories(pack, 'skins')]
266 267
268 -class ZPLDataSources(ZenPackLoader):
269 270 name = "DataSources" 271 272
273 - def list(self, pack, app):
274 return [branchAfter(d, 'datasources') 275 for d in findFiles(pack, 'datasources', 276 lambda f: not f.endswith('.pyc') and f != '__init__.py')]
277 278
279 -class ZPLLibraries(ZenPackLoader):
280 281 name = "Libraries" 282 283
284 - def list(self, pack, app):
285 d = pack.path('lib') 286 if os.path.isdir(d): 287 return [l for l in os.listdir(d)] 288 return []
289
290 -class ZPLAbout(ZenPackLoader):
291 292 name = "About" 293
294 - def getAttributeValues(self, pack):
295 about = pack.path(CONFIG_FILE) 296 result = [] 297 if os.path.exists(about): 298 parser = ConfigParser.SafeConfigParser() 299 parser.read(about) 300 result = [] 301 for key, value in parser.items(CONFIG_SECTION_ABOUT): 302 try: 303 value = eval(value) 304 except: 305 # Blanket exception catchers like this are evil. 306 pass 307 result.append((key, value)) 308 return result
309 310
311 - def load(self, pack, app):
312 for name, value in self.getAttributeValues(pack): 313 setattr(pack, name, value)
314 315
316 - def upgrade(self, pack, app):
317 self.load(pack, app)
318 319
320 - def list(self, pack, app):
321 return [('%s %s' % av) for av in self.getAttributeValues(pack)]
322