Soft Patch Panel
 help / color / mirror / Atom feed
* [spp] [PATCH 00/12] Improve spp controller
@ 2018-03-06 10:39 ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 01/12] spp: add basic commands ogawa.yasufumi
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

This update is to improve usability and fix bugs. SPP controller
supports the least commands and help messages and it's insufficient
to actual usages. This update includes basic UNIX command support
(cd, pwd, ls, etc.), more intuitive help messages and bug fixes.

Yasufumi Ogawa (12):
  spp: add basic commands
  spp: add ls command
  spp: add completion for ls command
  spp: add common completion mehtod in Shell class
  spp: fix bug of completion
  spp: add file type support to compl_common
  spp: fix bug for record command
  spp: add completion for playback command
  spp: add completion for record command
  spp: add mkdir command and its completion
  spp: refactor help messages in Shell class
  spp: refactor logger

 src/spp.py | 263 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 237 insertions(+), 26 deletions(-)

-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 01/12] spp: add basic commands
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 02/12] spp: add ls command ogawa.yasufumi
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Add basic UNIX commands (cd, pwd, exit).

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/spp.py b/src/spp.py
index 84f1fa7..e9df1a3 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -6,6 +6,7 @@ from __future__ import print_function
 import argparse
 import cmd
 import json
+import os
 from Queue import Empty
 from Queue import Queue
 import re
@@ -680,6 +681,16 @@ class Shell(cmd.Cmd, object):
             self.recorded_file.close()
             self.recorded_file = None
 
+    def do_pwd(self, args):
+        print(os.getcwd())
+
+    def do_cd(self, args):
+        if os.path.isdir(args):
+            os.chdir(args)
+            print(os.getcwd())
+        else:
+            print("No such a directory.")
+
     def do_bye(self, arg):
         """Stop recording, close SPP, and exit: BYE"""
 
@@ -694,6 +705,11 @@ class Shell(cmd.Cmd, object):
             self.close()
             return True
 
+    def do_exit(self, args):
+        print('Thank you for using Soft Patch Panel')
+        self.close()
+        return True
+
 
 def main(argv):
     """main"""
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 02/12] spp: add ls command
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 01/12] spp: add basic commands ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 03/12] spp: add completion for " ogawa.yasufumi
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/spp.py b/src/spp.py
index e9df1a3..7b0b64f 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -13,6 +13,7 @@ import re
 import select
 import socket
 import SocketServer
+import subprocess
 import sys
 import threading
 import traceback
@@ -691,6 +692,13 @@ class Shell(cmd.Cmd, object):
         else:
             print("No such a directory.")
 
+    def do_ls(self, args):
+        if args == '' or os.path.isdir(args):
+            c = 'ls %s' % args
+            subprocess.call(c, shell=True)
+        else:
+            print("No such a directory.")
+
     def do_bye(self, arg):
         """Stop recording, close SPP, and exit: BYE"""
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 03/12] spp: add completion for ls command
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 01/12] spp: add basic commands ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 02/12] spp: add ls command ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 04/12] spp: add common completion mehtod in Shell class ogawa.yasufumi
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/src/spp.py b/src/spp.py
index 7b0b64f..06f5032 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -692,9 +692,46 @@ class Shell(cmd.Cmd, object):
         else:
             print("No such a directory.")
 
+    def ls_decorate_dir(self, filelist):
+        res = []
+        for f in filelist:
+            if os.path.isdir(f):
+                res.append('%s/' % f)
+            else:
+                res.append(f)
+        return res
+
+    def complete_ls(self, text, line, begidx, endidx):
+        if text == '':
+            tokens = line.split(' ')
+            target = tokens[-1]
+            if target == '':
+                completions = self.ls_decorate_dir(
+                    os.listdir(os.getcwd()))
+            else:
+                completions = self.ls_decorate_dir(
+                    os.listdir(target))
+        else:
+            tokens = line.split(' ')
+            target = tokens[-1]
+
+            if '/' in target:
+                seg = target.split('/')[-1]
+                target_dir = '/'.join(target.split('/')[0:-1])
+            else:
+                seg = text
+                target_dir = os.getcwd()
+
+            matched = []
+            for t in os.listdir(target_dir):
+                if seg in t:
+                    matched.append(t)
+            completions = self.ls_decorate_dir(matched)
+        return completions
+
     def do_ls(self, args):
         if args == '' or os.path.isdir(args):
