cbuildbot_launch: Wipe buildroot, if it has bad permissions.

We created some buildroots without group writability, which breaks
those builds. Wipe those buildroots.

BUG=chromium:710900
TEST=cbuildbot_launch_test

Change-Id: I9471506b1e6fc00e292cad7935eee9dd7779e7aa
Reviewed-on: https://chromium-review.googlesource.com/476114
Tested-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Paul Hobbs <phobbs@google.com>
Commit-Queue: Don Garrett <dgarrett@chromium.org>
Trybot-Ready: Don Garrett <dgarrett@chromium.org>
diff --git a/scripts/cbuildbot_launch.py b/scripts/cbuildbot_launch.py
index 2506e53..e60e1b4 100644
--- a/scripts/cbuildbot_launch.py
+++ b/scripts/cbuildbot_launch.py
@@ -16,6 +16,7 @@
 from __future__ import print_function
 
 import os
+import stat
 
 from chromite.cbuildbot import repository
 from chromite.cbuildbot.stages import sync_stages
@@ -71,6 +72,19 @@
   #   3) EmptyDir(buildroot, excludes=['.repo'])
   #   4) rm -rf buildroot
 
+  # TODO(dgarrett): Replace this block with fix for crbug.com/711048
+  try:
+    # Fix for bad checkouts from crbug.com/710900
+    st = os.stat(buildroot)
+    if not bool(st.st_mode & stat.S_IRGRP):
+      osutils.RmDir(buildroot, sudo=True)
+  except OSError:
+    # If the directory doesn't exist, the permissions will be okay.
+    pass
+
+  # Ensure buildroot exists.
+  osutils.SafeMakedirs(buildroot)
+
   state_file = os.path.join(buildroot, '.cbuildbot_launch_state')
   new_state = branchname or 'TOT'
 
@@ -177,9 +191,6 @@
   buildroot = options.buildroot
   git_cache_dir = options.git_cache_dir
 
-  # Ensure buildroot exists.
-  osutils.SafeMakedirs(buildroot)
-
   # Sometimes, we have to cleanup things that can break cbuildbot, especially
   # on the branch.
   CleanBuildroot(branchname, buildroot)