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