OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Paweł Hajdan Jr.
2016/03/04 07:57:09
nit: 2016, no (c)
justincohen
2016/03/04 16:46:44
Done.
| |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Download necessary mac toolchain files under certain conditions. If | |
7 xcode-select is already set and points to an external folder | |
8 (e.g. /Application/Xcode.app), this script only runs if the GYP_DEFINE | |
9 |force_mac_toolchain| is set. To override the values in | |
10 |TOOLCHAIN_REVISION|-|TOOLCHAIN_SUB_REVISION| below, GYP_DEFINE | |
11 mac_toolchain_revision can be used instead. | |
12 | |
13 This script will only run on machines with the following programs added to a | |
14 sudoers list: | |
15 /usr/bin/xcode-select | |
16 /usr/bin/xcodebuild | |
17 | |
18 Otherwise user input would be required to complete the script. Perhaps future | |
19 versions can be modified to allow for user input on developer machines. | |
20 """ | |
21 | |
22 import os | |
23 import shutil | |
24 import subprocess | |
25 import sys | |
26 import tarfile | |
27 import time | |
28 import tempfile | |
29 import urllib2 | |
30 | |
31 # Update sys.path to import gyp. | |
32 script_dir = os.path.dirname(os.path.realpath(__file__)) | |
33 chrome_src = os.path.abspath(os.path.join(script_dir, os.pardir)) | |
34 sys.path.insert(0, os.path.join(chrome_src, 'tools', 'gyp', 'pylib')) | |
35 import gyp | |
36 | |
37 # This can be changed after running /build/package_mac_toolchain.py. | |
38 TOOLCHAIN_REVISION = '7C68' | |
39 TOOLCHAIN_SUB_REVISION = 1 | |
40 TOOLCHAIN_VERSION = "%s-%s" % (TOOLCHAIN_REVISION, TOOLCHAIN_SUB_REVISION) | |
41 | |
42 BASE_DIR = os.path.abspath(os.path.dirname(__file__)) | |
43 TOOLCHAIN_BUILD_DIR = os.path.join(BASE_DIR, 'mac_files', 'Xcode.app') | |
44 STAMP_FILE = os.path.join(BASE_DIR, 'mac_files', 'toolchain_build_revision') | |
45 TOOLCHAIN_URL = 'gs://chrome-mac-sdk/' | |
46 | |
47 def ReadStampFile(): | |
48 """Return the contents of the stamp file, or '' if it doesn't exist.""" | |
49 try: | |
50 with open(STAMP_FILE, 'r') as f: | |
51 return f.read().rstrip() | |
52 except IOError: | |
53 return '' | |
54 | |
55 | |
56 def WriteStampFile(s): | |
57 """Write s to the stamp file.""" | |
58 EnsureDirExists(os.path.dirname(STAMP_FILE)) | |
59 with open(STAMP_FILE, 'w') as f: | |
60 f.write(s) | |
61 f.write('\n') | |
62 | |
63 | |
64 def EnsureDirExists(path): | |
65 if not os.path.exists(path): | |
66 os.makedirs(path) | |
67 | |
68 | |
69 def DownloadAndUnpack(url, output_dir): | |
70 """Decompresses |url| into a cleared |output_dir|.""" | |
71 temp_name = tempfile.mktemp(prefix='mac_toolchain') | |
72 print 'Downloading new toolchain.' | |
73 subprocess.check_call(['gsutil.py', 'cp', url, temp_name]) | |
74 if os.path.exists(output_dir): | |
75 print 'Deleting old toolchain.' | |
76 shutil.rmtree(output_dir) | |
77 EnsureDirExists(output_dir) | |
78 print 'Unpacking new toolchain.' | |
79 tarfile.open(mode='r:gz', name=temp_name).extractall(path=output_dir) | |
80 os.unlink(temp_name) | |
Paweł Hajdan Jr.
2016/03/04 07:57:10
Shouldn't this be in try-finally?
justincohen
2016/03/04 16:46:44
Good catch, Done.
| |
81 | |
82 | |
83 def CanAccessToolchainBucket(): | |
84 """Checks whether the user has access to |TOOLCHAIN_URL|.""" | |
85 proc = subprocess.Popen(['gsutil.py', 'ls', TOOLCHAIN_URL], | |
86 stdout=subprocess.PIPE) | |
87 proc.communicate() | |
88 return proc.returncode == 0 | |
89 | |
90 | |
91 def SwitchToolchain(directory): | |
92 """Use xcode-select to select new toolchain and accept license. This only | |
93 works if both xcode-select and xcodebuild are in sudoers.""" | |
94 print 'Setting xcode-select to %s.' % directory | |
95 subprocess.check_call(['sudo', '/usr/bin/xcode-select', '-s', directory]) | |
96 subprocess.check_call(['sudo', '/usr/bin/xcodebuild', '-license', 'accept']) | |
97 | |
98 | |
99 def main(): | |
100 gyp_defines = gyp.NameValueListToDict(gyp.ShlexEnv('GYP_DEFINES')) | |
101 force_pull = 'force_mac_toolchain' in gyp_defines | |
102 toolchain_revision = gyp_defines.get('mac_toolchain_revision', | |
103 TOOLCHAIN_VERSION) | |
104 | |
105 # Don't update the toolchain if there's already one installed outside of the | |
106 # expected location for a Chromium mac toolchain, unless |force_pull| is set. | |
107 proc = subprocess.Popen(['xcode-select', '-p'], stdout=subprocess.PIPE) | |
108 xcode_select_dir = proc.communicate()[0] | |
109 rc = proc.returncode | |
110 if not force_pull and rc == 0 and TOOLCHAIN_BUILD_DIR not in xcode_select_dir: | |
111 print 'Using local toolchain.' | |
112 return 0 | |
113 | |
114 if ReadStampFile() == toolchain_revision: | |
115 print 'Toolchain (%s) is already up to date.' % toolchain_revision | |
116 SwitchToolchain(TOOLCHAIN_BUILD_DIR) | |
117 return 0 | |
118 | |
119 # This script cannot complete if xcode-select and xcodebuild are not in | |
120 # sudoers. | |
121 proc = subprocess.Popen(['sudo', '-l'], stdout=subprocess.PIPE) | |
122 output = proc.communicate()[0] | |
123 cmds = ['xcode-select', 'xcodebuild'] | |
124 if not all(x in output for x in cmds): | |
125 print 'xcode-select and xcodebuild must be present in sudoers.' | |
126 | |
127 if not CanAccessToolchainBucket(): | |
128 print 'Cannot access toolchain bucket.' | |
129 return 0 | |
130 | |
131 # Reset the stamp file in case the build is unsuccessful. | |
132 WriteStampFile('') | |
133 | |
134 toolchain_file = "%s.tgz" % toolchain_revision | |
135 toolchain_full_url = TOOLCHAIN_URL + toolchain_file | |
136 | |
137 print 'Updating toolchain to %s...' % toolchain_revision | |
138 try: | |
139 toolchain_file = "toolchain-%s.tgz" % toolchain_revision | |
140 toolchain_full_url = TOOLCHAIN_URL + toolchain_file | |
141 DownloadAndUnpack(toolchain_full_url, TOOLCHAIN_BUILD_DIR) | |
142 SwitchToolchain(TOOLCHAIN_BUILD_DIR) | |
143 | |
144 print 'Toolchain %s unpacked.' % toolchain_revision | |
145 WriteStampFile(toolchain_revision) | |
146 | |
147 return 0 | |
148 except: | |
149 print 'Failed to download toolchain %s.' % toolchain_file | |
150 print 'Exiting.' | |
Paweł Hajdan Jr.
2016/03/04 07:57:09
Whoa, that seems to totally hide what was the exce
| |
151 return 1 | |
152 | |
153 if __name__ == '__main__': | |
154 sys.exit(main()) | |
OLD | NEW |