blob: 160bdf7ce5eabdba88e855e001af90b58350dae4 [file] [log] [blame]
Padraigb21b6762016-09-21 14:59:02 +01001#!/usr/bin/python
2'''
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'
45
46 vpp_process = subprocess.Popen(['sudo', 'vpp_api_test'],
47 stderr=subprocess.PIPE,
48 stdin=subprocess.PIPE,
49 stdout=subprocess.PIPE)
50 stdout_value = vpp_process.communicate(input_command)[0]
51
52 buffer_stdout = stdout_value.splitlines()
53
54 buffer_stdout[:] = [b for b in buffer_stdout
55 if line_remove not in b]
56
57 for i, num in enumerate(buffer_stdout):
58 buffer_stdout[i] = num.replace('vat# ','')
59
60 stdout_value = s.join(buffer_stdout)
61 print stdout_value
62
63 def do_help(self, line):
64 self.runVat("help")
65
66 def default(self, line):
67 self.runVat(line)
68
69 def do_exit(self, line):
70 self.historyWrite()
71
72 raise SystemExit
73
74 def emptyline(self):
75 pass
76
77 def preloop(self):
78 if readline and os.path.exists(persishist):
79 readline.read_history_file(persishist)
80
81 def postcmd(self, stop, line):
82 self.historyWrite()
83
84if __name__ == '__main__':
85 command_args = sys.argv
86
87 if not len(command_args) > 1:
88 prompt = Vppctl()
89 prompt.prompt = 'vpp# '
90 prompt.cmdloop("Starting Vppctl...")
91 else:
92 del command_args[0]
93 stdout_value = " ".join(command_args)
94 VatAddress = Vppctl()
95 VatAddress.runVat(stdout_value)
96