sanitycheck: export list of tests as CSV

Export tests to a file with Section, subsection and identifier as
columns making it easy to import testcases into test management system.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
diff --git a/scripts/sanitycheck b/scripts/sanitycheck
index dab6a58..214127c 100755
--- a/scripts/sanitycheck
+++ b/scripts/sanitycheck
@@ -2404,6 +2404,10 @@
     parser.add_argument("--list-tests", action="store_true",
             help="list all tests.")
 
+    parser.add_argument("--export-tests", action="store",
+            metavar="FILENAME",
+            help="Export tests case meta-data to a file in CSV format.")
+
     parser.add_argument("--detailed-report",
             action="store",
             metavar="FILENAME",
@@ -2721,6 +2725,34 @@
 
         return
 
+
+    def export_tests(filename, tests):
+        with open(filename, "wt") as csvfile:
+            fieldnames = ['section', 'subsection', 'title', 'reference']
+            cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep)
+            for test in tests:
+                data = test.split(".")
+                subsec = " ".join(data[1].split("_")).title()
+                rowdict = {
+                        "section": data[0].capitalize(),
+                        "subsection": subsec,
+                        "title": test,
+                        "reference": test
+                        }
+                cw.writerow(rowdict)
+
+    if options.export_tests:
+        cnt = 0
+        unq = []
+        for n,tc in ts.testcases.items():
+            for c in tc.cases:
+                unq.append(c)
+
+        tests = sorted(set(unq))
+        export_tests(options.export_tests, tests)
+        return
+
+
     if options.list_tests:
         cnt = 0
         unq = []