You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 1 | """ |
| 2 | Start CLI for CHOTestMonkey |
| 3 | Author: you@onlab.us |
| 4 | """ |
| 5 | from multiprocessing.connection import Client |
| 6 | |
| 7 | commandMap = {} |
| 8 | paramNum = {} |
| 9 | |
| 10 | def triggerEvent( debugMode, name, scheduleMethod, args ): |
| 11 | """ |
| 12 | This function inserts an event from CLI to CHOTestMonkey |
| 13 | """ |
| 14 | host = "localhost" |
| 15 | port = 6000 |
| 16 | address = ( host, port ) |
| 17 | conn = Client( address ) |
| 18 | request = [] |
| 19 | if debugMode: |
| 20 | request.append( 2 ) |
| 21 | else: |
| 22 | request.append( 1 ) |
| 23 | request.append( name ) |
| 24 | request.append( scheduleMethod ) |
| 25 | for arg in args: |
| 26 | request.append( arg ) |
| 27 | conn.send( request ) |
| 28 | response = conn.recv() |
| 29 | return response |
| 30 | |
| 31 | def startCLI(): |
| 32 | debugMode = False |
| 33 | while 1: |
| 34 | try: |
| 35 | if debugMode: |
| 36 | cmd = raw_input("CHOTestMonkey-debug>") |
| 37 | else: |
| 38 | cmd = raw_input("CHOTestMonkey>") |
| 39 | except EOFError: |
| 40 | print "exit" |
| 41 | return |
| 42 | except Exception: |
| 43 | print "Uncaught exception!" |
| 44 | return |
| 45 | |
| 46 | if cmd == 'help': |
| 47 | print 'Supported commands:' |
| 48 | print 'help' |
| 49 | print 'debug' |
| 50 | print 'exit' |
| 51 | for command in commandMap.keys(): |
| 52 | print command |
| 53 | elif cmd == '': |
| 54 | pass |
| 55 | elif cmd == 'debug': |
| 56 | debugMode = True |
| 57 | elif cmd == 'exit': |
| 58 | if debugMode: |
| 59 | debugMode = False |
| 60 | else: |
| 61 | return |
| 62 | else: |
| 63 | cmdList = cmd.split( ' ' ) |
| 64 | if cmdList[ 0 ] in commandMap.keys(): |
| 65 | num = paramNum[ cmdList[ 0 ] ] |
| 66 | name = commandMap[ cmdList[ 0 ] ] |
| 67 | if len( cmdList ) < num + 1: |
| 68 | print 'not enough arguments' |
| 69 | elif len( cmdList ) > num + 1: |
| 70 | print 'Too many arguments' |
| 71 | else: |
| 72 | result = triggerEvent( debugMode, name, 'RUN_BLOCK', cmdList[ 1: ] ) |
| 73 | if result == 10: |
| 74 | pass |
| 75 | elif result == 11: |
| 76 | print "Scheduler busy...Try later or use debugging mode by entering \'debug\'" |
| 77 | elif result == 20: |
| 78 | print "Unknown message to server" |
| 79 | elif result == 21: |
| 80 | print "Unknown event type to server" |
| 81 | elif result == 22: |
| 82 | print "Unknown schedule method to server" |
| 83 | elif result == 23: |
| 84 | print "Not enough argument" |
| 85 | else: |
| 86 | print "Unknown response from server" |
| 87 | else: |
| 88 | print 'Unknown command' |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | import xml.etree.ElementTree |
| 92 | try: |
| 93 | root = xml.etree.ElementTree.parse( '../CHOTestMonkey.params' ).getroot() |
| 94 | except Exception: |
| 95 | print "Uncaught exception!" |
| 96 | for child in root: |
| 97 | if child.tag == 'EVENT': |
| 98 | for event in child: |
| 99 | for item in event: |
| 100 | if item.tag == 'CLI': |
| 101 | CLI = str( item.text ) |
| 102 | if item.tag == 'typeString': |
| 103 | name = str( item.text ) |
| 104 | if item.tag == 'CLIParamNum': |
| 105 | num = int( item.text ) |
| 106 | commandMap[ CLI ] = name |
| 107 | paramNum[ CLI ] = num |
| 108 | startCLI() |
| 109 | |