Modify upload script to print only valid results.

BUG=b:76226061
TEST=./upload_cts.py <build_id> <results_dir_path>

Change-Id: I99e135db29ae8f4c3b08593e0081edc445cc3a79
Reviewed-on: https://chromium-review.googlesource.com/978592
Commit-Ready: Rohit Makasana <rohitbm@chromium.org>
Tested-by: Rohit Makasana <rohitbm@chromium.org>
Reviewed-by: Rohit Makasana <rohitbm@chromium.org>
diff --git a/provingground/cts/lib/upload_utils.py b/provingground/cts/lib/upload_utils.py
index 06ac5d9..8ad824e 100755
--- a/provingground/cts/lib/upload_utils.py
+++ b/provingground/cts/lib/upload_utils.py
@@ -13,7 +13,6 @@
 import shutil
 import zipfile
 
-
 class UploadUtils(object):
   """Internal Helper for CTS and CTS Verifier Upload scripts."""
 
@@ -43,9 +42,12 @@
                        'CTS VERIFIER VERSION')
   def __init__(self):
     self.build_info_list = []
+    self.passed_list = []
+    self.failed_list = []
     self.obsolete_file_count = 0
     self.untested_file_count = 0
     self.build_mismatch_file_count = 0
+    self.found = 0
 
   def PrintObsoleteFolderCount(self):
     """Prints obsolete file count in case of build id mismatch."""
@@ -126,7 +128,19 @@
           ('{:>%s}'%self.SPACE_COUNT_CTSV_BUILD_VERSION).format(item[2]),
           ('{:>%s}'%self.SPACE_COUNT_VERIFIER_VERSION).format(item[3]))
 
-  def PrintFormattedDataCts(self, item):
+  def SegregateCtsDataToPrint(self, item):
+    """Segregates passed and failed results into two separate lists.
+
+    Args:
+       item: Board information.
+    """
+    if item[7] == "1":
+      self.passed_list.append(item)
+      self.PrintCtsResults(item)
+    else:
+      self.failed_list.append(item)
+
+  def PrintCtsResults(self, item):
     """Prints build information in a tabular form to the terminal.
 
     Sample table printed to terminal:
diff --git a/provingground/cts/upload_cts.py b/provingground/cts/upload_cts.py
index 42168c1..01aa3a3 100755
--- a/provingground/cts/upload_cts.py
+++ b/provingground/cts/upload_cts.py
@@ -42,11 +42,15 @@
     self.obsolete_files_exist = False
     self.input_build_id = input_build_id
     self.dir_path = dir_path
+    self.nextelem = 0
+    self.idx = 0
 
   def PrintBuildInfoList(self):
     """Sorts build information by build board and prints to the terminal."""
     self.build_info_list.sort(key=lambda x: x[1])
     for item in self.build_info_list:
+      self.idx = (self.idx + 1) % len(self.build_info_list)
+      self.nextelem = self.build_info_list[self.idx]
       if self.current_test_board_name != item[1]:
         title_msg = 'List of files for %s' % item[1]
         table_column_header = self.TABLE_HEADER_CTS
@@ -63,7 +67,35 @@
               self.C_END)
 
         self.current_test_board_name = item[1]
-      self.PrintFormattedDataCts(item)
+        if self.failed_list:
+          self.failed_list.clear()
+        if self.passed_list:
+          self.passed_list.clear()
+
+      self.SegregateCtsDataToPrint(item)
+      #Print failed result if there is only one item in the list.
+      if len(self.build_info_list) == 1:
+        if self.failed_list:
+          for i in range(len(self.failed_list)):
+            self.PrintCtsResults(self.failed_list[i])
+
+      #If there is a mix of passed & failed results, print only valid failures.
+      if self.failed_list:
+        if self.passed_list:
+        #If a test has passed at least once, the results are not printed.
+          for i in range(len(self.failed_list)):
+            for j in range(len(self.passed_list)):
+              if(self.failed_list[i][1] == self.passed_list[j][1] and
+                 self.failed_list[i][3] == self.passed_list[j][3] and
+                 self.failed_list[i][5] == self.passed_list[j][5]):
+                self.found = 1
+              else:
+                if self.found != 1:
+                  self.found = 0
+          #Check no more results to parse for board and print valid failure.
+          if self.found == 0 and self.nextelem[1] != item[1]:
+            for i in range(len(self.failed_list)):
+              self.PrintCtsResults(self.failed_list[i])
     print('\n')
 
   def PrintGsutilLinks(self, dir_path):