Tool for checking that .robot files went through robot.tidy 83/27583/5
authorJozef Behran <jbehran@cisco.com>
Tue, 29 Sep 2015 13:07:39 +0000 (15:07 +0200)
committerJozef Behran <jbehran@cisco.com>
Mon, 5 Oct 2015 11:58:15 +0000 (13:58 +0200)
This adds a tool that traverses the specified directories and
either checks that all of the .robot files were passed
through the robot.tidy tool (when "check" is specified as the
command) or performs the "tidying" (when "tidy" is specified
as command). A convenience shell script is provided that
orders the tool to traverse the entire test project and check
(or tidy them if "tidy" argument is given to it) all .robot
files it finds whether they are tidy.

I built this tool in about 2 hours after I got bugged by "did
you run the test through 'tidy'" kinds of requests. When the
"check" version is wired to the validation jobs, this kind of
answers can now be obtained automatically.

Change-Id: I8540f14ba81b55394f2a3285130dea6e945d6649
Signed-off-by: Jozef Behran <jbehran@cisco.com>
tools/robot_check/tidy.sh [new file with mode: 0644]
tools/robot_check/tidytool.py [new file with mode: 0644]

diff --git a/tools/robot_check/tidy.sh b/tools/robot_check/tidy.sh
new file mode 100644 (file)
index 0000000..0c33747
--- /dev/null
@@ -0,0 +1,21 @@
+# Convenience script to run check on all .robot files in the project.
+# Run with the "quiet" argument to get rid of the non-error output.
+
+if test "${1-loud}" = "quiet"; then
+  COMMAND="quiet"
+elif test "${1-loud}" = "tidy"; then
+  COMMAND="tidy"
+else
+  COMMAND="check"
+fi
+BASE=`dirname ${0}`
+cd $BASE
+BASE=`pwd`
+cd ../..
+DIRLIST=""
+for Dir in *; do
+  if test -d $Dir; then
+    DIRLIST="$DIRLIST $Dir"
+  fi
+done
+python $BASE/tidytool.py $COMMAND $DIRLIST
diff --git a/tools/robot_check/tidytool.py b/tools/robot_check/tidytool.py
new file mode 100644 (file)
index 0000000..1f69ca2
--- /dev/null
@@ -0,0 +1,109 @@
+import os
+import sys
+import stat
+import robot.tidy
+
+
+def Error(FileSpec, Message):
+    global ErrorsReported
+    print "File", FileSpec + ":", Message
+    ErrorsReported = True
+
+
+def traverse_and_process(DirList, Processor):
+    """Traverse the directory and process all .robot files found"""
+    Stack = []
+    for Dir in DirList:
+        if Dir[-1] == "/":
+            Dir = Dir[:-1]
+        Stack.append(Dir)
+    while len(Stack) > 0:
+        Dir = Stack.pop() + "/"
+        try:
+            List = os.listdir(Dir)
+        except (IOError, OSError), e:
+            Error(Dir, "Directory not accessible: " + str(e))
+            continue
+        for Item in List:
+            Spec = Dir + Item
+            Stat = os.stat(Spec)
+            if stat.S_ISDIR(Stat.st_mode):
+                Stack.append(Spec)
+            elif Item.endswith(".robot"):
+                Processor(Spec)
+
+
+def check_quietly(FileSpec):
+    try:
+        Data = open(FileSpec).read()
+    except (IOError, OSError), e:
+        Error(FileSpec, "Not accessible: " + str(e))
+        return
+    TidyTool = robot.tidy.Tidy()
+    CleanedData = TidyTool.file(FileSpec)
+    if Data != CleanedData:
+        Error(FileSpec, "Found to be untidy")
+
+
+def check(FileSpec):
+    Index = FileSpec.rfind("/")
+    FileName = FileSpec
+    if Index >= 0:
+        FileName = FileSpec[Index + 1:]
+    sys.stdout.write("  " + FileName + "\r")
+    sys.stdout.flush()
+    check_quietly(FileSpec)
+    sys.stdout.write(" " * (2 + len(FileName)) + "\r")
+
+
+def tidy(FileSpec):
+    print "Processing file:", FileSpec
+    TidyTool = robot.tidy.Tidy()
+    try:
+        CleanedData = TidyTool.file(FileSpec)
+        open(FileSpec, "w").write(CleanedData)
+    except (IOError, OSError), e:
+        Error(FileSpec, "Not accessible: " + str(e))
+
+
+# TODO: Refactor the command line argument parsing to use argparse. Since I
+#       wanted to just quickly make this tool to get rid of manual robot.tidy
+#       runs I did not have time to create polished argparse based command
+#       line argument parsing code. Remember also to update the convenience
+#       scripts.
+
+
+def usage():
+    print "Usage:\ttidytool.py <command>k <dir1> [<dir2> <dir3> ...]"
+    print
+    print "where <command> is one of these:"
+    print
+    print "check\tCheck that the Robot test data is tidy."
+    print "quiet\tCheck quietly that the Robot test data is tidy."
+    print "tidy\tTidy the Robot test data."
+    print
+    print "The program traverses the specified directories, searching for"
+    print "Robot test data (.robot files) and performing the specified"
+    print "command on them."
+
+
+if __name__ == "__main__":
+    if len(sys.argv) < 2:
+        usage()
+        raise SystemExit
+    Command = sys.argv[1]
+    DirList = sys.argv[2:]
+    if Command == "check":
+        Processor = check
+    elif Command == "quiet":
+        Processor = check_quietly
+    elif Command == "tidy":
+        Processor = tidy
+    else:
+        print "Unrecognized command:", Command
+        sys.exit(1)
+    ErrorsReported = False
+    traverse_and_process(DirList, Processor)
+    if ErrorsReported:
+        print "tidytool run FAILED !!!"
+        sys.exit(1)