Package up2date_client :: Module getMethod
[hide private]
[frames] | no frames]

Source Code for Module up2date_client.getMethod

  1  # Retrieve action method name given queued action information. 
  2  # 
  3  # Client code for Update Agent 
  4  # Copyright (c) 1999--2016 Red Hat, Inc.  Distributed under GPLv2. 
  5  # 
  6  # An allowable xmlrpc method is retrieved given a base location, a 
  7  # hierarchical route to the class/module, and method name. 
  8  # 
  9   
 10  import os 
 11  import string 
 12  import sys 
 13   
 14  from rhn.tb import raise_with_tb 
 15   
 16  try: # python2 
 17      from types import ClassType 
 18  except ImportError: # python3 
 19      ClassType = type 
 20   
21 -class GetMethodException(Exception):
22 """Exception class""" 23 pass
24 25
26 -def sanity(methodNameComps):
27 #""" 28 # Verifies if all the components have proper names 29 #""" 30 # Allowed characters in each string 31 alpha = string.ascii_lowercase + string.ascii_uppercase 32 allowedChars = alpha + string.digits + '_' 33 for comp in methodNameComps: 34 if not len(comp): 35 raise GetMethodException("Empty method component") 36 for c in comp: 37 if c not in allowedChars: 38 raise GetMethodException( 39 "Invalid character '%s' in the method name" % c) 40 # Can only begin with a letter 41 if comp[0] not in alpha: 42 raise GetMethodException( 43 "Method names should start with an alphabetic character")
44 45
46 -def getMethod(methodName, baseClass):
47 #""" 48 #Retreive method given methodName, path to base of tree, and class/module 49 #route/label. 50 #""" 51 # First split the method name 52 methodNameComps = baseClass.split('.') + methodName.split('.') 53 # Sanity checks 54 sanity(methodNameComps) 55 # Look for the module, start with the most specific 56 for index in range(len(methodNameComps), 0, -1): 57 modulename = '.'.join(methodNameComps[:index]) 58 try: 59 actions = __import__(modulename) 60 except ImportError: 61 # does not exist, try next one 62 continue 63 except Exception: 64 raise_with_tb(GetMethodException("Could not import module %s" % modulename)) 65 # found one, skip the rest 66 break 67 else: 68 # no module found. die 69 raise GetMethodException("Action %s could not be imported" % methodName) 70 71 # The position of the file 72 fIndex = index 73 74 className = actions 75 # Iterate through the list of components and try to load that specific 76 # module/method 77 for index in range(1, len(methodNameComps)): 78 comp = methodNameComps[index] 79 if index < fIndex: 80 # This is a directory or a file we have to load 81 if not hasattr(className, comp): 82 # Hmmm... Not there 83 raise GetMethodException("Class %s has no attribute %s" % ( 84 '.'.join(methodNameComps[:index]), comp)) 85 className = getattr(className, comp) 86 #print(type(className)) 87 continue 88 # A file or method 89 # We look for the special __rhnexport__ array 90 if not hasattr(className, '__rhnexport__'): 91 raise GetMethodException("Class %s is not RHN-compliant" % \ 92 '.'.join(methodNameComps[:index])) 93 export = getattr(className, '__rhnexport__') 94 if comp not in export: 95 raise GetMethodException("Class %s does not export '%s'" % ( 96 '.'.join(methodNameComps[:index]), comp)) 97 className = getattr(className, comp) 98 if type(className) is ClassType: 99 # Try to instantiate it 100 className = className() 101 #print(type(className)) 102 103 return className
104 105 106 #----------------------------------------------------------------------------- 107 if __name__ == '__main__': 108 # Two valid ones and a bogus one 109 methods = [ 110 'a.b.c.d.e.f', 111 'a.b.c.d.e.foo.h', 112 'a.b.c.d.e.g.h', 113 'a.b.d.d.e.g.h', 114 'a.b.d.d._e.g.h', 115 'a.b.d.d.e_.g.h', 116 'a.b.d.d.e-.g.h', 117 'a.b.d.d..g.h', 118 ] 119 120 for m in methods: 121 print("----Running method %s: " % m) 122 try: 123 method = getMethod(m, 'Actions') 124 except GetMethodException: 125 e = sys.exc_info()[1] 126 print("Error getting the method %s: %s" % (m, 127 ''.join(map(str, e.args)))) 128 else: 129 method() 130 #----------------------------------------------------------------------------- 131