Script to generate Test result into CSV file

This script will read the user supplied output.xml of the robot test
result using robot framework API and writes the test data in a format
and creats a *.csv file to user supplied destination directory.

Resolves Partially openbmc/openbmc-test-automation#151

Change-Id: I61dcd5d7ed73c4dac70f4076d706e7f63fdc7f96
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/tools/oem/ibm/gen_csv_results.py b/tools/oem/ibm/gen_csv_results.py
new file mode 100755
index 0000000..5ecb853
--- /dev/null
+++ b/tools/oem/ibm/gen_csv_results.py
@@ -0,0 +1,125 @@
+#!/usr/bin/python
+r"""
+#############################################################
+#    @brief   Uses robot framework API to extract test result
+#             data from output.xml generated by robot tests.
+#             For more information on the Robot Framework API
+#             http://robot-framework.readthedocs.io/en/3.0
+#             /autodoc/robot.result.html
+#############################################################
+"""
+import sys
+import getopt
+import csv
+from datetime import datetime
+import robot.errors
+from robot.api import ExecutionResult
+from robot.result.visitor import ResultVisitor
+
+
+def parse_output_xml(i_source, i_dest):
+    result = ExecutionResult(i_source)
+    result.configure(stat_config={'suite_stat_level': 2,
+                                  'tag_stat_combine': 'tagANDanother'})
+
+    stats = result.statistics
+    print "--------------------------------------"
+    print "Total Test Count:\t",\
+          stats.total.critical.passed + stats.total.critical.failed
+    print "Total Test Failed:\t", stats.total.critical.failed
+    print "Total Test Passed:\t", stats.total.critical.passed
+    print "Test Start Time:\t", result.suite.starttime
+    print "Test End Time:\t\t", result.suite.endtime
+    print "--------------------------------------"
+
+    # Use ResultVisitor object and save off the test data info
+    class TestResult(ResultVisitor):
+        def __init__(self):
+            self.testData = []
+
+        def visit_test(self, test):
+            self.testData += [test]
+
+    collectDataObj = TestResult()
+    result.visit(collectDataObj)
+
+    # Write the result statistics attributes to CSV file
+    l_csvlist = []
+    l_subsys = 'OPENBMC'
+    l_test_type = 'FTC'
+    l_pse_rel = 'OBMC910'
+
+    # Default header
+    l_header = ['test_start', 'test_end', 'subsys', 'test_type',
+                'test_result', 'test_name', 'pse_rel']
+
+    l_csvlist.append(l_header)
+
+    # Generate CSV file onto the path with current time stamp
+    l_base_dir = i_dest
+    l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
+    l_csvfile = l_base_dir + l_timestamp + ".csv"
+
+    print "Writing data into csv file:", l_csvfile
+
+    for testcase in collectDataObj.testData:
+        with open(l_csvfile, "w") as output:
+            writer = csv.writer(output, lineterminator='\n')
+            writer.writerows(l_csvlist)
+
+        # Test category : Test case type
+        l_test_name = str(testcase.parent) + ":" + str(testcase)
+
+        # Test Result pass=0 fail=1
+        if testcase.status == 'PASS':
+            l_test_result = 0
+        else:
+            l_test_result = 1
+
+        # Data Sequence: test_start,test_end,subsys,test_type,
+        #                test_result,test_name,pse_rel
+        l_data = [testcase.starttime, testcase.endtime, l_subsys,
+                  l_test_type, l_test_result, l_test_name, l_pse_rel]
+        l_csvlist.append(l_data)
+
+    l_csvlist.close()
+
+
+def usage():
+    name = 'gen_csv_results.py'
+    print 'Usage: '
+    print name, '-s <source path> -d <destination path>'
+    print '\t-s | --source=  : output.xml robot test result file path'
+    print '\t-d | --dest=    : Where the *.csv file will be generated'
+    sys.exit()
+
+
+def main(argv):
+
+    source = ''
+    dest = ''
+    try:
+        opts, args = getopt.getopt(argv, "h:s:d:", ["source=", "dest="])
+    except getopt.GetoptError:
+        usage()
+
+    for opt, arg in opts:
+        if opt == '-h':
+            usage()
+        elif opt in ("-s", "--source"):
+            source = arg
+        elif opt in ("-d", "--dest"):
+            dest = arg
+
+    if source == '':
+        usage()
+        print 'ERROR: Provide input file path to robot generated output.xml '
+
+    if dest == '':
+        usage()
+        print 'ERROR: Destination directory where *.csv file will be generated'
+
+    parse_output_xml(source, dest)
+
+if __name__ == "__main__":
+    main(sys.argv[1:])