1
2
3
4
5
6
7
8
9
10 import os
11 import string
12 import sys
13
14 from rhn.tb import raise_with_tb
15
16 try:
17 from types import ClassType
18 except ImportError:
19 ClassType = type
20
22 """Exception class"""
23 pass
24
25
27
28
29
30
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
41 if comp[0] not in alpha:
42 raise GetMethodException(
43 "Method names should start with an alphabetic character")
44
45
47
48
49
50
51
52 methodNameComps = baseClass.split('.') + methodName.split('.')
53
54 sanity(methodNameComps)
55
56 for index in range(len(methodNameComps), 0, -1):
57 modulename = '.'.join(methodNameComps[:index])
58 try:
59 actions = __import__(modulename)
60 except ImportError:
61
62 continue
63 except Exception:
64 raise_with_tb(GetMethodException("Could not import module %s" % modulename))
65
66 break
67 else:
68
69 raise GetMethodException("Action %s could not be imported" % methodName)
70
71
72 fIndex = index
73
74 className = actions
75
76
77 for index in range(1, len(methodNameComps)):
78 comp = methodNameComps[index]
79 if index < fIndex:
80
81 if not hasattr(className, comp):
82
83 raise GetMethodException("Class %s has no attribute %s" % (
84 '.'.join(methodNameComps[:index]), comp))
85 className = getattr(className, comp)
86
87 continue
88
89
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
100 className = className()
101
102
103 return className
104
105
106
107 if __name__ == '__main__':
108
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