Install Xcode via swarming.

This script runs swarming_xcode_install on the bots.  It should be run when 
we need to upgrade all the swarming testers.  It:
  1) Packages two python files into an isolate.
  2) Runs the isolate on swarming machines that satisfy certain dimensions.

This version will remove Xcode9-b4 and replace it with Xcode 9.

Bug: 
Change-Id: Ia86f4d963b43af175445da428be8920899e7e05e
Reviewed-on: https://chromium-review.googlesource.com/641379
Reviewed-by: Dirk Pranke <[email protected]>
Reviewed-by: Erik Chen <[email protected]>
Reviewed-by: Sergey Berezin <[email protected]>
Commit-Queue: Justin Cohen <[email protected]>
Cr-Commit-Position: refs/heads/master@{#502238}
diff --git a/build/swarming_xcode_install.py b/build/swarming_xcode_install.py
new file mode 100755
index 0000000..7764aa55
--- /dev/null
+++ b/build/swarming_xcode_install.py
@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+# Copyright 2017 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Script used to install Xcode on the swarming bots.
+"""
+
+import os
+import shutil
+import subprocess
+import sys
+import tarfile
+import tempfile
+
+import mac_toolchain
+
+VERSION = '9A235'
+URL = 'gs://chrome-mac-sdk/ios-toolchain-9A235-1.tgz'
+REMOVE_DIR = '/Applications/Xcode9.0-Beta4.app/'
+OUTPUT_DIR = '/Applications/Xcode9.0.app/'
+
+def main():
+  # Check if it's already installed.
+  if os.path.exists(OUTPUT_DIR):
+    env = os.environ.copy()
+    env['DEVELOPER_DIR'] = OUTPUT_DIR
+    cmd = ['xcodebuild', '-version']
+    found_version = \
+        subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE).communicate()[0]
+    if VERSION in found_version:
+      print "Xcode %s already installed" % VERSION
+      sys.exit(0)
+
+  # Confirm old dir is there first.
+  if not os.path.exists(REMOVE_DIR):
+    print "Failing early since %s isn't there." % REMOVE_DIR
+    sys.exit(1)
+
+  # Download Xcode.
+  with tempfile.NamedTemporaryFile() as temp:
+    env = os.environ.copy()
+    env['PATH'] += ":/b/depot_tools"
+    subprocess.check_call(['gsutil.py', 'cp', URL, temp.name], env=env)
+    if os.path.exists(OUTPUT_DIR):
+      shutil.rmtree(OUTPUT_DIR)
+    if not os.path.exists(OUTPUT_DIR):
+      os.makedirs(OUTPUT_DIR)
+    tarfile.open(mode='r:gz', name=temp.name).extractall(path=OUTPUT_DIR)
+
+  # Accept license, call runFirstLaunch.
+  mac_toolchain.FinalizeUnpack(OUTPUT_DIR, 'ios')
+
+  # Set new Xcode as default.
+  subprocess.check_call(['sudo', '/usr/bin/xcode-select', '-s', OUTPUT_DIR])
+
+  if os.path.exists(REMOVE_DIR):
+    shutil.rmtree(REMOVE_DIR)
+
+
+if __name__ == '__main__':
+  sys.exit(main())
+