-            c = 'ls %s' % args
+            c = 'ls -F %s' % args
             subprocess.call(c, shell=True)
         else:
             print("No such a directory.")
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 04/12] spp: add common completion mehtod in Shell class
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (2 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 03/12] spp: add completion for " ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 05/12] spp: fix bug of completion ogawa.yasufumi
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Completion is required to implementation in methods inspecting parents
or children directories. Therefor, it is better to be an common method
and call from others.

This update for adding common completion and change 'complete_cd' and
'complete_ls' methods to use this common method.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 50 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 17 deletions(-)

diff --git a/src/spp.py b/src/spp.py
index 06f5032..e230b93 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -20,6 +20,7 @@ import traceback
 
 # Turn true if activate logger to debug remote command.
 logger = None
+logger = True
 
 # Maximum num of sock queues for secondaries
 MAX_SECONDARY = 16
@@ -685,32 +686,25 @@ class Shell(cmd.Cmd, object):
     def do_pwd(self, args):
         print(os.getcwd())
 
-    def do_cd(self, args):
-        if os.path.isdir(args):
-            os.chdir(args)
-            print(os.getcwd())
-        else:
-            print("No such a directory.")
-
-    def ls_decorate_dir(self, filelist):
+    def decorate_dir(self, curdir, filelist):
         res = []
         for f in filelist:
-            if os.path.isdir(f):
+            if os.path.isdir('%s/%s' % (curdir, f)):
                 res.append('%s/' % f)
             else:
                 res.append(f)
         return res
 
-    def complete_ls(self, text, line, begidx, endidx):
+    def compl_common(self, text, line, ftype=None):
         if text == '':
             tokens = line.split(' ')
-            target = tokens[-1]
-            if target == '':
-                completions = self.ls_decorate_dir(
-                    os.listdir(os.getcwd()))
+            target_dir = tokens[-1]
+            if target_dir == '':
+                res = self.decorate_dir(
+                    '.', os.listdir(os.getcwd()))
             else:
-                completions = self.ls_decorate_dir(
-                    os.listdir(target))
+                res = self.decorate_dir(
+                    target_dir, os.listdir(target_dir))
         else:
             tokens = line.split(' ')
             target = tokens[-1]
@@ -726,9 +720,21 @@ class Shell(cmd.Cmd, object):
             for t in os.listdir(target_dir):
                 if seg in t:
                     matched.append(t)
-            completions = self.ls_decorate_dir(matched)
+            res = self.decorate_dir(target_dir, matched)
+
+        if ftype is not None:
+            if ftype == 'directory':
+                completions = []
+                for fn in res:
+                    if fn[-1] == '/':
+                        completions.append(fn)
+        else:
+            completions = res
         return completions
 
+    def complete_ls(self, text, line, begidx, endidx):
+        return self.compl_common(text, line)
+
     def do_ls(self, args):
         if args == '' or os.path.isdir(args):
             c = 'ls -F %s' % args
@@ -736,6 +742,16 @@ class Shell(cmd.Cmd, object):
         else:
             print("No such a directory.")
 
+    def complete_cd(self, text, line, begidx, endidx):
+        return self.compl_common(text, line, 'directory')
+
+    def do_cd(self, args):
+        if os.path.isdir(args):
+            os.chdir(args)
+            print(os.getcwd())
+        else:
+            print("No such a directory.")
+
     def do_bye(self, arg):
         """Stop recording, close SPP, and exit: BYE"""
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 05/12] spp: fix bug of completion
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (3 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 04/12] spp: add common completion mehtod in Shell class ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 06/12] spp: add file type support to compl_common ogawa.yasufumi
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

comple_common() returns file name if it matches a given keyword any of
position in the file, for instance, matches keyword 'bc' for file 'abc'.

This update fixes to match only started with keyword.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/spp.py b/src/spp.py
index e230b93..0580066 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -718,7 +718,7 @@ class Shell(cmd.Cmd, object):
 
             matched = []
             for t in os.listdir(target_dir):
