admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | import inspect |
| 2 | import sys |
| 3 | import os |
| 4 | import re |
| 5 | sys.path.append("../") |
| 6 | from core import xmldict |
| 7 | ''' |
| 8 | @author: Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | |
| 24 | ''' |
| 25 | |
| 26 | |
| 27 | class UpdateDriver: |
| 28 | def __init__(self): |
| 29 | self.default = '' |
| 30 | self.configFile = "/home/openflow/TestON/config/ofadriver.cfg" |
| 31 | self.methodDict = {} |
| 32 | self.fileDict = {} |
| 33 | |
| 34 | |
| 35 | def getmethods(self,modulePath,Class) : |
| 36 | ''' |
| 37 | This will get the list of methods in given module or class. |
| 38 | It accepts the module path and class name. If there is no |
| 39 | class name then it has be mentioned as None. |
| 40 | ''' |
| 41 | methodList = [] |
| 42 | moduleList = modulePath.split("/") |
| 43 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 44 | print "Message : Method list is being obatined , Please wait ..." |
| 45 | try : |
| 46 | if Class : |
| 47 | Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [Class], -1) |
| 48 | ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] |
| 49 | self.ClassList = ClassList |
| 50 | Class = vars(Module)[Class] |
| 51 | methodList = [x.__name__ for x in Class.__dict__.values() if inspect.isfunction(x)] |
| 52 | else : |
| 53 | Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(),[moduleList[len(moduleList) - 2]], -1) |
| 54 | methodList = [x.__name__ for x in Module.__dict__.values() if inspect.isfunction(x)] |
| 55 | ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)] |
| 56 | self.ClassList = ClassList |
| 57 | except : |
| 58 | print "Error : " +str(sys.exc_info()[1]) |
| 59 | |
| 60 | |
| 61 | self.method = methodList |
| 62 | return self.method |
| 63 | |
| 64 | def echo(self) : |
| 65 | print "Echoing !!!!!!" |
| 66 | |
| 67 | def getargs(self,moduleName,className,method) : |
| 68 | ''' |
| 69 | This will return the list of arguments in a method of python module of class. |
| 70 | It accepts method list as an argument. |
| 71 | ''' |
| 72 | print "Message : Argument list is being obtained for each method" |
| 73 | methodArgsDict = {} |
| 74 | if className == None: |
| 75 | moduleList = moduleName.split(".") |
| 76 | for index,name in enumerate(method) : |
| 77 | Module = __import__(moduleList[len(moduleList) -1], globals(), locals(), [moduleList[len(moduleList) -2]], -1) |
| 78 | try : |
| 79 | names = vars(Module)[name] |
| 80 | except KeyError: |
| 81 | print "Message : method '" + name + "'does not exists,Continued with including it. " |
| 82 | return False |
| 83 | argumentList = inspect.getargspec(names) #inspect.getargvalues(name) |
| 84 | methodArgsDict[name] = argumentList[0] |
| 85 | else : |
| 86 | moduleList = moduleName.split(".") |
| 87 | for index,name in enumerate(method) : |
| 88 | Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [className], -1) |
| 89 | Class = getattr(Module, className) |
| 90 | try : |
| 91 | names = vars(Class)[name] |
| 92 | except KeyError : |
| 93 | print "Message : method '" + name + "'does not exists,Continued with include it." |
| 94 | return False |
| 95 | |
| 96 | argumentList = inspect.getargspec(names) #inspect.getargvalues(name) |
| 97 | methodArgsDict[name] = argumentList[0] |
| 98 | |
| 99 | return methodArgsDict |
| 100 | |
| 101 | def configparser(self,fileName): |
| 102 | ''' |
| 103 | It will parse the config file (ofa.cfg) and return as dictionary |
| 104 | ''' |
| 105 | |
| 106 | matchFileName = re.match(r'(.*)\.cfg', fileName, re.M | re.I) |
| 107 | if matchFileName: |
| 108 | self.configFile = fileName |
| 109 | try : |
| 110 | xml = open(fileName).read() |
| 111 | self.configDict = xmldict.xml_to_dict(xml) |
| 112 | return self.configDict |
| 113 | except : |
| 114 | print "Error : Config file " + self.configFile + " not defined properly or file path error" |
| 115 | |
| 116 | |
| 117 | def getList(self): |
| 118 | ''' |
| 119 | This method will maintain the hash with module->class->methodList or |
| 120 | module -> methodList .It will return the same Hash. |
| 121 | ''' |
| 122 | classList = [] |
| 123 | try : |
| 124 | moduleList = self.configDict['config-driver']['importTypes'][self.driver]['modules'].keys() |
| 125 | except KeyError,e: |
| 126 | print "Error : Module Does not Exists" |
| 127 | print e |
| 128 | return False |
| 129 | |
| 130 | for index,value in enumerate(moduleList): |
| 131 | modulePath = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['path'] |
| 132 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['name'] |
| 133 | |
| 134 | try : |
| 135 | pathList = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['set-path'].split(",") |
| 136 | sys.path.extend(pathList) |
| 137 | except KeyError : |
| 138 | print "Error : No System Path is given " |
| 139 | pass |
| 140 | try : |
| 141 | Class = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['classes'] |
| 142 | except : |
| 143 | Class = None |
| 144 | if Class == None : |
| 145 | methodList = self.getmethods(modulePath,None) |
| 146 | self.methodDict[moduleName] = methodList |
| 147 | self.method_ignoreList(value,None) |
| 148 | self.getMethodArgsHash(moduleName,value,None) |
| 149 | else : |
| 150 | classList = self.configDict['config-driver']['importTypes'][self.driver]['modules'][value]['classes'].keys() |
| 151 | for indx,className in enumerate(classList): |
| 152 | if className == 'ignore-list' : |
| 153 | pass |
| 154 | else : |
| 155 | methodList = self.getmethods(modulePath,className) |
| 156 | self.methodDict[moduleName] = {className : methodList} |
| 157 | self.method_ignoreList(value,className) |
| 158 | self.class_ignoreList(value) |
| 159 | self.getMethodArgsHash(moduleName,value,className) |
| 160 | |
| 161 | def class_ignoreList(self,module) : |
| 162 | ''' |
| 163 | It removes the ignored classes for each module mention in ofadriver.cfg |
| 164 | ''' |
| 165 | class_ignoreList = [] |
| 166 | if self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'] == None : |
| 167 | pass |
| 168 | else : |
| 169 | try : |
| 170 | class_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes']['ignore-list']).split(",") |
| 171 | except KeyError : |
| 172 | print "Message : No Class Ignore List present" |
| 173 | return True |
| 174 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] |
| 175 | try : |
| 176 | for index,className in enumerate(class_ignoreList): |
| 177 | if className in self.methodDict[moduleName].keys(): |
| 178 | del self.methodDict[moduleName][className] |
| 179 | except AttributeError: |
| 180 | pass |
| 181 | return self.methodDict |
| 182 | |
| 183 | def method_ignoreList(self,module,className): |
| 184 | ''' |
| 185 | It removes the ignored methods of each module or class mentioned in ofadriver.cfg. |
| 186 | ''' |
| 187 | method_ignoreList = [] |
| 188 | |
| 189 | try : |
| 190 | if className == None : |
| 191 | try : |
| 192 | method_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['methods']['ignore-list']).split(",") |
| 193 | except TypeError : |
| 194 | pass |
| 195 | else : |
| 196 | try : |
| 197 | method_ignoreList = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'][className]['methods']['ignore-list']).split(",") |
| 198 | except TypeError : |
| 199 | pass |
| 200 | except KeyError : |
| 201 | print "Message : No Ignore-List Exists , proceeding for looking add method" |
| 202 | self.add_method(module,className) |
| 203 | return True |
| 204 | |
| 205 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] |
| 206 | #import pprint |
| 207 | #pprint.pprint(self.methodDict[moduleName]) |
| 208 | for index, method in enumerate(method_ignoreList) : |
| 209 | if className == None : |
| 210 | try : |
| 211 | self.methodDict[moduleName].remove(method) |
| 212 | #pprint.pprint(self.methodDict) |
| 213 | except ValueError: |
| 214 | print "Message : Method " + method + "Does not exist in module " + moduleName + ", Continue to rest execution" |
| 215 | pass |
| 216 | |
| 217 | else : |
| 218 | if method in self.methodDict[moduleName][className] : |
| 219 | self.methodDict[moduleName][className].remove(method) |
| 220 | self.add_method(module,className) |
| 221 | return self.methodDict |
| 222 | |
| 223 | def add_method(self,module,className) : |
| 224 | ''' |
| 225 | This will add the methods(mentioned in ofadriver.cfg file) into method list if it doesnot exists in list. |
| 226 | ''' |
| 227 | method_List = [] |
| 228 | try : |
| 229 | if className == None : |
| 230 | try : |
| 231 | method_List = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['methods']['add-list']).split(",") |
| 232 | except TypeError : |
| 233 | pass |
| 234 | else : |
| 235 | try : |
| 236 | method_List = str(self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['classes'][className]['methods']['add-list']).split(",") |
| 237 | except TypeError : |
| 238 | pass |
| 239 | |
| 240 | except KeyError : |
| 241 | print "Message : No Add-List Exists , Proceeding with all available methods" |
| 242 | return True |
| 243 | moduleName = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['name'] |
| 244 | for index, method in enumerate(method_List) : |
| 245 | if className == None : |
| 246 | self.methodDict[moduleName] = [] |
| 247 | self.methodDict[moduleName].append(method) |
| 248 | else : |
| 249 | self.methodDict[moduleName][className] = [] |
| 250 | self.methodDict[moduleName][className].append(method) |
| 251 | |
| 252 | def getMethodArgsHash(self,moduleName,module,className): |
| 253 | ''' |
| 254 | This will maintain a Hash of class->method->argumentsList |
| 255 | ''' |
| 256 | modulePath = self.configDict['config-driver']['importTypes'][self.driver]['modules'][module]['path'] |
| 257 | moduleList = modulePath.split("/") |
| 258 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 259 | if className == None : |
| 260 | methodArgs = self.getargs(newModule,None,self.methodDict[moduleName]) |
| 261 | self.fileDict[moduleName] = methodArgs |
| 262 | else : |
| 263 | methodArgs = self.getargs(newModule,className,self.methodDict[moduleName][className]) |
| 264 | self.fileDict[className] = methodArgs |
| 265 | return self.fileDict |
| 266 | |
| 267 | def appendDriver(self,fileName): |
| 268 | ''' |
| 269 | This will append the given driver file with methods along with arguments. |
| 270 | ''' |
| 271 | matchFileName = re.match(r'(.*)\.py', fileName, re.M | re.I) |
| 272 | |
| 273 | if matchFileName: |
| 274 | fileHandle = None |
| 275 | try : |
| 276 | print "Message : Writing Driver file at " + fileName |
| 277 | fileHandle = open(fileName,"a") |
| 278 | content = '' |
| 279 | |
| 280 | for index, key in enumerate(self.fileDict.keys()): |
| 281 | try : |
| 282 | for ind, method in enumerate(self.fileDict[key].keys()): |
| 283 | if not method == "__init__" : |
| 284 | args = '' |
| 285 | args = ",".join(self.fileDict[key][method]) |
| 286 | content = content + "\n" + " " * 4 + "def " + method + "(self," + args + ") :" |
| 287 | content = content + "\n" + " " * 8 + "return " + key + "." + method + "(" + args + ")\n" |
| 288 | except AttributeError : |
| 289 | pass |
| 290 | fileHandle.write(content) |
| 291 | fileHandle.close() |
| 292 | return content |
| 293 | |
| 294 | except : |
| 295 | print "Error : Driver file " + fileName + "does not exists" |
| 296 | else : |
| 297 | print "Error : File name " + fileName + "is not python module" |
| 298 | return False |
| 299 | |
| 300 | |
| 301 | def writeDriver(self, driver) : |
| 302 | ''' |
| 303 | This will accept the List of driver name and write those drivers if no driver name is specified |
| 304 | then it will write all of the driver specified in the ofadriver.cfg. |
| 305 | ''' |
| 306 | self.printHeader(driver) |
| 307 | drivers = [] |
| 308 | commaMatch = re.search(",", driver, flags=0) |
| 309 | if commaMatch: |
| 310 | drivers = driver.split(",") |
| 311 | else : |
| 312 | drivers.append(driver) |
| 313 | self.driverList = [] |
| 314 | if len(drivers) == 0: |
| 315 | for index, driverName in enumerate(self.configDict['config-driver']['importTypes'].keys()): |
| 316 | self.driver = driverName |
| 317 | result = self.getList() |
| 318 | if result : |
| 319 | self.getDriverPath() |
| 320 | self.appendDriver(self.driverPath + self.driver + ".py") |
| 321 | self.driverList.append(self.driverPath + self.driver + ".py") |
| 322 | else : |
| 323 | return False |
| 324 | else : |
| 325 | for index, driverName in enumerate(drivers) : |
| 326 | |
| 327 | self.driver = driverName |
| 328 | result = self.getList() |
| 329 | if result : |
| 330 | self.getDriverPath() |
| 331 | self.appendDriver(self.driverPath + self.driver + ".py") |
| 332 | self.driverList.append(self.driverPath + self.driver + ".py") |
| 333 | else : |
| 334 | return False |
| 335 | |
| 336 | print "=" * 90 |
| 337 | print " " * 30 + "Output Driver File :" |
| 338 | print ",\n".join(self.driverList) |
| 339 | print "=" * 90 |
| 340 | return True |
| 341 | |
| 342 | |
| 343 | |
| 344 | def getDriverPath(self): |
| 345 | ''' |
| 346 | It will set the driver path and returns it.If driver path is not specified then it will take |
| 347 | default path (/lib/updatedriver/). |
| 348 | ''' |
| 349 | self.driverPath = '' |
| 350 | try : |
| 351 | self.driverPath = self.configDict['config-driver']['importTypes'][self.driver]['driver-path'] |
| 352 | |
| 353 | except KeyError : |
| 354 | path = re.sub("(bin)$", "", os.getcwd()) |
| 355 | self.driverPath = path + "/lib/updatedriver/" |
| 356 | return self.driverPath |
| 357 | |
| 358 | |
| 359 | def printHeader(self,driver): |
| 360 | content = '' |
| 361 | |
| 362 | print " " * 10 +"=" * 90 + "\n" |
| 363 | content = content + " " * 30 + "*-- Welcome to Updated Driver --*\n" |
| 364 | content = content + "\n" + " " * 10 + " " * 10 + "Config File : " + "/home/openflow/TestON/config/ofadriver.py" |
| 365 | content = content + "\n" + " " * 10 + " " * 10 + "Drivers Name : " + driver |
| 366 | print content |
| 367 | print " " * 10 + "=" * 90 |
| 368 | |
| 369 | |
| 370 | |