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

# Source Code for Module Products.ZenReports.ReportLoader

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