-                if seg in t:
+                if t.find(seg) == 0:
                     matched.append(t)
             res = self.decorate_dir(target_dir, matched)
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 06/12] spp: add file type support to compl_common
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (4 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 05/12] spp: fix bug of completion ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 07/12] spp: fix bug for record command ogawa.yasufumi
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Add filtering to exclude directories for completion.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/spp.py b/src/spp.py
index 0580066..46c1613 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -728,6 +728,12 @@ class Shell(cmd.Cmd, object):
                 for fn in res:
                     if fn[-1] == '/':
                         completions.append(fn)
+            elif ftype == 'file':
+                for fn in res:
+                    if fn[-1] != '/':
+                        completions.append(fn)
+            else:
+                completions = res
         else:
             completions = res
         return completions
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 07/12] spp: fix bug for record command
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (5 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 06/12] spp: add file type support to compl_common ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 08/12] spp: add completion for playback command ogawa.yasufumi
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Add entry of exit command to precmd() method to avoid be recorded.
If exit command is recorded, spp controller is terminated immediately
as log file is loaded from playback command.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/spp.py b/src/spp.py
index 46c1613..db43ad5 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -671,7 +671,10 @@ class Shell(cmd.Cmd, object):
 
     def precmd(self, line):
         if self.recorded_file:
-            if not (('playback' in line) or ('bye' in line)):
+            if not (
+                    ('playback' in line) or
+                    ('bye' in line) or
+                    ('exit' in line)):
                 print(line, file=self.recorded_file)
         return line
 
@@ -773,8 +776,8 @@ class Shell(cmd.Cmd, object):
             return True
 
     def do_exit(self, args):
-        print('Thank you for using Soft Patch Panel')
         self.close()
+        print('Thank you for using Soft Patch Panel')
         return True
 
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 08/12] spp: add completion for playback command
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (6 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 07/12] spp: fix bug for record command ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 09/12] spp: add completion for record command ogawa.yasufumi
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 107 +++++++++++++++++++++++++++++++------------------------------
 1 file changed, 55 insertions(+), 52 deletions(-)

diff --git a/src/spp.py b/src/spp.py
index db43ad5..552e921 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -368,6 +368,58 @@ class Shell(cmd.Cmd, object):
     SEC_SUBCMDS = ['vhost', 'ring', 'pcap', 'nullpmd']
     BYE_CMDS = ['sec', 'all']
 
+    def decorate_dir(self, curdir, filelist):
+        res = []
+        for f in filelist:
+            if os.path.isdir('%s/%s' % (curdir, f)):
+                res.append('%s/' % f)
+            else:
+                res.append(f)
+        return res
+
+    def compl_common(self, text, line, ftype=None):
+        if text == '':
+            tokens = line.split(' ')
+            target_dir = tokens[-1]
+            if target_dir == '':
+                res = self.decorate_dir(
+                    '.', os.listdir(os.getcwd()))
+            else:
+                res = self.decorate_dir(
+                    target_dir, os.listdir(target_dir))
+        else:
+            tokens = line.split(' ')
+            target = tokens[-1]
+
+            if '/' in target:
+                seg = target.split('/')[-1]
+                target_dir = '/'.join(target.split('/')[0:-1])
+            else:
+                seg = text
+                target_dir = os.getcwd()
+
+            matched = []
+            for t in os.listdir(target_dir):
+                if t.find(seg) == 0:
+                    matched.append(t)
+            res = self.decorate_dir(target_dir, matched)
+
+        if ftype is not None:
+            completions = []
+            if ftype == 'directory':
+                for fn in res:
+                    if fn[-1] == '/':
+                        completions.append(fn)
+            elif ftype == 'file':
+                for fn in res:
+                    if fn[-1] != '/':
+                        completions.append(fn)
+            else:
+                completions = res
+        else:
+            completions = res
+        return completions
+
     def is_comment_line(self, line):
         input_line = line.strip()
         if len(input_line) > 0:
@@ -648,6 +700,9 @@ class Shell(cmd.Cmd, object):
             self.recorded_file = open(fname, 'w')
             self.response(self.CMD_OK, "record")
 
+    def complete_playback(self, text, line, begidx, endidx):
+        return self.compl_common(text, line)
+
     def do_playback(self, fname):
         """Playback commands from a file:  PLAYBACK filename.cmd"""
 
@@ -689,58 +744,6 @@ class Shell(cmd.Cmd, object):
     def do_pwd(self, args):
         print(os.getcwd())
 
