blob: f398060a0e7c2d67978a8734b73b6f12df08b598 [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
Simran Basi7d7dbd82015-01-14 14:01:55 -08009import os
Ryan Cui21aa0be2013-05-02 10:34:21 -070010
Aviv Keshetb7519e12016-10-04 00:50:00 -070011from chromite.lib import constants
David Jamesef74b1c2012-11-12 07:47:47 -080012from chromite.lib import commandline
13from chromite.lib import cros_build_lib
Don Garrettb5b33d02016-06-01 09:37:58 -070014from chromite.lib import cros_logging as logging
David Jamesef74b1c2012-11-12 07:47:47 -080015from 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
Don Garrettb5b33d02016-06-01 09:37:58 -070043def SyncChrome(gclient_path, options):
44 """Sync new Chrome."""
45 gclient.WriteConfigFile(gclient_path, options.chrome_root,
46 options.internal, options.version,
47 options.gclient_template, options.use_cache)
48 gclient.Sync(gclient_path, options.chrome_root, reset=options.reset)
49
50
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040051def main(argv):
David Jamesef74b1c2012-11-12 07:47:47 -080052 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040053 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080054
Simran Basi7d7dbd82015-01-14 14:01:55 -080055 if options.gclient is '':
56 parser.error('--gclient can not be an empty string!')
57 gclient_path = options.gclient or osutils.Which('gclient')
58 if not gclient_path:
59 gclient_path = os.path.join(constants.DEPOT_TOOLS_DIR, 'gclient')
60
Don Garrettb5b33d02016-06-01 09:37:58 -070061 try:
62 if options.reset:
63 # Revert any lingering local changes.
Simran Basi7d7dbd82015-01-14 14:01:55 -080064 gclient.Revert(gclient_path, options.chrome_root)
David Jamesef74b1c2012-11-12 07:47:47 -080065
Don Garrettb5b33d02016-06-01 09:37:58 -070066 SyncChrome(gclient_path, options)
67 except cros_build_lib.RunCommandError:
68 # If we have an error resetting, or syncing, we clobber, and fresh sync.
69 logging.warning('Chrome checkout appears corrupt. Clobbering.')
Aviv Keshet35fe07c2016-06-14 17:19:53 -070070 osutils.RmDir(options.chrome_root, ignore_missing=True)
Don Garrettb5b33d02016-06-01 09:37:58 -070071 osutils.SafeMakedirs(options.chrome_root)
72 SyncChrome(gclient_path, options)
Ryan Cui21aa0be2013-05-02 10:34:21 -070073
David Jamesef74b1c2012-11-12 07:47:47 -080074 return 0