| """ |
| Start CLI for CHOTestMonkey |
| Author: you@onlab.us |
| """ |
| from multiprocessing.connection import Client |
| |
| commandMap = {} |
| paramNum = {} |
| |
| |
| def triggerEvent( debugMode, name, scheduleMethod, args ): |
| """ |
| This function inserts an event from CLI to CHOTestMonkey |
| """ |
| host = "localhost" |
| port = 6000 |
| address = ( host, port ) |
| conn = Client( address ) |
| request = [] |
| if debugMode: |
| request.append( 2 ) |
| else: |
| request.append( 1 ) |
| request.append( name ) |
| request.append( scheduleMethod ) |
| for arg in args: |
| request.append( arg ) |
| conn.send( request ) |
| response = conn.recv() |
| return response |
| |
| |
| def startCLI(): |
| debugMode = False |
| while True: |
| try: |
| if debugMode: |
| cmd = raw_input( "CHOTestMonkey-debug>" ) |
| else: |
| cmd = raw_input( "CHOTestMonkey>" ) |
| except EOFError: |
| print "exit" |
| return |
| except Exception: |
| print "Uncaught exception!" |
| return |
| |
| if cmd == 'help': |
| print 'Supported commands:' |
| print 'help' |
| print 'debug' |
| print 'exit' |
| for command in commandMap.keys(): |
| print command |
| elif cmd == '': |
| pass |
| elif cmd == 'debug': |
| debugMode = True |
| elif cmd == 'exit': |
| if debugMode: |
| debugMode = False |
| else: |
| return |
| else: |
| cmdList = cmd.split( ' ' ) |
| if cmdList[ 0 ] in commandMap.keys(): |
| num = paramNum[ cmdList[ 0 ] ] |
| name = commandMap[ cmdList[ 0 ] ] |
| """ |
| if len( cmdList ) < num + 1: |
| print 'not enough arguments' |
| elif len( cmdList ) > num + 1: |
| print 'Too many arguments' |
| else: |
| """ |
| result = triggerEvent( debugMode, name, 'RUN_BLOCK', cmdList[ 1: ] ) |
| if result == 10: |
| pass |
| elif result == 11: |
| print "Scheduler busy...Try later or use debugging mode by entering \'debug\'" |
| elif result == 20: |
| print "Unknown message to server" |
| elif result == 21: |
| print "Unknown event type to server" |
| elif result == 22: |
| print "Unknown schedule method to server" |
| elif result == 23: |
| print "Not enough argument" |
| else: |
| print "Unknown response from server" |
| else: |
| print 'Unknown command' |
| |
| if __name__ == '__main__': |
| import xml.etree.ElementTree |
| try: |
| root = xml.etree.ElementTree.parse( '../CHOTestMonkey.params' ).getroot() |
| except Exception: |
| print "Uncaught exception!" |
| for child in root: |
| if child.tag == 'EVENT': |
| for event in child: |
| for item in event: |
| if item.tag == 'CLI': |
| CLI = str( item.text ) |
| if item.tag == 'typeString': |
| name = str( item.text ) |
| if item.tag == 'CLIParamNum': |
| num = int( item.text ) |
| commandMap[ CLI ] = name |
| paramNum[ CLI ] = num |
| startCLI() |