-    def decorate_dir(self, curdir, filelist):
-        res = []
-        for f in filelist:
-            if os.path.isdir('%s/%s' % (curdir, f)):
-                res.append('%s/' % f)
-            else:
-                res.append(f)
-        return res
-
-    def compl_common(self, text, line, ftype=None):
-        if text == '':
-            tokens = line.split(' ')
-            target_dir = tokens[-1]
-            if target_dir == '':
-                res = self.decorate_dir(
-                    '.', os.listdir(os.getcwd()))
-            else:
-                res = self.decorate_dir(
-                    target_dir, os.listdir(target_dir))
-        else:
-            tokens = line.split(' ')
-            target = tokens[-1]
-
-            if '/' in target:
-                seg = target.split('/')[-1]
-                target_dir = '/'.join(target.split('/')[0:-1])
-            else:
-                seg = text
-                target_dir = os.getcwd()
-
-            matched = []
-            for t in os.listdir(target_dir):
-                if t.find(seg) == 0:
-                    matched.append(t)
-            res = self.decorate_dir(target_dir, matched)
-
-        if ftype is not None:
-            if ftype == 'directory':
-                completions = []
-                for fn in res:
-                    if fn[-1] == '/':
-                        completions.append(fn)
-            elif ftype == 'file':
-                for fn in res:
-                    if fn[-1] != '/':
-                        completions.append(fn)
-            else:
-                completions = res
-        else:
-            completions = res
-        return completions
-
     def complete_ls(self, text, line, begidx, endidx):
         return self.compl_common(text, line)
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 09/12] spp: add completion for record command
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (7 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 08/12] spp: add completion for playback command ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 10/12] spp: add mkdir command and its completion ogawa.yasufumi
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/spp.py b/src/spp.py
index 552e921..ef7d998 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -691,6 +691,9 @@ class Shell(cmd.Cmd, object):
             print ("first %s" % cmds[1])
             self.response(self.CMD_ERROR, "invalid format")
 
+    def complete_record(self, text, line, begidx, endidx):
+        return self.compl_common(text, line)
+
     def do_record(self, fname):
         """Save future commands to filename:  RECORD filename.cmd"""
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 10/12] spp: add mkdir command and its completion
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (8 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 09/12] spp: add completion for record command ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 11/12] spp: refactor help messages in Shell class ogawa.yasufumi
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/spp.py b/src/spp.py
index ef7d998..232b528 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -767,6 +767,13 @@ class Shell(cmd.Cmd, object):
         else:
             print("No such a directory.")
 
+    def complete_mkdir(self, text, line, begidx, endidx):
+        return self.compl_common(text, line)
+
+    def do_mkdir(self, args):
+        c = 'mkdir -p %s' % args
+        subprocess.call(c, shell=True)
+
     def do_bye(self, arg):
         """Stop recording, close SPP, and exit: BYE"""
 
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 11/12] spp: refactor help messages in Shell class
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (9 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 10/12] spp: add mkdir command and its completion ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-06 10:39 ` [spp] [PATCH 12/12] spp: refactor logger ogawa.yasufumi
  2018-03-27 23:36 ` [spp] [PATCH 00/12] Improve spp controller Ferruh Yigit
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Each of help messages for command are defined as a comment of 'do_*'
method and referred from help command.

This update is adding or revising help messages for 'do_*' methods.
It also revise other comments in Shell class.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 137 insertions(+), 22 deletions(-)

diff --git a/src/spp.py b/src/spp.py
index 232b528..935616f 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -369,6 +369,11 @@ class Shell(cmd.Cmd, object):
     BYE_CMDS = ['sec', 'all']
 
     def decorate_dir(self, curdir, filelist):
+        """Add '/' the end of dirname for path completion
+
+        'filelist' is a list of files contained in a directory.
+        """
+
         res = []
         for f in filelist:
             if os.path.isdir('%s/%s' % (curdir, f)):
@@ -378,21 +383,38 @@ class Shell(cmd.Cmd, object):
         return res
 
     def compl_common(self, text, line, ftype=None):
