blob: 1cc53d7595371cff7367c6e3a6abe03353308156 [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
10
David Jamesef74b1c2012-11-12 07:47:47 -080011from chromite.lib import commandline
12from chromite.lib import cros_build_lib
13from chromite.lib import gclient
14from chromite.lib import osutils
15
16
17def GetParser():
18 """Creates the argparse parser."""
19 parser = commandline.ArgumentParser(description=__doc__)
20
21 version = parser.add_mutually_exclusive_group()
22 version.add_argument('--tag', help='Sync to specified Chrome release',
23 dest='version')
Stefan Zagerd49d9ff2014-08-15 21:33:37 -070024 version.add_argument('--revision', help='Sync to specified git revision',
25 dest='version')
David Jamesef74b1c2012-11-12 07:47:47 -080026
27 parser.add_argument('--internal', help='Sync internal version of Chrome',
28 action='store_true', default=False)
David Jamesef74b1c2012-11-12 07:47:47 -080029 parser.add_argument('--reset', help='Revert local changes',
30 action='store_true', default=False)
31 parser.add_argument('--gclient', help=commandline.argparse.SUPPRESS,
32 default='gclient')
Ben Chan2e19e4e2015-01-06 20:17:15 -080033 parser.add_argument('--gclient_template', help='Template gclient input file')
David Jamesef74b1c2012-11-12 07:47:47 -080034 parser.add_argument('chrome_root', help='Directory to sync chrome in')
35
36 return parser
37
38
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040039def main(argv):
David Jamesef74b1c2012-11-12 07:47:47 -080040 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040041 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080042
43 # Revert any lingering local changes.
44 if not osutils.SafeMakedirs(options.chrome_root) and options.reset:
45 try:
46 gclient.Revert(options.gclient, options.chrome_root)
47 except cros_build_lib.RunCommandError:
48 osutils.RmDir(options.chrome_root)
49 osutils.SafeMakedirs(options.chrome_root)
50
51 # Sync new Chrome.
52 gclient.WriteConfigFile(options.gclient, options.chrome_root,
Ben Chan2e19e4e2015-01-06 20:17:15 -080053 options.internal, options.version,
54 options.gclient_template)
Ryan Cui21aa0be2013-05-02 10:34:21 -070055 sync_fn = functools.partial(
56 gclient.Sync, options.gclient, options.chrome_root, reset=options.reset)
57
58 # Sync twice when run with --reset, which implies 'gclient sync -D'.
59 #
60 # There's a bug with 'gclient sync -D' that gets hit when the location of a
61 # dependency checkout (in the DEPS file) is moved to a path that contains
62 # (in a directory fashion) its old path. E.g., when Blink is moved from
63 # Webkit/Source/ to Webkit/. When this happens, a 'gclient sync -D' will
64 # blow away Webkit/Source/ after the sync, since it is no longer in the
65 # DEPS file, leaving the Blink checkout missing a Source/ subdirectory.
66 #
67 # This bug also gets hit the other way around - E.g., if Blink moves from
68 # Webkit/ to Webkit/Source/.
69 #
70 # To work around this, we sync twice, so that any directories deleted by
71 # the first sync will be restored in the second.
72 #
73 # TODO(rcui): Remove this workaround when the bug is fixed in gclient, or
74 # replace with a more sophisticated solution that syncs twice only when any
75 # paths in the DEPS file cannot be found after initial sync.
76 if options.reset:
77 sync_fn()
78 sync_fn()
David Jamesef74b1c2012-11-12 07:47:47 -080079 return 0