Fix various issues in the roll_gn script.

This patch address a bunch of issues people have found in the roll_gn script:

- Fixes yet another bug where we weren't waiting for the try jobs
  to finish.
- Fixes a bug where we were adding an extra '\n' onto the DEPS file
  in the final roll CL.
- Closes the 'build_gn' CL once the build has completed.
- Adds better logging at the end of the 'wait' and 'roll_buildtools' steps.
- Removes mac_chromium_gn_rel from CQ_EXTRA_TRYBOTS, since
  it is part of the default set now.

R=brettw@chromium.org, andybons@chromium.org
BUG=

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

Cr-Original-Commit-Position: refs/heads/master@{#351141}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 513d94882c9720688a3e3f67da2b0a529e27b7b9
diff --git a/tools/gn/bin/roll_gn.py b/tools/gn/bin/roll_gn.py
index a479bdc..1e9dec9 100644
--- a/tools/gn/bin/roll_gn.py
+++ b/tools/gn/bin/roll_gn.py
@@ -182,13 +182,26 @@
     print('Checking build')
     results = self.CheckBuild()
     while (len(results) < 3 or
-           any(r['state'] == 'pending' for r in results.values())):
+           any(r['state'] in ('pending', 'started')
+               for r in results.values())):
       print()
       print('Sleeping for 30 seconds')
       time.sleep(30)
       print('Checking build')
       results = self.CheckBuild()
-    return 0 if all(r['state'] == 'success' for r in results.values()) else 1
+
+    ret = 0 if all(r['state'] == 'success' for r in results.values()) else 1
+    if ret:
+      print('Build failed.')
+    else:
+      print('Builds ready.')
+
+    # Close the build CL and move off of the build branch back to whatever
+    # we were on before.
+    self.Call('git-cl set-close')
+    self.MovetoLastHead()
+
+    return ret
 
   def CheckBuild(self):
     _, out, _ = self.Call('git-cl issue')
@@ -264,7 +277,8 @@
 
   def RollBuildtools(self):
     results = self.CheckBuild()
-    if not all(r['state'] == 'success' for r in results.values()):
+    if (len(results) < 3 or
+        not all(r['state'] == 'success' for r in results.values())):
       print("Roll isn't done or didn't succeed, exiting:")
       return 1
 
@@ -306,6 +320,11 @@
     # merged branch.
     self.Call('git checkout origin/master', cwd=self.buildtools_dir)
 
+    _, out, _ = self.Call('git rev-parse origin/master',
+                          cwd=self.buildtools_dir)
+    new_buildtools_commitish = out.strip()
+    print('Ready to roll buildtools to %s in DEPS' % new_buildtools_commitish)
+
     return 0
 
   def RollDEPS(self):
@@ -336,7 +355,7 @@
       return 1
 
     with open('DEPS', 'w') as fp:
-      fp.write(''.join(new_deps_lines) + '\n')
+      fp.write(''.join(new_deps_lines))
 
     desc = self.GetDEPSRollDesc(old_buildtools_commitish,
                                 new_buildtools_commitish)
@@ -349,11 +368,19 @@
     finally:
       os.remove(desc_file.name)
 
-    # Intentionally leave the src checkout on the new branch with the roll
-    # since we're not auto-committing it.
+    # Move off of the roll branch onto whatever we were on before.
+    # Do not explicitly close the roll CL issue, however; the CQ
+    # will close it when the roll lands, assuming it does so.
+    self.MoveToLastHead()
 
     return 0
 
+  def MovetoLastHead(self):
+    _, out, _ = self.Call('git reflog -1')
+    m = re.match('moving from ([^\s]+)', out)
+    last_head = m.group(1)
+    self.Call('git checkout %s' % last_head)
+
   def GetBuildtoolsDesc(self):
     gn_changes = self.GetGNChanges()
     return (
@@ -382,8 +409,7 @@
       '%s'
       '\n'
       'TBR=%s\n'
-      'CQ_EXTRA_TRYBOTS=tryserver.chromium.mac:mac_chromium_gn_rel,'
-      'mac_chromium_gn_dbg;'
+      'CQ_EXTRA_TRYBOTS=tryserver.chromium.mac:mac_chromium_gn_dbg;'
       'tryserver.chromium.win:win8_chromium_gn_dbg,'
       'win_chromium_gn_x64_rel\n' % (
         old_buildtools_commitish[:COMMITISH_DIGITS],