-        if text == '':
+        """File path completion for 'complete_*' method
+
+        This method is called from 'complete_*' to complete 'do_*'.
+        'text' and 'line' are arguments of 'complete_*'.
+
+        `complete_*` is a member method of builtin Cmd class and
+        called if tab key is pressed in a command defiend by 'do_*'.
+        'text' and 'line' are contents of command line.
+        For example, if you type tab at 'command arg1 ar',
+        last token 'ar' is assigned to 'text' and whole line
+        'command arg1 ar' is assigned to 'line'.
+
+        NOTE:
+        If tab is typed after '/', empty text '' is assigned to
+        'text'. For example 'aaa b/', text is not 'b/' but ''.
+        """
+
+        if text == '':  # tab is typed after command name or '/'
             tokens = line.split(' ')
-            target_dir = tokens[-1]
-            if target_dir == '':
+            target_dir = tokens[-1]  # get dirname for competion
+            if target_dir == '':  # no dirname means current dir
                 res = self.decorate_dir(
                     '.', os.listdir(os.getcwd()))
-            else:
+            else:  # after '/'
                 res = self.decorate_dir(
                     target_dir, os.listdir(target_dir))
-        else:
+        else:  # tab is typed in the middle of a word
             tokens = line.split(' ')
-            target = tokens[-1]
+            target = tokens[-1]  # target dir for completion
 
-            if '/' in target:
-                seg = target.split('/')[-1]
+            if '/' in target:  # word is a path such as 'path/to/file'
+                seg = target.split('/')[-1]  # word to be completed
                 target_dir = '/'.join(target.split('/')[0:-1])
             else:
                 seg = text
@@ -400,11 +422,11 @@ class Shell(cmd.Cmd, object):
 
             matched = []
             for t in os.listdir(target_dir):
-                if t.find(seg) == 0:
+                if t.find(seg) == 0:  # get words matched with 'seg'
                     matched.append(t)
             res = self.decorate_dir(target_dir, matched)
 
-        if ftype is not None:
+        if ftype is not None:  # filtering by ftype
             completions = []
             if ftype == 'directory':
                 for fn in res:
@@ -421,6 +443,14 @@ class Shell(cmd.Cmd, object):
         return completions
 
     def is_comment_line(self, line):
+        """Find commend line to not to interpret as a command
+
+        Return True if given line is a comment, or False.
+        Supported comment styles are
+          * python ('#')
+          * C ('//')
+        """
+
         input_line = line.strip()
         if len(input_line) > 0:
             if (input_line[0] == '#') or (input_line[0:2] == '//'):
@@ -431,10 +461,8 @@ class Shell(cmd.Cmd, object):
     def default(self, line):
         """Define defualt behaviour
 
-        If user input is commend styled, controller simply echo as a comment.
-        Supported styles are
-          - python ('#')
-          - C ('//')
+        If user input is commend styled, controller simply echo
+        as a comment.
         """
 
         if self.is_comment_line(line):
@@ -451,7 +479,7 @@ class Shell(cmd.Cmd, object):
         pass
 
     def close_all_secondary(self):
-        """Exit all secondary processes"""
+        """Terminate all secondary processes"""
 
         global SECONDARY_COUNT
         global SECONDARY_LIST
@@ -464,13 +492,19 @@ class Shell(cmd.Cmd, object):
         SECONDARY_COUNT = 0
 
     def get_status(self):
+        """Return status of primary and secondary processes
+
+        It is called from do_status() method and return primary status
+        and a list of secondary processes as status.
+        """
+
         global SECONDARY_LIST
 
         secondary = []
         for i in SECONDARY_LIST:
             secondary.append("%d" % i)
         stat = {
-            "primary": "%d" % PRIMARY,
+            "primary": "%d" % PRIMARY,  # PRIMARY is 1 if it is running
             "secondary": secondary
             }
         return stat
@@ -637,6 +671,7 @@ class Shell(cmd.Cmd, object):
 
     def response(self, result, message):
         """Enqueue message from other than CLI"""
+
         try:
             rcmd = RCMD_EXECUTE_QUEUE.get(False)
         except Empty:
@@ -650,14 +685,23 @@ class Shell(cmd.Cmd, object):
                 logger.debug("unknown remote command = %s" % rcmd)
 
     def do_status(self, _):
-        """Display Soft Patch Panel Status"""
+        """Display status info of SPP processes
+
+        spp > status
+        """
 
         self.print_status()
         stat = self.get_status()
         self.response(self.CMD_OK, json.dumps(stat))
 
     def do_pri(self, command):
