summaryrefslogtreecommitdiff
path: root/tools/cmd_check.py
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2016-10-24 22:55:44 +0000
committerQuentin Young <qlyoung@cumulusnetworks.com>2016-10-24 22:55:44 +0000
commit71f11720efcb373d5ce312c9b7fb988405b2e97b (patch)
tree8dbaa343c3d55904b148919bc45fcb589d89f92e /tools/cmd_check.py
parentb09bceec68e9963b0bffc70c04f06d307d474adb (diff)
tools: Add tool to check for uninstalled commands
cmd_check.py parses source files looking for commands that are defined but not installed in any node. Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'tools/cmd_check.py')
-rw-r--r--tools/cmd_check.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/tools/cmd_check.py b/tools/cmd_check.py
new file mode 100644
index 0000000000..ccfb2964bb
--- /dev/null
+++ b/tools/cmd_check.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python3
+
+# Parses a source file, ensuring that CLI definitions (DEFUNs, ALIASs, etc)
+# have install_command called on them at some point.
+import sys
+import glob
+import re
+import os
+from pprint import pprint
+
+# searching regex
+search = [
+ r'DEF.*\(.*\n\s*(.*_cmd)',
+ r'ALIAS.*\(.*\n\s*(.*_cmd)',
+]
+
+def process(filename):
+ cmds = []
+ notinstalled = []
+ with open(filename) as cf:
+ try:
+ tf = cf.read()
+ except:
+ print('Error reading {0}, skipping'.format(filename))
+ return
+ # build list of defined commands
+ for expression in search:
+ for match in re.findall(expression, tf, re.M):
+ cmds.append(match)
+ # build list of not installed commands
+ notinstalled = filter(
+ lambda x: len(re.findall('install_element.*\(.*{0}'.format(x), tf, re.M)) == 0,
+ cmds)
+ notinstalled = list(notinstalled)
+
+ if len(notinstalled) > 0:
+ print('\033[92m', end='')
+ print('{0}'.format(filename))
+ print('\033[0m', end='')
+ for cmd in notinstalled:
+ print(' {0}'.format(cmd))
+ print('')
+
+usage = """
+Usage:
+ ./cmd_check.py <path> [<path>...]
+
+ where 'path' is a C source file or directory
+ containing C source files
+"""
+
+if __name__ == '__main__':
+ if len(sys.argv) < 2:
+ print(usage)
+ exit()
+
+ cwd = os.getcwd()
+ for arg in sys.argv[1:]:
+ # collect all c files
+ globstr = arg
+ if os.path.isdir(arg):
+ os.chdir(arg)
+ globstr = '*.c'
+ for filename in glob.glob(globstr):
+ process(filename)
+ os.chdir(cwd)