blob: a9028f2144f96819c5f3c12a257ea283dc498f9b [file] [log] [blame]
David Jamesef74b1c2012-11-12 07:47:47 -08001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Sync the Chrome source code used by Chrome OS to the specified directory."""
6
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Ryan Cui21aa0be2013-05-02 10:34:21 -07009import functools
Simran Basi7d7dbd82015-01-14 14:01:55 -080010import os
Ryan Cui21aa0be2013-05-02 10:34:21 -070011
Simran Basi7d7dbd82015-01-14 14:01:55 -080012from chromite.cbuildbot import constants
David Jamesef74b1c2012-11-12 07:47:47 -080013from chromite.lib import commandline
14from chromite.lib import cros_build_lib
15from chromite.lib import gclient
16from chromite.lib import osutils
17
18
19def GetParser():
20 """Creates the argparse parser."""
21 parser = commandline.ArgumentParser(description=__doc__)
22
23 version = parser.add_mutually_exclusive_group()
24 version.add_argument('--tag', help='Sync to specified Chrome release',
25 dest='version')
Stefan Zagerd49d9ff2014-08-15 21:33:37 -070026 version.add_argument('--revision', help='Sync to specified git revision',
27 dest='version')
David Jamesef74b1c2012-11-12 07:47:47 -080028
29 parser.add_argument('--internal', help='Sync internal version of Chrome',
30 action='store_true', default=False)
David Jamesef74b1c2012-11-12 07:47:47 -080031 parser.add_argument('--reset', help='Revert local changes',
32 action='store_true', default=False)
33 parser.add_argument('--gclient', help=commandline.argparse.SUPPRESS,
Simran Basi7d7dbd82015-01-14 14:01:55 -080034 default=None)
Ben Chan2e19e4e2015-01-06 20:17:15 -080035 parser.add_argument('--gclient_template', help='Template gclient input file')
Ben Chan11aad4d2015-02-20 12:47:23 -080036 parser.add_argument('--skip_cache', help='Skip using git cache',
37 dest='use_cache', action='store_false')
David Jamesef74b1c2012-11-12 07:47:47 -080038 parser.add_argument('chrome_root', help='Directory to sync chrome in')
39
40 return parser
41
42
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040043def main(argv):
David Jamesef74b1c2012-11-12 07:47:47 -080044 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040045 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080046
Simran Basi7d7dbd82015-01-14 14:01:55 -080047 if options.gclient is '':
48 parser.error('--gclient can not be an empty string!')
49 gclient_path = options.gclient or osutils.Which('gclient')
50 if not gclient_path:
51 gclient_path = os.path.join(constants.DEPOT_TOOLS_DIR, 'gclient')
52
David Jamesef74b1c2012-11-12 07:47:47 -080053 # Revert any lingering local changes.
54 if not osutils.SafeMakedirs(options.chrome_root) and options.reset:
55 try:
Simran Basi7d7dbd82015-01-14 14:01:55 -080056 gclient.Revert(gclient_path, options.chrome_root)
David Jamesef74b1c2012-11-12 07:47:47 -080057 except cros_build_lib.RunCommandError:
58 osutils.RmDir(options.chrome_root)
59 osutils.SafeMakedirs(options.chrome_root)
60
61 # Sync new Chrome.
Simran Basi7d7dbd82015-01-14 14:01:55 -080062 gclient.WriteConfigFile(gclient_path, options.chrome_root,
Ben Chan2e19e4e2015-01-06 20:17:15 -080063 options.internal, options.version,
Ben Chan11aad4d2015-02-20 12:47:23 -080064 options.gclient_template, options.use_cache)
Ryan Cui21aa0be2013-05-02 10:34:21 -070065 sync_fn = functools.partial(
Simran Basi7d7dbd82015-01-14 14:01:55 -080066 gclient.Sync, gclient_path, options.chrome_root, reset=options.reset)
Ryan Cui21aa0be2013-05-02 10:34:21 -070067
68 # Sync twice when run with --reset, which implies 'gclient sync -D'.
69 #
70 # There's a bug with 'gclient sync -D' that gets hit when the location of a
71 # dependency checkout (in the DEPS file) is moved to a path that contains
72 # (in a directory fashion) its old path. E.g., when Blink is moved from
73 # Webkit/Source/ to Webkit/. When this happens, a 'gclient sync -D' will
74 # blow away Webkit/Source/ after the sync, since it is no longer in the
75 # DEPS file, leaving the Blink checkout missing a Source/ subdirectory.
76 #
77 # This bug also gets hit the other way around - E.g., if Blink moves from
78 # Webkit/ to Webkit/Source/.
79 #
80 # To work around this, we sync twice, so that any directories deleted by
81 # the first sync will be restored in the second.
82 #
83 # TODO(rcui): Remove this workaround when the bug is fixed in gclient, or
84 # replace with a more sophisticated solution that syncs twice only when any
85 # paths in the DEPS file cannot be found after initial sync.
86 if options.reset:
87 sync_fn()
88 sync_fn()
David Jamesef74b1c2012-11-12 07:47:47 -080089 return 0