-        """Send command to primary process"""
+        """Send command to primary process
+
+        Spp primary takes sub commands.
+
+        spp > pri;status
+        spp > pri;clear
+        """
 
         if command and command in self.PRI_CMDS:
             result, message = self.command_primary(command)
@@ -668,7 +712,15 @@ class Shell(cmd.Cmd, object):
             self.response(self.CMD_ERROR, message)
 
     def do_sec(self, arg):
-        """Send command to secondary process"""
+        """Send command to secondary process
+
+        SPP secondary process is specified with secondary ID and takes
+        sub commands.
+
+        spp > sec 1;status
+        spp > sec 1;add ring 0
+        spp > sec 1;patch 0 2
+        """
 
         # remove unwanted spaces to avoid invalid command error
         tmparg = clean_sec_cmd(arg)
@@ -695,7 +747,15 @@ class Shell(cmd.Cmd, object):
         return self.compl_common(text, line)
 
     def do_record(self, fname):
-        """Save future commands to filename:  RECORD filename.cmd"""
+        """Save commands to a log file
+
+        Save command history to a log file for loading from playback
+        command later as a config file.
+        Config is a series of SPP command and you can also create it
+        from scratch without playback command.
+
+        spp > record path/to/file
+        """
 
         if fname == '':
             print("Record file is required!")
@@ -707,7 +767,13 @@ class Shell(cmd.Cmd, object):
         return self.compl_common(text, line)
 
     def do_playback(self, fname):
-        """Playback commands from a file:  PLAYBACK filename.cmd"""
+        """Load a config file to reproduce network configuration
+
+        Config is a series of SPP command and you can also create it
+        from scratch without playback command.
+
+        spp > playback path/to/config
+        """
 
         if fname == '':
             print("Record file is required!")
@@ -728,6 +794,11 @@ class Shell(cmd.Cmd, object):
                 self.response(self.CMD_NG, message)
 
     def precmd(self, line):
