blob: 073d2cb7f72cf30e1e878163e87474cfe35313bc [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +01001#!/usr/bin/env python
2# Copyright 2016 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"""Compress and upload Mac toolchain files."""
7
8import argparse
9import glob
10import os
11import plistlib
12import re
13import subprocess
14import sys
15import tarfile
16import tempfile
17
18
19TOOLCHAIN_URL = "gs://chrome-mac-sdk"
20
21# It's important to at least remove unused Platform folders to cut down on the
22# size of the toolchain folder. There are other various unused folders that
23# have been removed through trial and error. If future versions of Xcode become
24# problematic it's possible this list is incorrect, and can be reduced to just
25# the unused platforms. On the flip side, it's likely more directories can be
26# excluded.
27EXCLUDE_FOLDERS = [
28'Contents/Applications',
29'Contents/Developer/Documentation',
30'Contents/Developer/Platforms/AppleTVOS.platform',
31'Contents/Developer/Platforms/AppleTVSimulator.platform',
32'Contents/Developer/Platforms/WatchOS.platform',
33'Contents/Developer/Platforms/WatchSimulator.platform',
34'Contents/Developer/Platforms/iPhoneOS.platform',
35'Contents/Developer/Platforms/iPhoneSimulator.platform',
36'Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-migrator',
37'Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift',
38'Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/'
39 'usr/share/man',
40'Contents/Developer/Library/Xcode/Templates'
41]
42
43
44def main():
45 """Compress |target_dir| and upload to |TOOLCHAIN_URL|"""
46 parser = argparse.ArgumentParser()
47 parser.add_argument('target_dir',
48 help="Xcode installation directory.")
49 args = parser.parse_args()
50
51 # Verify this looks like an Xcode directory.
52 contents_dir = os.path.join(args.target_dir, 'Contents')
53 plist_file = os.path.join(contents_dir, 'version.plist')
54 try:
55 info = plistlib.readPlist(plist_file)
56 except:
57 print "Invalid Xcode dir."
58 return 0
59 build_version = info['ProductBuildVersion']
60
61 # Look for previous toolchain tgz files with the same |build_version|.
62 wildcard_filename = '%s/toolchain-%s-*.tgz' % (TOOLCHAIN_URL, build_version)
63 p = subprocess.Popen(['gsutil.py', 'ls', wildcard_filename],
64 stdout=subprocess.PIPE,
65 stderr=subprocess.PIPE)
66 output = p.communicate()[0]
67 next_count = 1
68 if p.returncode == 0:
69 next_count = len(output.split('\n'))
70 sys.stdout.write("%s already exists (%s). "
71 "Do you want to create another? [y/n] "
72 % (build_version, next_count - 1))
73
74 if raw_input().lower() not in set(['yes','y', 'ye']):
75 print "Skipping duplicate upload."
76 return 0
77
78 os.chdir(args.target_dir)
79 toolchain_file_name = "toolchain-%s-%s" % (build_version, next_count)
80 toolchain_name = tempfile.mktemp(suffix='toolchain.tgz')
81
82 print "Creating %s (%s)." % (toolchain_file_name, toolchain_name)
83 os.environ["COPYFILE_DISABLE"] = "1"
84 args = ['tar', '-cvzf', toolchain_name]
85 args.extend(map('--exclude={0}'.format, EXCLUDE_FOLDERS))
86 args.extend(['.'])
87 subprocess.check_call(args)
88
89 print "Uploading %s toolchain." % toolchain_file_name
90 destination_path = '%s/%s.tgz' % (TOOLCHAIN_URL, toolchain_file_name)
91 subprocess.check_call(['gsutil.py', 'cp', '-n', '-a', 'public-read',
92 toolchain_name, destination_path])
93
94 print "Done with %s upload." % toolchain_file_name
95 return 0
96
97if __name__ == '__main__':
98 sys.exit(main())