1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import os
24 import string
25 import sys
26 from spacewalk.common.usix import ClassType
27 from distutils.sysconfig import get_python_lib
28
29 from spacewalk.common.usix import raise_with_tb
30
32
33 """Exception class"""
34 pass
35
36
38 """ Verifies if all the components have proper names."""
39
40 alpha = string.lowercase + string.uppercase
41 allowedChars = alpha + string.digits + '_'
42 for comp in methodNameComps:
43 if not len(comp):
44 raise GetMethodException("Empty method component")
45 for c in comp:
46 if c not in allowedChars:
47 raise GetMethodException(
48 "Invalid character '%s' in the method name" % c)
49
50 if comp[0] not in alpha:
51 raise GetMethodException(
52 "Method names should start with an alphabetic character")
53
54
56 """ Retreive method given methodName, path to base of tree, and class/module
57 route/label.
58 """
59
60 methodNameComps = ['spacewalk'] + string.split(baseClass, '.') + string.split(methodName, '.')
61
62 sanity(methodNameComps)
63
64 path = get_python_lib()
65 for index in range(len(methodNameComps)):
66 comp = methodNameComps[index]
67 path = "%s/%s" % (path, comp)
68
69 if os.path.isdir(path):
70
71 continue
72
73 for extension in ['py', 'pyc', 'pyo']:
74 if os.path.isfile("%s.%s" % (path, extension)):
75
76 break
77 else:
78
79 raise GetMethodException("Action %s could not be found" % methodName)
80 break
81 else:
82
83 raise GetMethodException("Very wrong")
84
85
86 fIndex = index + 1
87
88 modulename = string.join(methodNameComps[:fIndex], '.')
89
90 try:
91 actions = __import__(modulename)
92 except ImportError:
93 raise_with_tb(GetMethodException("Could not import module %s" % modulename), sys.exc_info()[2])
94
95 className = actions
96
97
98 for index in range(1, len(methodNameComps)):
99 comp = methodNameComps[index]
100 if index < fIndex:
101
102 if not hasattr(className, comp):
103
104 raise GetMethodException("Class %s has no attribute %s" % (
105 string.join(methodNameComps[:index], '.'), comp))
106 className = getattr(className, comp)
107
108 continue
109
110
111 if not hasattr(className, '__rhnexport__'):
112 raise GetMethodException("Class %s is not RHN-compliant" %
113 string.join(methodNameComps[:index], '.'))
114 export = getattr(className, '__rhnexport__')
115 if comp not in export:
116 raise GetMethodException("Class %s does not export '%s'" % (
117 string.join(methodNameComps[:index], '.'), comp))
118 className = getattr(className, comp)
119 if type(className) is ClassType:
120
121 className = className()
122
123
124 return className
125
126
127
128 if __name__ == '__main__':
129
130 methods = [
131 'a.b.c.d.e.f',
132 'a.b.c.d.e.foo.h',
133 'a.b.c.d.e.g.h',
134 'a.b.d.d.e.g.h',
135 'a.b.d.d._e.g.h',
136 'a.b.d.d.e_.g.h',
137 'a.b.d.d.e-.g.h',
138 'a.b.d.d..g.h',
139 ]
140
141 for m in methods:
142 print("----Running method %s: " % m)
143 try:
144 method = getMethod(m, 'Actions')
145 except GetMethodException:
146 e = sys.exc_info()[1]
147 print("Error getting the method %s: %s" % (m,
148 string.join(map(str, e.args))))
149 else:
150 method()
151
152