1#!/usr/bin/env python
2# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""
7Script used to install Xcode on the swarming bots.
8"""
9
10import os
11import shutil
12import subprocess
13import sys
14import tarfile
15import tempfile
16
17import mac_toolchain
18
19VERSION = '9A235'
20URL = 'gs://chrome-mac-sdk/ios-toolchain-9A235-1.tgz'
21REMOVE_DIR = '/Applications/Xcode9.0-Beta4.app/'
22OUTPUT_DIR = '/Applications/Xcode9.0.app/'
23
24def main():
25  # Check if it's already installed.
26  if os.path.exists(OUTPUT_DIR):
27    env = os.environ.copy()
28    env['DEVELOPER_DIR'] = OUTPUT_DIR
29    cmd = ['xcodebuild', '-version']
30    found_version = \
31        subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE).communicate()[0]
32    if VERSION in found_version:
33      print "Xcode %s already installed" % VERSION
34      sys.exit(0)
35
36  # Confirm old dir is there first.
37  if not os.path.exists(REMOVE_DIR):
38    print "Failing early since %s isn't there." % REMOVE_DIR
39    sys.exit(1)
40
41  # Download Xcode.
42  with tempfile.NamedTemporaryFile() as temp:
43    env = os.environ.copy()
44    env['PATH'] += ":/b/depot_tools"
45    subprocess.check_call(['gsutil.py', 'cp', URL, temp.name], env=env)
46    if os.path.exists(OUTPUT_DIR):
47      shutil.rmtree(OUTPUT_DIR)
48    if not os.path.exists(OUTPUT_DIR):
49      os.makedirs(OUTPUT_DIR)
50    tarfile.open(mode='r:gz', name=temp.name).extractall(path=OUTPUT_DIR)
51
52  # Accept license, call runFirstLaunch.
53  mac_toolchain.FinalizeUnpack(OUTPUT_DIR, 'ios')
54
55  # Set new Xcode as default.
56  subprocess.check_call(['sudo', '/usr/bin/xcode-select', '-s', OUTPUT_DIR])
57
58  if os.path.exists(REMOVE_DIR):
59    shutil.rmtree(REMOVE_DIR)
60
61
62if __name__ == '__main__':
63  sys.exit(main())
64
65