Added HTML output to commit-tracker

Change-Id: Ib62deb9204a79080fb45ba23ea43170a8590ade4
Signed-off-by: Charles Hofer <cphofer@us.ibm.com>
diff --git a/tools/commit-tracker b/tools/commit-tracker
index b393984..edeea34 100755
--- a/tools/commit-tracker
+++ b/tools/commit-tracker
@@ -64,6 +64,23 @@
             l_cl_string += '\n' + l_report.to_cl_string(i_level + 1)
         return l_cl_string
 
+    def to_html(self, i_level=0):
+        l_repo_url = re.sub('git://', 'http://', self.repo_uri)
+        # Get HTML for this commit
+        l_html = \
+            '<div style="margin-left: ' + str(i_level * 20) + 'px">' \
+            + '<a href="' + l_repo_url + '" target="_blank" ' \
+            + 'style="color: red">' + self.repo_name + '</a>&nbsp;' \
+            + '<a href="' + l_repo_url + '/commit/' + self.sha \
+            + '" target="_blank" style="color: blue">' + self.nice_name \
+            + '</a>&nbsp;' \
+            + '<span>' + re.sub('\s+', ' ', self.summary) + '</span>' \
+            + '</div>\n'
+        # Get the HTML for all subcommits
+        for l_commit in self.subreports:
+            l_html += l_commit.to_html(i_level + 1)
+        return l_html
+
     def get_total_insertions(self):
         l_insertions = self.insertions
         for l_commit in self.subreports:
@@ -89,15 +106,18 @@
 ###############################################################################
 def main(i_args):
     # Parse the arguments
-    l_args_obj = parse_arguments(i_args)
+    l_args = parse_arguments(i_args)
 
-    # Print every commit
-    print 'Getting report for ' + l_args_obj.repo_dir
+    # Set the logger level
+    logging.basicConfig(level=logging.ERROR)
+
+    # Generate the commit reports
+    print 'Getting report for ' + l_args.repo_dir
     l_reports = generate_commit_reports(
-        l_args_obj.repo_uri,
-        l_args_obj.repo_dir,
-        l_args_obj.latest_commit,
-        l_args_obj.earliest_commit)
+        l_args.repo_uri,
+        l_args.repo_dir,
+        l_args.latest_commit,
+        l_args.earliest_commit)
 
     # Compile issues, insertions, and deletions
     l_issues = []
@@ -109,16 +129,35 @@
         l_issues.extend(l_report.get_all_closed_issues())
 
     # Print commit information to the console
-    print 'Commits'
+    print 'Commits...'
     for l_report in l_reports:
         print l_report.to_cl_string()
     print 'Closed issues...'
     for l_issue in l_issues:
         print '  ' + str(l_issue[0]) + ' ' + str(l_issue[1])
-    print 'Insertions and Deletions'
+    print 'Insertions and deletions...'
     print str(l_total_insertions) + ' insertions'
     print str(l_total_deletions) + ' deletions'
 
+    # Write to the HTML file if the user set the flag
+    if l_args.html_file:
+        print 'Writing to HTML file...'
+        l_html_file = open(l_args.html_file, 'w+')
+        l_html_file.write('<html><body>\n')
+        for l_report in l_reports:
+            l_html_file.write(l_report.to_html())
+        l_html_file.write('<p>' + str(l_total_insertions) \
+                          + ' insertions and ' + str(l_total_deletions) \
+                          + ' deletions</p>')
+        l_html_file.write('<div>Closed Issues</div>')
+        for l_issue in l_issues:
+            l_html_file.write('<div><a href="http://www.github.com/' \
+                              + re.sub('#', '/issues/', l_issue[0]) \
+                              + '" target="_blank">' + l_issue[0] + '</a> ' \
+                              + l_issue[1] + '</div>')
+        l_html_file.write('</body></html>')
+        l_html_file.close()
+
 ###############################################################################
 # @brief Parses the arguments from the command line
 #
@@ -146,6 +185,11 @@
     l_parser.add_argument(
         'earliest_commit',
         help='A reference to the earliest commit to get information for')
+    l_parser.add_argument(
+        '--html_file',
+        default=None,
+        help='If set to a file path, this script will write an HTML ' \
+             +'version of the console output to the file path given')
     return l_parser.parse_args(i_args)
 
 ###############################################################################