sync_chromium.py revision 374d39b7aed15aaa7bbf0a66cfd4f22ad6281fab
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
10import argparse
11import os
12import subprocess
13import sys
14
15# Bump this whenever the algorithm changes and you need bots/devs to re-sync,
16# ignoring the .last_sync_chromium file
17SCRIPT_VERSION = 1
18
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
45  target_os_list = get_target_os_list()
46
47  # 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')
50  flag_file_content = '\n'.join([
51    str(SCRIPT_VERSION),
52    opts.target_revision,
53    repr(target_os_list),
54  ])
55  if os.path.exists(flag_file):
56    with open(flag_file, 'r') as f:
57      if f.read() == flag_file_content:
58        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 = [
66      gclient_cmd, 'sync', '--force', '--revision', 'src@'+opts.target_revision
67  ]
68
69  if os.environ.get('CHROME_HEADLESS') == '1':
70    args.append('-vvv')
71
72    if sys.platform.startswith('win'):
73      cache_path = os.path.join(os.path.splitdrive(ROOT_DIR)[0] + os.path.sep,
74                                'b', 'git-cache')
75    else:
76      cache_path = '/b/git-cache'
77
78    gclientfile = os.path.join(opts.chromium_dir, '.gclient')
79    with open(gclientfile, 'rb') as spec:
80      spec = spec.read().splitlines()
81      spec[-1] = 'cache_dir = %r' % (cache_path,)
82    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    ]
89  else:
90    args.append('--no-history')
91
92  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:
99      f.write(flag_file_content)
100
101  return ret
102
103
104if __name__ == '__main__':
105  sys.exit(main())
106