sync_chromium.py 5.4 KB

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