blob: d02346c97a457ffe26eb23e4120bb1bcad9f9cce [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
62 env = os.environ.copy()
63 env['GYP_CHROMIUM_NO_ACTION'] = '1'
64 gclient_cmd = 'gclient.bat' if sys.platform.startswith('win') else 'gclient'
65 args = [
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000066 gclient_cmd, 'sync', '--force', '--revision', 'src@'+opts.target_revision
67 ]
68
kjellander@webrtc.org3aa837c2014-08-20 14:25:43 +000069 if os.environ.get('CHROME_HEADLESS') == '1':
70 args.append('-vvv')
71
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000072 if sys.platform.startswith('win'):
iannucci@chromium.org286210d2014-08-21 02:14:11 +000073 cache_path = os.path.join(os.path.splitdrive(ROOT_DIR)[0] + os.path.sep,
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000074 'b', 'git-cache')
75 else:
76 cache_path = '/b/git-cache'
77
iannucci@chromium.org286210d2014-08-21 02:14:11 +000078 gclientfile = os.path.join(opts.chromium_dir, '.gclient')
79 with open(gclientfile, 'rb') as spec:
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000080 spec = spec.read().splitlines()
81 spec[-1] = 'cache_dir = %r' % (cache_path,)
iannucci@chromium.org286210d2014-08-21 02:14:11 +000082 with open(gclientfile + '.bot', 'wb') as f:
83 f.write('\n'.join(spec))
84
85 args += [
86 '--gclientfile', '.gclient.bot',
87 '--delete_unversioned_trees', '--reset', '--upstream'
88 ]
iannucci@chromium.org98d92d62014-08-20 23:53:59 +000089 else:
90 args.append('--no-history')
91
kjellander@webrtc.org89256622014-08-20 12:10:11 +000092 if target_os_list:
93 args += ['--deps=' + target_os_list]
94
95 print 'Running "%s" in %s' % (' '.join(args), opts.chromium_dir)
96 ret = subprocess.call(args, cwd=opts.chromium_dir, env=env)
97 if ret == 0:
98 with open(flag_file, 'wb') as f:
iannucci@chromium.org16136382014-08-21 15:48:23 +000099 f.write(flag_file_content)
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000100
101 return ret
102
103
104if __name__ == '__main__':
105 sys.exit(main())