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

# Source Code for Module Products.ZenRelations.checkrel

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