blob: 872590ccfe71fe374a6c807b9d3f2542af298b06 [file] [log] [blame]
Padraig Connollyfba8a952016-11-14 11:37:37 +00001#! /usr/bin/python
Padraigb21b6762016-09-21 14:59:02 +01002'''
3Copyright 2016 Intel Corporation
Ed Warnicke33007f52016-04-04 14:37:21 -07004
Padraigb21b6762016-09-21 14:59:02 +01005Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16'''
17
18from cmd import Cmd
19import os
20import subprocess
21import re
22import sys
23try:
24 import readline
25except ImportError:
26 readline = None
27
28persishist = os.path.expanduser('~/.vpphistory')
29persishist_size = 1000
30if not persishist:
31 os.mknod(persishist, stat.S_IFREG)
32
33class 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 Pfisterf524f0a2016-11-03 13:14:29 +000045 command = ['vpp_api_test']
Padraigb21b6762016-09-21 14:59:02 +010046
Pierre Pfisterf524f0a2016-11-03 13:14:29 +000047 if os.geteuid() != 0:
48 command = ['sudo', 'vpp_api_test']
49
50 vpp_process = subprocess.Popen(command,
Padraigb21b6762016-09-21 14:59:02 +010051 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()
75
76 raise SystemExit
77
78 def emptyline(self):
79 pass
80
81 def preloop(self):
82 if readline and os.path.exists(persishist):
83 readline.read_history_file(persishist)
84
85 def postcmd(self, stop, line):
86 self.historyWrite()
87
88if __name__ == '__main__':
89 command_args = sys.argv
90
Padraig Connollyfba8a952016-11-14 11:37:37 +000091
Padraigb21b6762016-09-21 14:59:02 +010092 if not len(command_args) > 1:
93 prompt = Vppctl()
Padraig Connollyfba8a952016-11-14 11:37:37 +000094 red_set = '\033[31m'
95 norm_set = '\033[0m'
96 if sys.stdout.isatty():
97 prompt.prompt = 'vpp# '
98 prompt.cmdloop(red_set + " _______ _ " + norm_set + " _ _____ ___ \n" +
99 red_set + " __/ __/ _ \ (_)__ " + norm_set + " | | / / _ \/ _ \\\n" +
100 red_set + " _/ _// // / / / _ \\" + norm_set + " | |/ / ___/ ___/\n" +
101 red_set + " /_/ /____(_)_/\___/ " + norm_set + "|___/_/ /_/ \n")
102 else:
103 prompt.cmdloop()
Padraigb21b6762016-09-21 14:59:02 +0100104 else:
105 del command_args[0]
106 stdout_value = " ".join(command_args)
107 VatAddress = Vppctl()
108 VatAddress.runVat(stdout_value)
109
Padraig Connollyfba8a952016-11-14 11:37:37 +0000110
111