admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import logging |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 3 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 4 | Created on 24-Oct-2012 |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 5 | |
| 6 | author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ), |
| 7 | Raghav Kashyap( raghavkashyap@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 8 | |
| 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 |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 13 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 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 |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 22 | |
| 23 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 25 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | import re |
| 27 | from logging import Logger |
| 28 | |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 29 | |
| 30 | class Component( object ): |
| 31 | |
| 32 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | This is the tempalte class for components |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 34 | """ |
| 35 | def __init__( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 36 | self.default = '' |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 37 | self.wrapped = sys.modules[ __name__ ] |
| 38 | |
| 39 | def __getattr__( self, name ): |
| 40 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | This will invoke, if the attribute wasn't found the usual ways. |
| 42 | Here it will look for assert_attribute and will execute when AttributeError occurs. |
| 43 | It will return the result of the assert_attribute. |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 44 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 45 | try: |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 46 | return getattr( self.wrapped, name ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 47 | except AttributeError: |
| 48 | try: |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 49 | def experimentHandling( **kwargs ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | if main.EXPERIMENTAL_MODE == main.TRUE: |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 51 | result = self.experimentRun( **kwargs ) |
| 52 | main.log.info( "EXPERIMENTAL MODE. API " + str( |
| 53 | name ) + " not yet implemented. Returning dummy values" ) |
| 54 | return result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | else: |
| 56 | return main.FALSE |
| 57 | return experimentHandling |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 58 | except TypeError as e: |
| 59 | main.log.error( |
| 60 | "Arguments for experimental mode does not have key 'retruns'" + |
| 61 | e ) |
| 62 | |
| 63 | def connect( self ): |
| 64 | |
| 65 | vars( main )[ self.name + 'log' ] = logging.getLogger( self.name ) |
| 66 | |
| 67 | session_file = main.logdir + "/" + self.name + ".session" |
| 68 | self.log_handler = logging.FileHandler( session_file ) |
| 69 | self.log_handler.setLevel( logging.DEBUG ) |
| 70 | |
| 71 | vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG ) |
| 72 | _formatter = logging.Formatter( |
| 73 | "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" ) |
| 74 | self.log_handler.setFormatter( _formatter ) |
| 75 | vars( main )[ self.name + 'log' ].addHandler( self.log_handler ) |
| 76 | # Adding header for the component log |
| 77 | vars( main )[ self.name + 'log' ].info( main.logHeader ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 78 | # Opening the session log to append command's execution output |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 79 | self.logfile_handler = open( session_file, "a" ) |
| 80 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | return "Dummy" |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 82 | |
| 83 | def execute( self, cmd ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | return main.TRUE |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 85 | # import commands |
| 86 | # return commands.getoutput( cmd ) |
| 87 | |
| 88 | def disconnect( self ): |
| 89 | return main.TRUE |
| 90 | |
| 91 | def config( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 92 | self = self |
| 93 | # Need to update the configuration code |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 94 | |
| 95 | def cleanup( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 96 | return main.TRUE |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 97 | |
| 98 | def log( self, message ): |
| 99 | """ |
| 100 | Here finding the for the component to which the |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | log message based on the called child object. |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 102 | """ |
| 103 | vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" ) |
| 104 | |
| 105 | def close_log_handles( self ): |
| 106 | vars( main )[ self.name + 'log' ].removeHandler( self.log_handler ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 107 | if self.logfile_handler: |
| 108 | self.logfile_handler.close() |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 109 | |
| 110 | def get_version( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | return "Version unknown" |
| 112 | |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 113 | def experimentRun( self, **kwargs ): |
| 114 | args = utilities.parse_args( [ "RETURNS" ], **kwargs ) |
| 115 | return args[ "RETURNS" ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | |
| 117 | |
| 118 | if __name__ != "__main__": |
| 119 | import sys |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 120 | sys.modules[ __name__ ] = Component() |
| 121 | |