[Fixed from older branch] Timing/RDB for lower-level checks

presubmit: Report timing to rdb for individual checks
Rather than using CheckChangeOnCommit() or CheckChangeOnUpload() as the
sole entry points for PRESUBMIT.py files, this cl enables presubmit_support.py
to use any CheckXYZ[OnCommit/Upload] function to serve as an entry point.

This way, we can perform timing and RDB reporting for each of the
presubmit check functions and have check-specific data.

Bug: 1106943
Change-Id: Ifdabd68c0904a6d70a828f12de157369c6c1571d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2332321
Commit-Queue: Saagar Sanghavi <saagarsanghavi@google.com>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
diff --git a/presubmit_support.py b/presubmit_support.py
index cd2eb7b..6c41f20 100755
--- a/presubmit_support.py
+++ b/presubmit_support.py
@@ -1529,6 +1529,7 @@
     Return:
       A list of result objects, empty if no problems.
     """
+
     # Change to the presubmit file's directory to support local imports.
     main_path = os.getcwd()
     presubmit_dir = os.path.dirname(presubmit_path)
@@ -1547,41 +1548,41 @@
     except Exception as e:
       raise PresubmitFailure('"%s" had an exception.\n%s' % (presubmit_path, e))
 
-    # These function names must change if we make substantial changes to
-    # the presubmit API that are not backwards compatible.
-    if self.committing:
-      function_name = 'CheckChangeOnCommit'
-    else:
-      function_name = 'CheckChangeOnUpload'
-    if function_name in context:
-      try:
-        context['__args'] = (input_api, output_api)
+    context['__args'] = (input_api, output_api)
+
+    # Get path of presubmit directory relative to repository root
+    # Always use forward slashes, so that path is same in *nix and Windows
+    root = input_api.change.RepositoryRoot()
+    rel_path = os.path.relpath(presubmit_dir, root)
+    rel_path = rel_path.replace(os.path.sep, '/')
+
+    # Perform all the desired presubmit checks.
+    results = []
+    try:
+      for function_name in context:
+        if not function_name.startswith('Check'):
+          continue
+        if function_name.endswith('Commit') and not self.committing:
+          continue
+        if function_name.endswith('Upload') and self.committing:
+          continue
         logging.debug('Running %s in %s', function_name, presubmit_path)
-
-        # TODO (crbug.com/1106943): Dive into each of the individual checks
-
-        # Get path of presubmit directory relative to repository root.
-        # Always use forward slashes, so that path is same in *nix and Windows
-        root = input_api.change.RepositoryRoot()
-        rel_path = os.path.relpath(presubmit_dir, root)
-        rel_path = rel_path.replace(os.path.sep, '/')
-
         with rdb_wrapper.setup_rdb(function_name, rel_path) as my_status:
           result = eval(function_name + '(*__args)', context)
           self._check_result_type(result)
           if any(res.fatal for res in result):
             my_status.status = rdb_wrapper.STATUS_FAIL
+          results.extend(result)
         logging.debug('Running %s done.', function_name)
         self.more_cc.extend(output_api.more_cc)
-      finally:
-        for f in input_api._named_temporary_files:
-          os.remove(f)
-    else:
-      result = ()  # no error since the script doesn't care about current event.
+
+    finally:
+      for f in input_api._named_temporary_files:
+        os.remove(f)
 
     # Return the process to the original working directory.
     os.chdir(main_path)
-    return result
+    return results
 
   def _check_result_type(self, result):
     """Helper function which ensures result is a list, and all elements are