Re-land: Use script in depot_tools to retrieve builders from new cq.cfg

This was already landed twice before:
 - https://codereview.chromium.org/1152823005/
 - https://codereview.chromium.org/1148303006/

This CL is idential to the previous one, but a fix was landed to depot_tools:
 - https://codereview.chromium.org/1162993005/

[email protected], [email protected]
TEST=ran git-cl-try on this CL on Win, Mac and Linux and also tested with devs
     for which it failed last time

Review URL: https://codereview.chromium.org/1148813006

Cr-Commit-Position: refs/heads/master@{#332654}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 2e33261c..4d5c853 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -1788,29 +1788,30 @@
 
 
 def GetPreferredTryMasters(project, change):
-  import re
-  files = change.LocalPaths()
-
-  import os
   import json
-  with open(os.path.join(
-      change.RepositoryRoot(), 'testing', 'commit_queue', 'config.json')) as f:
-    cq_config = json.load(f)
-    cq_verifiers = cq_config.get('verifiers_no_patch', {})
-    cq_try_jobs = cq_verifiers.get('try_job_verifier', {})
-    builders = cq_try_jobs.get('launched', {})
+  import os.path
+  import platform
+  import subprocess
 
-    for master, master_config in cq_try_jobs.get('triggered', {}).iteritems():
-      for triggered_bot in master_config:
-        builders.get(master, {}).pop(triggered_bot, None)
+  cq_config_path = os.path.join(
+      change.RepositoryRoot(), 'infra', 'config', 'cq.cfg')
+  # commit_queue.py below is a script in depot_tools directory, which has a
+  # 'builders' command to retrieve a list of CQ builders from the CQ config.
+  is_win = platform.system() == 'Windows'
+  masters = json.loads(subprocess.check_output(
+      ['commit_queue', 'builders', cq_config_path], shell=is_win))
 
-    # Explicitly iterate over copies of dicts since we mutate them.
-    for master in builders.keys():
-      for builder in builders[master].keys():
-        # Do not trigger presubmit builders, since they're likely to fail
-        # (e.g. OWNERS checks before finished code review), and we're
-        # running local presubmit anyway.
-        if 'presubmit' in builder:
-          builders[master].pop(builder)
+  # Explicitly iterate over copies of keys since we mutate them.
+  for master in masters.keys():
+    for builder in masters[master].keys():
+      # Do not trigger presubmit builders, since they're likely to fail
+      # (e.g. OWNERS checks before finished code review), and we're
+      # running local presubmit anyway.
+      if 'presubmit' in builder:
+        masters[master].pop(builder)
+      else:
+        # Convert testfilter format to the one expected by git-cl-try.
+        testfilter = masters[master][builder].get('testfilter', 'defaulttests')
+        masters[master][builder] = [testfilter]
 
-  return builders
+  return masters