+        """Called before running a command
+
+        It is called for checking a contents of command line.
+        """
+
         if self.recorded_file:
             if not (
                     ('playback' in line) or
@@ -745,12 +816,26 @@ class Shell(cmd.Cmd, object):
             self.recorded_file = None
 
     def do_pwd(self, args):
+        """Show corrent directory
+
+        It behaves as UNIX's pwd command.
+
+        spp > pwd
+        """
+
         print(os.getcwd())
 
     def complete_ls(self, text, line, begidx, endidx):
         return self.compl_common(text, line)
 
     def do_ls(self, args):
+        """Show a list of specified directory
+
+        It behaves as UNIX's ls command.
+
+        spp > ls path/to/dir
+        """
+
         if args == '' or os.path.isdir(args):
             c = 'ls -F %s' % args
             subprocess.call(c, shell=True)
@@ -761,6 +846,11 @@ class Shell(cmd.Cmd, object):
         return self.compl_common(text, line, 'directory')
 
     def do_cd(self, args):
+        """Change current directory
+
+        spp > cd path/to/dir
+        """
+
         if os.path.isdir(args):
             os.chdir(args)
             print(os.getcwd())
@@ -771,11 +861,30 @@ class Shell(cmd.Cmd, object):
         return self.compl_common(text, line)
 
     def do_mkdir(self, args):
+        """Create a new directory
+
+        It behaves as 'mkdir -p'.
+
+        spp > mkdir path/to/dir
+        """
+
         c = 'mkdir -p %s' % args
         subprocess.call(c, shell=True)
 
     def do_bye(self, arg):
-        """Stop recording, close SPP, and exit: BYE"""
+        """Terminate SPP processes and controller
+
+        It also terminates logging if you activate recording.
+
+        (1) Terminate secondary processes
+        spp > bye sec
+
+        (2) Terminate primary and secondary processes
+        spp > bye all
+
+        (3) Terminate SPP controller (not for primary and secondary)
+        spp > bye
+        """
 
         cmds = arg.split(' ')
         if cmds[0] == 'sec':
@@ -789,6 +898,12 @@ class Shell(cmd.Cmd, object):
             return True
 
     def do_exit(self, args):
+        """Terminate SPP controller
+
+        It is an alias for bye command and same as bye command.
+
+        spp > exit
+        """
         self.close()
         print('Thank you for using Soft Patch Panel')
         return True
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [spp] [PATCH 12/12] spp: refactor logger
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (10 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 11/12] spp: refactor help messages in Shell class ogawa.yasufumi
@ 2018-03-06 10:39 ` ogawa.yasufumi
  2018-03-27 23:36 ` [spp] [PATCH 00/12] Improve spp controller Ferruh Yigit
  12 siblings, 0 replies; 14+ messages in thread
From: ogawa.yasufumi @ 2018-03-06 10:39 UTC (permalink / raw)
  To: ferruh.yigit, spp; +Cc: Yasufumi Ogawa

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 src/spp.py | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/src/spp.py b/src/spp.py
index 935616f..73752bd 100755
--- a/src/spp.py
+++ b/src/spp.py
@@ -20,7 +20,17 @@ import traceback
 
 # Turn true if activate logger to debug remote command.
 logger = None
-logger = True
+
+if logger is True:
+    import logging
+    logger = logging.getLogger(__name__)
+    handler = logging.StreamHandler()
+    handler.setLevel(logging.DEBUG)
+    formatter = logging.Formatter(
+        '%(asctime)s,[%(filename)s][%(name)s][%(levelname)s]%(message)s')
+    handler.setFormatter(formatter)
+    logger.setLevel(logging.DEBUG)
+    logger.addHandler(handler)
 
 # Maximum num of sock queues for secondaries
 MAX_SECONDARY = 16
@@ -971,18 +981,5 @@ def main(argv):
 
 
 if __name__ == "__main__":
-    if logger is True:
-        from logging import DEBUG
-        from logging import Formatter
-        from logging import getLogger
-        from logging import StreamHandler
-        logger = getLogger(__name__)
-        handler = StreamHandler()
-        handler.setLevel(DEBUG)
-        formatter = Formatter(
-            '%(asctime)s,[%(filename)s][%(name)s][%(levelname)s]%(message)s')
-        handler.setFormatter(formatter)
-        logger.setLevel(DEBUG)
-        logger.addHandler(handler)
 
     main(sys.argv[1:])
-- 
2.13.1

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [spp] [PATCH 00/12] Improve spp controller
  2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
                   ` (11 preceding siblings ...)
  2018-03-06 10:39 ` [spp] [PATCH 12/12] spp: refactor logger ogawa.yasufumi
@ 2018-03-27 23:36 ` Ferruh Yigit
  12 siblings, 0 replies; 14+ messages in thread
From: Ferruh Yigit @ 2018-03-27 23:36 UTC (permalink / raw)
  To: ogawa.yasufumi, spp

On 3/6/2018 10:39 AM, ogawa.yasufumi@lab.ntt.co.jp wrote:
> From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
> 
> This update is to improve usability and fix bugs. SPP controller
> supports the least commands and help messages and it's insufficient
> to actual usages. This update includes basic UNIX command support
> (cd, pwd, ls, etc.), more intuitive help messages and bug fixes.
> 
> Yasufumi Ogawa (12):
>   spp: add basic commands
>   spp: add ls command
>   spp: add completion for ls command
>   spp: add common completion mehtod in Shell class
>   spp: fix bug of completion
>   spp: add file type support to compl_common
>   spp: fix bug for record command
>   spp: add completion for playback command
>   spp: add completion for record command
>   spp: add mkdir command and its completion
>   spp: refactor help messages in Shell class
>   spp: refactor logger

Series applied thanks.

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-03-27 23:36 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-06 10:39 [spp] [PATCH 00/12] Improve spp controller ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 01/12] spp: add basic commands ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 02/12] spp: add ls command ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 03/12] spp: add completion for " ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 04/12] spp: add common completion mehtod in Shell class ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 05/12] spp: fix bug of completion ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 06/12] spp: add file type support to compl_common ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 07/12] spp: fix bug for record command ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 08/12] spp: add completion for playback command ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 09/12] spp: add completion for record command ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 10/12] spp: add mkdir command and its completion ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 11/12] spp: refactor help messages in Shell class ogawa.yasufumi
2018-03-06 10:39 ` [spp] [PATCH 12/12] spp: refactor logger ogawa.yasufumi
2018-03-27 23:36 ` [spp] [PATCH 00/12] Improve spp controller Ferruh Yigit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).