Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 2 | ''' |
| 3 | Copyright 2016 Intel Corporation |
Ed Warnicke | 33007f5 | 2016-04-04 14:37:21 -0700 | [diff] [blame] | 4 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | ''' |
| 17 | |
| 18 | from cmd import Cmd |
| 19 | import os |
| 20 | import subprocess |
| 21 | import re |
| 22 | import sys |
| 23 | try: |
| 24 | import readline |
| 25 | except ImportError: |
| 26 | readline = None |
| 27 | |
| 28 | persishist = os.path.expanduser('~/.vpphistory') |
| 29 | persishist_size = 1000 |
| 30 | if not persishist: |
| 31 | os.mknod(persishist, stat.S_IFREG) |
| 32 | |
| 33 | class Vppctl(Cmd): |
| 34 | |
| 35 | def historyWrite(self): |
| 36 | if readline: |
| 37 | readline.set_history_length(persishist_size) |
| 38 | readline.write_history_file(persishist) |
| 39 | |
| 40 | def runVat(self, line): |
| 41 | input_prefix = "exec " |
| 42 | input_command = input_prefix + line |
| 43 | line_remove = '^load_one_plugin:' |
| 44 | s = '\n' |
Pierre Pfister | f524f0a | 2016-11-03 13:14:29 +0000 | [diff] [blame] | 45 | command = ['vpp_api_test'] |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 46 | |
Pierre Pfister | f524f0a | 2016-11-03 13:14:29 +0000 | [diff] [blame] | 47 | if os.geteuid() != 0: |
| 48 | command = ['sudo', 'vpp_api_test'] |
| 49 | |
| 50 | vpp_process = subprocess.Popen(command, |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 51 | stderr=subprocess.PIPE, |
| 52 | stdin=subprocess.PIPE, |
| 53 | stdout=subprocess.PIPE) |
| 54 | stdout_value = vpp_process.communicate(input_command)[0] |
| 55 | |
| 56 | buffer_stdout = stdout_value.splitlines() |
| 57 | |
| 58 | buffer_stdout[:] = [b for b in buffer_stdout |
| 59 | if line_remove not in b] |
| 60 | |
| 61 | for i, num in enumerate(buffer_stdout): |
| 62 | buffer_stdout[i] = num.replace('vat# ','') |
| 63 | |
| 64 | stdout_value = s.join(buffer_stdout) |
| 65 | print stdout_value |
| 66 | |
| 67 | def do_help(self, line): |
| 68 | self.runVat("help") |
| 69 | |
| 70 | def default(self, line): |
| 71 | self.runVat(line) |
| 72 | |
| 73 | def do_exit(self, line): |
| 74 | self.historyWrite() |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 75 | raise SystemExit |
| 76 | |
| 77 | def emptyline(self): |
| 78 | pass |
| 79 | |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 80 | def do_EOF(self,line): |
| 81 | self.historyWrite() |
| 82 | sys.stdout.write('\n') |
| 83 | raise SystemExit |
| 84 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 85 | def preloop(self): |
| 86 | if readline and os.path.exists(persishist): |
| 87 | readline.read_history_file(persishist) |
| 88 | |
| 89 | def postcmd(self, stop, line): |
| 90 | self.historyWrite() |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | command_args = sys.argv |
| 94 | |
Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 95 | |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 96 | if not len(command_args) > 1: |
| 97 | prompt = Vppctl() |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 98 | red_set = '\033[31m' |
| 99 | norm_set = '\033[0m' |
| 100 | if sys.stdout.isatty(): |
Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 101 | prompt.prompt = 'vpp# ' |
Padraig Connolly | 1f417aa | 2016-11-28 10:21:19 +0000 | [diff] [blame] | 102 | try: |
| 103 | prompt.cmdloop(red_set + " _______ _ " + norm_set + " _ _____ ___ \n" + |
| 104 | red_set + " __/ __/ _ \ (_)__ " + norm_set + " | | / / _ \/ _ \\\n" + |
| 105 | red_set + " _/ _// // / / / _ \\" + norm_set + " | |/ / ___/ ___/\n" + |
| 106 | red_set + " /_/ /____(_)_/\___/ " + norm_set + "|___/_/ /_/ \n") |
| 107 | except KeyboardInterrupt: |
| 108 | sys.stdout.write('\n') |
| 109 | else: |
| 110 | try: |
| 111 | prompt.cmdloop() |
| 112 | except KeyboardInterrupt: |
| 113 | sys.stdout.write('\n') |
Padraig | b21b676 | 2016-09-21 14:59:02 +0100 | [diff] [blame] | 114 | else: |
| 115 | del command_args[0] |
| 116 | stdout_value = " ".join(command_args) |
| 117 | VatAddress = Vppctl() |
| 118 | VatAddress.runVat(stdout_value) |
| 119 | |
Padraig Connolly | fba8a95 | 2016-11-14 11:37:37 +0000 | [diff] [blame] | 120 | |
| 121 | |