3 Copyright 2016 Intel Corporation
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
23 from optparse import OptionParser
30 persishist = os.path.expanduser('~/.vpphistory')
31 persishist_size = 1000
33 os.mknod(persishist, stat.S_IFREG)
37 def __init__(self,api_prefix=None):
39 self.api_prefix = api_prefix
41 def historyWrite(self):
43 readline.set_history_length(persishist_size)
44 readline.write_history_file(persishist)
46 def runVat(self, line):
47 input_prefix = "exec "
48 input_command = input_prefix + line
49 line_remove = '^load_one_plugin:'
51 if ( self.api_prefix is None):
52 command = ['vpp_api_test']
54 command = ['vpp_api_test',"chroot prefix %s " % self.api_prefix]
57 command = ['sudo'] + command
59 vpp_process = subprocess.Popen(command,
60 stderr=subprocess.PIPE,
61 stdin=subprocess.PIPE,
62 stdout=subprocess.PIPE)
63 stdout_value = vpp_process.communicate(input_command)[0]
65 buffer_stdout = stdout_value.splitlines()
67 buffer_stdout[:] = [b for b in buffer_stdout
68 if line_remove not in b]
70 for i, num in enumerate(buffer_stdout):
71 buffer_stdout[i] = num.replace('vat# ','')
73 stdout_value = s.join(buffer_stdout)
76 def do_help(self, line):
79 def default(self, line):
82 def do_exit(self, line):
89 def do_EOF(self,line):
91 sys.stdout.write('\n')
95 if readline and os.path.exists(persishist):
96 readline.read_history_file(persishist)
98 def postcmd(self, stop, line):
101 if __name__ == '__main__':
102 parser = OptionParser()
103 parser.add_option("-p","--prefix",action="store",type="string",dest="prefix")
104 (options,command_args) = parser.parse_args(sys.argv)
106 if not len(command_args) > 1:
107 prompt = Vppctl(options.prefix)
110 if sys.stdout.isatty():
111 if(options.prefix is None):
112 prompt.prompt = 'vpp# '
114 prompt.prompt = '%s# ' % options.prefix
116 prompt.cmdloop(red_set + " _______ _ " + norm_set + " _ _____ ___ \n" +
117 red_set + " __/ __/ _ \ (_)__ " + norm_set + " | | / / _ \/ _ \\\n" +
118 red_set + " _/ _// // / / / _ \\" + norm_set + " | |/ / ___/ ___/\n" +
119 red_set + " /_/ /____(_)_/\___/ " + norm_set + "|___/_/ /_/ \n")
120 except KeyboardInterrupt:
121 sys.stdout.write('\n')
125 except KeyboardInterrupt:
126 sys.stdout.write('\n')
129 stdout_value = " ".join(command_args)
130 VatAddress = Vppctl(options.prefix)
131 VatAddress.runVat(stdout_value)