Package Products :: Package ZenRelations :: Module checkrel |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenRelations.checkrel

     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  import sys
    15  import logging
    16  logging.basicConfig()
    17  root = logging.getLogger()
    18  root.setLevel(logging.CRITICAL)
    19
    20  import Globals
    21
    22  from utils import importClass, importClasses
    23  from Products.ZenRelations.Exceptions import ZenSchemaError
    24
    25 -def lookupSchema(cls, relname):
    26      """
    27      Lookup the schema definition for a relationship.
    28      All base classes are checked until RelationshipManager is found.
    29      """
    30      for name, schema in cls._relations:
    31          if name == relname: return schema
    32      raise ZenSchemaError("Schema for relation %s not found on %s" %
    33                              (relname, cls.__name__))
    34
    35
    36 -def checkRelationshipSchema(cls, baseModule):
    37      """
    38      Walk all relationship schema definitions and confirm that they
    39      have reciprical peers on the remote class.
    40      """
    41      for relname, rel in cls._relations:
    42          try:
    43              remoteClass = importClass(rel.remoteClass, None)
    44          except AttributeError, e:
    45              logging.critical("RemoteClass '%s' from '%s.%s' not found",
    46                          rel.remoteClass, cls.__name__, relname)
    47              continue
    48          try:
    49              rschema = lookupSchema(remoteClass, rel.remoteName)
    50          except ZenSchemaError, e:
    51              logging.critical("Inverse def '%s' for '%s.%s' not found on '%s'",
    52                  rel.remoteName, cls.__name__, relname,rel.remoteClass)
    53              continue
    54          except Exception, e:
    55              logging.critical("RemoteClass '%s' for '%s.%s' problem.",
    56                  rel.remoteName, cls.__name__, relname)
    57              logging.critical(e)
    58              continue
    59          try:
    60              localClass = importClass(rschema.remoteClass, None)
    61          except AttributeError, e:
    62              logging.critical(e)
    63          if not issubclass(cls, localClass):
    64              logging.critical("Inverse def '%s' from '%s.%s' wrong "
    65              "remoteClass: '%s'", rel.remoteName, cls.__name__,
    66                                   relname,rschema.remoteClass)
    67          if rschema.remoteName != relname:
    68              logging.critical("Inverse def '%s' from '%s.%s' wrong "
    69                  "remoteName: '%s'", rel.remoteName, cls.__name__, relname,
    70                                      rschema.remoteName)
    71          if rel.remoteType != rschema.__class__:
    72              logging.critical("'%s.%s' inverse '%s' type %s != %s",
    73                              cls.__name__, relname, rel.remoteName,
    74                              rschema.__class__.__name__, rel.remoteType.__name__)
    75
    76
    77  baseModule = None
    78  if len(sys.argv) > 1:
    79      baseModule = sys.argv[1]
    80
    81  classList = importClasses(basemodule=baseModule,
    82              skipnames=("ZentinelPortal", "ZDeviceLoader"))
    83
    84  for classdef in classList:
    85      if hasattr(classdef, '_relations'):
    86          logging.info("checking class %s...", classdef.__name__)
    87          checkRelationshipSchema(classdef, baseModule)
    88