kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | # |
| 4 | # Use of this source code is governed by a BSD-style license |
| 5 | # that can be found in the LICENSE file in the root of the source |
| 6 | # tree. An additional intellectual property rights grant can be found |
| 7 | # in the file PATENTS. All contributing project authors may |
| 8 | # be found in the AUTHORS file in the root of the source tree. |
| 9 | |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 10 | """Script to download a Chromium checkout into the workspace. |
| 11 | |
| 12 | The script downloads a full Chromium Git clone and its DEPS. |
| 13 | |
| 14 | The following environment variable can be used to alter the behavior: |
| 15 | * CHROMIUM_NO_HISTORY - If set to 1, a Git checkout with no history will be |
| 16 | downloaded. This is consumes less bandwidth and disk space but is known to be |
| 17 | slower in general if you have a high-speed connection. |
| 18 | |
| 19 | After a successful sync has completed, a .last_sync_chromium file is written to |
| 20 | the chromium directory. While it exists, no more gclient sync operations will be |
| 21 | performed until the --target-revision changes or the SCRIPT_VERSION constant is |
| 22 | incremented. The file can be removed manually to force a new sync. |
| 23 | """ |
| 24 | |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 25 | import argparse |
| 26 | import os |
| 27 | import subprocess |
| 28 | import sys |
| 29 | |
iannucci@chromium.org | 1613638 | 2014-08-21 15:48:23 +0000 | [diff] [blame] | 30 | # Bump this whenever the algorithm changes and you need bots/devs to re-sync, |
| 31 | # ignoring the .last_sync_chromium file |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 32 | SCRIPT_VERSION = 3 |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 33 | |
| 34 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 35 | CHROMIUM_NO_HISTORY = 'CHROMIUM_NO_HISTORY' |
| 36 | |
| 37 | def _parse_gclient_dict(): |
| 38 | gclient_dict = {} |
| 39 | try: |
| 40 | main_gclient = os.path.join(os.path.dirname(ROOT_DIR), '.gclient') |
| 41 | with open(main_gclient, 'rb') as deps_content: |
| 42 | exec(deps_content, gclient_dict) |
| 43 | except Exception as e: |
| 44 | print >> sys.stderr, 'error while parsing .gclient:', e |
| 45 | return gclient_dict |
| 46 | |
| 47 | |
| 48 | def get_cache_dir(): |
| 49 | return _parse_gclient_dict().get('cache_dir') |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def get_target_os_list(): |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 53 | return ','.join(_parse_gclient_dict().get('target_os', [])) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def main(): |
| 57 | CR_DIR = os.path.join(ROOT_DIR, 'chromium') |
| 58 | |
| 59 | p = argparse.ArgumentParser() |
| 60 | p.add_argument('--target-revision', required=True, |
| 61 | help='The target chromium git revision [REQUIRED]') |
| 62 | p.add_argument('--chromium-dir', default=CR_DIR, |
| 63 | help=('The path to the chromium directory to sync ' |
| 64 | '(default: %(default)r)')) |
| 65 | opts = p.parse_args() |
| 66 | opts.chromium_dir = os.path.abspath(opts.chromium_dir) |
| 67 | |
iannucci@chromium.org | 1613638 | 2014-08-21 15:48:23 +0000 | [diff] [blame] | 68 | target_os_list = get_target_os_list() |
| 69 | |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 70 | # Do a quick check to see if we were successful last time to make runhooks |
| 71 | # sooper fast. |
| 72 | flag_file = os.path.join(opts.chromium_dir, '.last_sync_chromium') |
iannucci@chromium.org | 1613638 | 2014-08-21 15:48:23 +0000 | [diff] [blame] | 73 | flag_file_content = '\n'.join([ |
| 74 | str(SCRIPT_VERSION), |
| 75 | opts.target_revision, |
| 76 | repr(target_os_list), |
| 77 | ]) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 78 | if os.path.exists(flag_file): |
| 79 | with open(flag_file, 'r') as f: |
iannucci@chromium.org | 1613638 | 2014-08-21 15:48:23 +0000 | [diff] [blame] | 80 | if f.read() == flag_file_content: |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 81 | print 'Chromium already up to date: ', opts.target_revision |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 82 | return 0 |
| 83 | os.unlink(flag_file) |
| 84 | |
| 85 | env = os.environ.copy() |
| 86 | env['GYP_CHROMIUM_NO_ACTION'] = '1' |
| 87 | gclient_cmd = 'gclient.bat' if sys.platform.startswith('win') else 'gclient' |
| 88 | args = [ |
iannucci@chromium.org | 98d92d6 | 2014-08-20 23:53:59 +0000 | [diff] [blame] | 89 | gclient_cmd, 'sync', '--force', '--revision', 'src@'+opts.target_revision |
| 90 | ] |
| 91 | |
kjellander@webrtc.org | 3aa837c | 2014-08-20 14:25:43 +0000 | [diff] [blame] | 92 | if os.environ.get('CHROME_HEADLESS') == '1': |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 93 | # Running on a buildbot. |
kjellander@webrtc.org | 3aa837c | 2014-08-20 14:25:43 +0000 | [diff] [blame] | 94 | args.append('-vvv') |
| 95 | |
iannucci@chromium.org | 98d92d6 | 2014-08-20 23:53:59 +0000 | [diff] [blame] | 96 | if sys.platform.startswith('win'): |
iannucci@chromium.org | 286210d | 2014-08-21 02:14:11 +0000 | [diff] [blame] | 97 | cache_path = os.path.join(os.path.splitdrive(ROOT_DIR)[0] + os.path.sep, |
iannucci@chromium.org | 98d92d6 | 2014-08-20 23:53:59 +0000 | [diff] [blame] | 98 | 'b', 'git-cache') |
| 99 | else: |
| 100 | cache_path = '/b/git-cache' |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 101 | else: |
| 102 | # Support developers setting the cache_dir in .gclient. |
| 103 | cache_path = get_cache_dir() |
iannucci@chromium.org | 98d92d6 | 2014-08-20 23:53:59 +0000 | [diff] [blame] | 104 | |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 105 | # Allow for users with poor internet connections to download a Git clone |
| 106 | # without history (saves several gigs but is generally slower and doesn't work |
| 107 | # with the Git cache). |
| 108 | if os.environ.get(CHROMIUM_NO_HISTORY) == '1': |
| 109 | if cache_path: |
| 110 | print >> sys.stderr, ( |
| 111 | 'You cannot use "no-history" mode for syncing Chrome (i.e. set the ' |
| 112 | '%s environment variable to 1) when you have cache_dir configured in ' |
| 113 | 'your .gclient.' % CHROMIUM_NO_HISTORY) |
| 114 | return 1 |
| 115 | args.append('--no-history') |
| 116 | gclient_entries_file = os.path.join(opts.chromium_dir, '.gclient_entries') |
| 117 | else: |
| 118 | # Write a temporary .gclient file that has the cache_dir variable added. |
iannucci@chromium.org | 286210d | 2014-08-21 02:14:11 +0000 | [diff] [blame] | 119 | gclientfile = os.path.join(opts.chromium_dir, '.gclient') |
| 120 | with open(gclientfile, 'rb') as spec: |
iannucci@chromium.org | 98d92d6 | 2014-08-20 23:53:59 +0000 | [diff] [blame] | 121 | spec = spec.read().splitlines() |
| 122 | spec[-1] = 'cache_dir = %r' % (cache_path,) |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 123 | with open(gclientfile + '.tmp', 'wb') as f: |
iannucci@chromium.org | 286210d | 2014-08-21 02:14:11 +0000 | [diff] [blame] | 124 | f.write('\n'.join(spec)) |
| 125 | |
| 126 | args += [ |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 127 | '--gclientfile', '.gclient.tmp', |
iannucci@chromium.org | 286210d | 2014-08-21 02:14:11 +0000 | [diff] [blame] | 128 | '--delete_unversioned_trees', '--reset', '--upstream' |
| 129 | ] |
kjellander@webrtc.org | 6c6680a | 2014-09-27 18:41:03 +0000 | [diff] [blame] | 130 | gclient_entries_file = os.path.join(opts.chromium_dir, |
kjellander@webrtc.org | 8539bd0 | 2014-10-23 12:17:58 +0000 | [diff] [blame] | 131 | '.gclient.tmp_entries') |
kjellander@webrtc.org | 6c6680a | 2014-09-27 18:41:03 +0000 | [diff] [blame] | 132 | |
| 133 | # To avoid gclient sync problems when DEPS entries have been removed we must |
| 134 | # wipe the gclient's entries file that contains cached URLs for all DEPS. |
| 135 | if os.path.exists(gclient_entries_file): |
| 136 | os.unlink(gclient_entries_file) |
iannucci@chromium.org | 98d92d6 | 2014-08-20 23:53:59 +0000 | [diff] [blame] | 137 | |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 138 | if target_os_list: |
| 139 | args += ['--deps=' + target_os_list] |
| 140 | |
| 141 | print 'Running "%s" in %s' % (' '.join(args), opts.chromium_dir) |
| 142 | ret = subprocess.call(args, cwd=opts.chromium_dir, env=env) |
| 143 | if ret == 0: |
| 144 | with open(flag_file, 'wb') as f: |
iannucci@chromium.org | 1613638 | 2014-08-21 15:48:23 +0000 | [diff] [blame] | 145 | f.write(flag_file_content) |
kjellander@webrtc.org | 8925662 | 2014-08-20 12:10:11 +0000 | [diff] [blame] | 146 | |
| 147 | return ret |
| 148 | |
| 149 | |
| 150 | if __name__ == '__main__': |
| 151 | sys.exit(main()) |