David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 1 | # 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 Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Ryan Cui | 21aa0be | 2013-05-02 10:34:21 -0700 | [diff] [blame] | 9 | import functools |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 10 | import os |
Ryan Cui | 21aa0be | 2013-05-02 10:34:21 -0700 | [diff] [blame] | 11 | |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 12 | from chromite.cbuildbot import constants |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 13 | from chromite.lib import commandline |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import gclient |
| 16 | from chromite.lib import osutils |
| 17 | |
| 18 | |
| 19 | def 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 Zager | d49d9ff | 2014-08-15 21:33:37 -0700 | [diff] [blame] | 26 | version.add_argument('--revision', help='Sync to specified git revision', |
| 27 | dest='version') |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 28 | |
| 29 | parser.add_argument('--internal', help='Sync internal version of Chrome', |
| 30 | action='store_true', default=False) |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 31 | parser.add_argument('--reset', help='Revert local changes', |
| 32 | action='store_true', default=False) |
| 33 | parser.add_argument('--gclient', help=commandline.argparse.SUPPRESS, |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 34 | default=None) |
Ben Chan | 2e19e4e | 2015-01-06 20:17:15 -0800 | [diff] [blame] | 35 | parser.add_argument('--gclient_template', help='Template gclient input file') |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 36 | parser.add_argument('chrome_root', help='Directory to sync chrome in') |
| 37 | |
| 38 | return parser |
| 39 | |
| 40 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 41 | def main(argv): |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 42 | parser = GetParser() |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 43 | options = parser.parse_args(argv) |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 44 | |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 45 | if options.gclient is '': |
| 46 | parser.error('--gclient can not be an empty string!') |
| 47 | gclient_path = options.gclient or osutils.Which('gclient') |
| 48 | if not gclient_path: |
| 49 | gclient_path = os.path.join(constants.DEPOT_TOOLS_DIR, 'gclient') |
| 50 | |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 51 | # Revert any lingering local changes. |
| 52 | if not osutils.SafeMakedirs(options.chrome_root) and options.reset: |
| 53 | try: |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 54 | gclient.Revert(gclient_path, options.chrome_root) |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 55 | except cros_build_lib.RunCommandError: |
| 56 | osutils.RmDir(options.chrome_root) |
| 57 | osutils.SafeMakedirs(options.chrome_root) |
| 58 | |
| 59 | # Sync new Chrome. |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 60 | gclient.WriteConfigFile(gclient_path, options.chrome_root, |
Ben Chan | 2e19e4e | 2015-01-06 20:17:15 -0800 | [diff] [blame] | 61 | options.internal, options.version, |
| 62 | options.gclient_template) |
Ryan Cui | 21aa0be | 2013-05-02 10:34:21 -0700 | [diff] [blame] | 63 | sync_fn = functools.partial( |
Simran Basi | 7d7dbd8 | 2015-01-14 14:01:55 -0800 | [diff] [blame] | 64 | gclient.Sync, gclient_path, options.chrome_root, reset=options.reset) |
Ryan Cui | 21aa0be | 2013-05-02 10:34:21 -0700 | [diff] [blame] | 65 | |
| 66 | # Sync twice when run with --reset, which implies 'gclient sync -D'. |
| 67 | # |
| 68 | # There's a bug with 'gclient sync -D' that gets hit when the location of a |
| 69 | # dependency checkout (in the DEPS file) is moved to a path that contains |
| 70 | # (in a directory fashion) its old path. E.g., when Blink is moved from |
| 71 | # Webkit/Source/ to Webkit/. When this happens, a 'gclient sync -D' will |
| 72 | # blow away Webkit/Source/ after the sync, since it is no longer in the |
| 73 | # DEPS file, leaving the Blink checkout missing a Source/ subdirectory. |
| 74 | # |
| 75 | # This bug also gets hit the other way around - E.g., if Blink moves from |
| 76 | # Webkit/ to Webkit/Source/. |
| 77 | # |
| 78 | # To work around this, we sync twice, so that any directories deleted by |
| 79 | # the first sync will be restored in the second. |
| 80 | # |
| 81 | # TODO(rcui): Remove this workaround when the bug is fixed in gclient, or |
| 82 | # replace with a more sophisticated solution that syncs twice only when any |
| 83 | # paths in the DEPS file cannot be found after initial sync. |
| 84 | if options.reset: |
| 85 | sync_fn() |
| 86 | sync_fn() |
David James | ef74b1c | 2012-11-12 07:47:47 -0800 | [diff] [blame] | 87 | return 0 |