platform: rewrote vppctl script to include history 40/3040/6
authorPadraig <[email protected]>
Wed, 21 Sep 2016 13:59:02 +0000 (14:59 +0100)
committerDamjan Marion <[email protected]>
Mon, 10 Oct 2016 19:48:38 +0000 (19:48 +0000)
Added more ability to search history to vppctl shell
*Up and down keys give history
*Script now written in Python 2.7.6
*Contains all original functionality
*Added Python dependency for deb/rpms

Change-Id: I5088f7b018fce92b9b5411df0bffc34709810dec
Signed-off-by: Padraig Connolly <[email protected]>
build-root/deb/debian/control
build-root/rpm/vpp.spec
vpp-api-test/scripts/vppctl

index bc19768..4be6c4a 100644 (file)
@@ -7,7 +7,7 @@ Standards-Version: 3.9.4
 
 Package: vpp
 Architecture: any
-Depends: vpp-lib (= ${source:Version}), ${shlibs:Depends}, ${misc:Depends}
+Depends: vpp-lib (= ${source:Version}), ${shlibs:Depends}, ${misc:Depends}, ${python:Depends}
 Description: Vector Packet Processing--executables
  This package provides VPP executables: vpp, vpp_api_test, vpp_json_test
  vpp - the vector packet engine
index f55ca66..d365060 100644 (file)
@@ -22,7 +22,7 @@ Summary: Vector Packet Processing
 License: MIT
 Version: %{_version}
 Release: %{_release}
-Requires: vpp-lib = %{_version}-%{_release}, net-tools, pciutils
+Requires: vpp-lib = %{_version}-%{_release}, net-tools, pciutils, python
 
 %description
 This package provides VPP executables: vpp, vpp_api_test, vpp_json_test
index c348fff..160bdf7 100755 (executable)
@@ -1,30 +1,96 @@
-#!/bin/bash
-PREFIX_ARGS=""
-CMD_ARGS=""
-while [[ $# -gt 0 ]]; do
-    arg=$1
-    shift
-    case ${arg} in
-       --prefix|-p)
-           PREFIX_ARGS="chroot prefix ${1}"
-           shift
-       ;;
-       *)
-           CMD_ARGS="$CMD_ARGS ${arg}"
-        ;;
-    esac
-done
-
-if [ "x${CMD_ARGS}" != "x" ]; then
-    echo exec ${CMD_ARGS} | vpp_api_test $PREFIX_ARGS 2> >(grep -v "^load_one_plugin:")| sed 's/vat# //g'
-else
-    echo -n "vpp# "
-    while read CMD; do
-        if [ "x$CMD" == "xexit" ]; then
-            exit
-        elif [ "x$CMD" != "x" ]; then
-            echo exec $CMD | vpp_api_test $PREFIX_ARGS 2> >(grep -v "^load_one_plugin:")| sed 's/vat# //g'
-        fi
-        echo -n "vpp# "
-    done
-fi
+#!/usr/bin/python
+'''
+Copyright 2016 Intel Corporation
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+from cmd import Cmd
+import os
+import subprocess
+import re
+import sys
+try:
+        import readline
+except ImportError:
+        readline = None
+
+persishist = os.path.expanduser('~/.vpphistory')
+persishist_size = 1000
+if not persishist:
+        os.mknod(persishist, stat.S_IFREG)
+
+class Vppctl(Cmd):
+
+        def historyWrite(self):
+                if readline:
+                        readline.set_history_length(persishist_size)
+                        readline.write_history_file(persishist)
+
+        def runVat(self, line):
+                input_prefix = "exec "
+                input_command = input_prefix + line
+                line_remove = '^load_one_plugin:'
+                s = '\n'
+
+                vpp_process = subprocess.Popen(['sudo', 'vpp_api_test'],
+                        stderr=subprocess.PIPE,
+                        stdin=subprocess.PIPE,
+                        stdout=subprocess.PIPE)
+                stdout_value = vpp_process.communicate(input_command)[0]
+
+                buffer_stdout = stdout_value.splitlines()
+
+                buffer_stdout[:]  = [b for b in buffer_stdout
+                        if line_remove not in b]
+
+                for i, num in enumerate(buffer_stdout):
+                        buffer_stdout[i] = num.replace('vat# ','')
+
+                stdout_value = s.join(buffer_stdout)
+                print stdout_value
+
+        def do_help(self, line):
+                self.runVat("help")
+
+        def default(self, line):
+                self.runVat(line)
+
+        def do_exit(self, line):
+                self.historyWrite()
+
+                raise SystemExit
+
+        def emptyline(self):
+                pass
+
+        def preloop(self):
+                if readline and os.path.exists(persishist):
+                        readline.read_history_file(persishist)
+
+        def postcmd(self, stop, line):
+                self.historyWrite()
+
+if __name__ == '__main__':
+        command_args = sys.argv
+
+        if not len(command_args) > 1:
+                prompt = Vppctl()
+                prompt.prompt = 'vpp# '
+                prompt.cmdloop("Starting Vppctl...")
+        else:
+                del command_args[0]
+                stdout_value = " ".join(command_args)
+                VatAddress = Vppctl()
+                VatAddress.runVat(stdout_value)
+