Package Products :: Package ZenReports :: Module ReportLoader |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenReports.ReportLoader

      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 or (at your
      8  # option) any later version as published by the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  __doc__ = """ReportLoader
     15  Load Zope reports into the ZODB.
     16  """
     17
     18  import os
     19  import sys
     20  import re
     21  import transaction
     22  import logging
     23  import Globals
     24
     25  from Products.ZenUtils.ZCmdBase import ZCmdBase
     26  from Products.ZenUtils.Utils import zenPath
     27  from Products.ZenModel.Report import Report
     28  from Products.Zuul.utils import CatalogLoggingFilter
     29
     30
     31 -class ReportLoader(ZCmdBase):
     32
     33 -    def buildOptions(self):
     34          ZCmdBase.buildOptions(self)
     35          self.parser.add_option('-f', '--force', dest='force',
     36                                 action='store_true', default=0,
     37                                 help="Load all reports, overwriting any existing reports.")
     38          self.parser.add_option('-d', '--dir', dest='dir',
     39                                 default="reports",
     40                                 help="Directory from which to load reports: default '%default'")
     41          self.parser.add_option('-p', '--zenpack', dest='zenpack',
     42                                 default='',
     43                                 help="ZenPack from which to load reports")
     44
     45      # FIXME: This call is deprecated, look for all instances of this
     46 -    def loadDatabase(self):
     47          self.loadAllReports()
     48
     49 -    def loadAllReports(self):
     50          """
     51          Load reports from the directories into the ZODB
     52          """
     53          repdirs = [zenPath('Products/ZenReports', self.options.dir)]
     54          if self.options.zenpack:
     55              repdirs = self.getZenPackDirs(self.options.zenpack)
     56
     57          for repdir in repdirs:
     58              if os.path.isdir(repdir):
     59                  self.loadDirectory(repdir)
     60                  transaction.commit()
     61
     62 -    def getZenPackDirs(self, name):
     63          matches = []
     64          for zp in self.dmd.ZenPackManager.packs():
     65              if re.search(name, zp.id):
     66                  path = os.path.join(zp.path(), self.options.dir)
     67                  matches.append(path)
     68
     69          if not matches:
     70              self.log.error("No ZenPack named '%s' was found -- exiting",
     71                             self.options.zenpack)
     72              sys.exit(1)
     73          return matches
     74
     75 -    def reports(self, directory):
     76          def normalize(f):
     77              return f.replace("_", " ")
     78          def toOrg(path):
     79              path = normalize(path).split("/")
     80              path = path[path.index("reports") + 1:]
     81              return "/" + "/".join(path)
     82          return [(toOrg(p), normalize(f[:-4]), os.path.join(p, f))
     83                  for p, ds, fs in os.walk(directory)
     84                  for f in fs
     85                  if f.endswith(".rpt")]
     86
     87 -    def unloadDirectory(self, repdir):
     88          self.log.info("Removing reports from %s", repdir)
     89          reproot = self.dmd.Reports
     90          for orgpath, fid, fullname in self.reports(repdir):
     91              rorg = reproot.createOrganizer(orgpath)
     92              if getattr(rorg, fid, False):
     93                  rorg._delObject(fid)
     94              while rorg.id != 'Reports':
     95                  if not rorg.objectValues():
     96                      id = rorg.id
     97                      rorg = rorg.getPrimaryParent()
     98                      rorg._delObject(id)
     99
    100 -    def loadDirectory(self, repdir):
    101          self.log.info("Loading reports from %s", repdir)
    102          # If zencatalog hasn't finished yet, we get ugly messages that don't
    103          # mean anything. Hide them.
    104          logFilter = None
    105          if not getattr(self.dmd.zport, '_zencatalog_completed', False):
    106              logFilter = CatalogLoggingFilter()
    107              logging.getLogger('Zope.ZCatalog').addFilter(logFilter)
    108          try:
    109              reproot = self.dmd.Reports
    110              for orgpath, fid, fullname in self.reports(repdir):
    111                  rorg = reproot.createOrganizer(orgpath)
    112                  if getattr(rorg, fid, False):
    113                      if self.options.force:
    114                          rorg._delObject(fid)
    115                      else:
    116                          continue
    117                  self.log.info("loading: %s/%s", orgpath, fid)
    118                  self.loadFile(rorg, fid, fullname)
    119          finally:
    120              # Remove our logging filter so we don't hide anything important
    121              if logFilter is not None:
    122                  logging.getLogger('Zope.ZCatalog').removeFilter(logFilter)
    123
    124 -    def loadFile(self, root, id, fullname):
    125          fdata = file(fullname).read()
    126          rpt = Report(id, text=fdata)
    127          root._setObject(id, rpt)
    128          return rpt
    129
    130
    131  if __name__ == "__main__":
    132      rl = ReportLoader()
    133      rl.loadAllReports()
    134