blob: db6fb98ef0672183143a650367cf95d663781d02 [file] [log] [blame]
kjellander@webrtc.org89256622014-08-20 12:10:11 +00001#!/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
10import argparse
11import os
12import subprocess
13import sys
14
iannucci@chromium.org16136382014-08-21 15:48:23 +000015# Bump this whenever the algorithm changes and you need bots/devs to re-sync,
16# ignoring the .last_sync_chromium file
kjellander@webrtc.orgdc926a02014-08-26 19:22:03 +000017SCRIPT_VERSION = 2
kjellander@webrtc.org89256622014-08-20 12:10:11 +000018
19ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
20
21
22def get_target_os_list():
23 try:
24 main_gclient = os.path.join(os.path.dirname(ROOT_DIR), '.gclient')
25 config_dict = {}
26 with open(main_gclient, 'rb') as deps_content:
27 exec(deps_content, config_dict)
28 return ','.join(config_dict.get('target_os', []))
29 except Exception as e:
30 print >> sys.stderr, "error while parsing .gclient:", e
31
32
33def main():
34 CR_DIR = os.path.join(ROOT_DIR, 'chromium')
35
36 p = argparse.ArgumentParser()
37 p.add_argument('--target-revision', required=True,
38 help='The target chromium git revision [REQUIRED]')
39 p.add_argument('--chromium-dir', default=CR_DIR,
40 help=('The path to the chromium directory to sync '
41 '(default: %(default)r)'))
42 opts = p.parse_args()
43 opts.chromium_dir = os.path.abspath(opts.chromium_dir)
44
iannucci@chromium.org16136382014-08-21 15:48:23 +000045 target_os_list = get_target_os_list()
46
kjellander@webrtc.org89256622014-08-20 12:10:11 +000047 # Do a quick check to see if we were successful last time to make runhooks
48 # sooper fast.
49 flag_file = os.path.join(opts.chromium_dir, '.last_sync_chromium')
iannucci@chromium.org16136382014-08-21 15:48:23 +000050 flag_file_content = '\n'.join([
51 str(SCRIPT_VERSION),
52 opts.target_revision,
53 repr(target_os_list),
54 ])
kjellander@webrtc.org89256622014-08-20 12:10:11 +000055 if os.path.exists(flag_file):
56 with open(flag_file, 'r') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +000057 if f.read() == flag_file_content:
kjellander@webrtc.org89256622014-08-20 12:10:11 +000058 print "Chromium already up to date:", opts.target_revision
59 return 0
60 os.unlink(flag_file)
61
kjellander@webrtc.orge94f83a2014-09-18 13:47:23 +000062 # To avoid gclient sync problems when DEPS entries have been removed we must
63 # wipe the .gclient_entries file that contains cached URLs for all DEPS.
64 entries_file = os.path.join(opts.chromium_dir, '.gclient_entries')
65 if os.path.exists(entries_file):
66 os.unlink(entries_file)
67
kjellander@webrtc.org89256622014-08-20 12:10:11 +000068 env = os.environ.copy()
69 env['GYP_CHROMIUM_NO_ACTION'] = '1'
70 gclient_cmd = 'gclient.bat' if sys.platform.startswith('win') else 'gclient'
71 args = [
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000072 gclient_cmd, 'sync', '--force', '--revision', 'src@'+opts.target_revision
73 ]
74
kjellander@webrtc.org3aa837c2014-08-20 14:25:43 +000075 if os.environ.get('CHROME_HEADLESS') == '1':
76 args.append('-vvv')
77
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000078 if sys.platform.startswith('win'):
iannucci@chromium.org286210d2014-08-21 02:14:11 +000079 cache_path = os.path.join(os.path.splitdrive(ROOT_DIR)[0] + os.path.sep,
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000080 'b', 'git-cache')
81 else:
82 cache_path = '/b/git-cache'
83
iannucci@chromium.org286210d2014-08-21 02:14:11 +000084 gclientfile = os.path.join(opts.chromium_dir, '.gclient')
85 with open(gclientfile, 'rb') as spec:
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000086 spec = spec.read().splitlines()
87 spec[-1] = 'cache_dir = %r' % (cache_path,)
iannucci@chromium.org286210d2014-08-21 02:14:11 +000088 with open(gclientfile + '.bot', 'wb') as f:
89 f.write('\n'.join(spec))
90
91 args += [
92 '--gclientfile', '.gclient.bot',
93 '--delete_unversioned_trees', '--reset', '--upstream'
94 ]
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000095 else:
96 args.append('--no-history')
97
kjellander@webrtc.org89256622014-08-20 12:10:11 +000098 if target_os_list:
99 args += ['--deps=' + target_os_list]
100
101 print 'Running "%s" in %s' % (' '.join(args), opts.chromium_dir)
102 ret = subprocess.call(args, cwd=opts.chromium_dir, env=env)
103 if ret == 0:
104 with open(flag_file, 'wb') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +0000105 f.write(flag_file_content)
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000106
107 return ret
108
109
110if __name__ == '__main__':
111 sys.exit(main())