blob: c85ccfba7dbfe08fcf7cf0c7cfeac4f54c780118 [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 Frysinger49c6e1f2022-04-14 15:41:40 -04007import argparse
Chris McDonald59650c32021-07-20 15:29:28 -06008import logging
Simran Basi7d7dbd82015-01-14 14:01:55 -08009import os
Ryan Cui21aa0be2013-05-02 10:34:21 -070010
David Jamesef74b1c2012-11-12 07:47:47 -080011from chromite.lib import commandline
Chris McDonald59650c32021-07-20 15:29:28 -060012from chromite.lib import constants
David Jamesef74b1c2012-11-12 07:47:47 -080013from chromite.lib import cros_build_lib
14from chromite.lib import gclient
15from chromite.lib import osutils
16
17
18def GetParser():
19 """Creates the argparse parser."""
20 parser = commandline.ArgumentParser(description=__doc__)
21
22 version = parser.add_mutually_exclusive_group()
23 version.add_argument('--tag', help='Sync to specified Chrome release',
24 dest='version')
Stefan Zagerd49d9ff2014-08-15 21:33:37 -070025 version.add_argument('--revision', help='Sync to specified git revision',
26 dest='version')
David Jamesef74b1c2012-11-12 07:47:47 -080027
28 parser.add_argument('--internal', help='Sync internal version of Chrome',
29 action='store_true', default=False)
David Jamesef74b1c2012-11-12 07:47:47 -080030 parser.add_argument('--reset', help='Revert local changes',
31 action='store_true', default=False)
Mike Frysinger49c6e1f2022-04-14 15:41:40 -040032 parser.add_argument('--gclient', help=argparse.SUPPRESS, default=None)
Ben Chan2e19e4e2015-01-06 20:17:15 -080033 parser.add_argument('--gclient_template', help='Template gclient input file')
Don Garrettc4424eb2018-06-20 17:45:37 +000034 parser.add_argument('--skip_cache', help='Skip using git cache',
35 dest='use_cache', action='store_false')
Don Garrett85b258b2019-02-13 17:33:31 -080036 parser.add_argument('--git_cache_dir', type='path',
37 help='Define explicit git cache.')
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,
Don Garrett85b258b2019-02-13 17:33:31 -080047 options.gclient_template, options.use_cache,
48 git_cache_dir=options.git_cache_dir)
Mike Frysinger2b19e622022-07-26 21:23:30 -040049 try:
50 gclient.Sync(gclient_path, options.chrome_root, reset=options.reset)
51 except cros_build_lib.RunCommandError as e:
52 cros_build_lib.Die(f'gclient sync exited {e.returncode}')
Don Garrettb5b33d02016-06-01 09:37:58 -070053
54
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040055def main(argv):
David Jamesef74b1c2012-11-12 07:47:47 -080056 parser = GetParser()
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040057 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080058
Mike Frysingera0313d02017-07-10 16:44:43 -040059 if options.gclient == '':
Simran Basi7d7dbd82015-01-14 14:01:55 -080060 parser.error('--gclient can not be an empty string!')
61 gclient_path = options.gclient or osutils.Which('gclient')
62 if not gclient_path:
63 gclient_path = os.path.join(constants.DEPOT_TOOLS_DIR, 'gclient')
64
Don Garrettb5b33d02016-06-01 09:37:58 -070065 try:
66 if options.reset:
67 # Revert any lingering local changes.
Simran Basi7d7dbd82015-01-14 14:01:55 -080068 gclient.Revert(gclient_path, options.chrome_root)
David Jamesef74b1c2012-11-12 07:47:47 -080069
Don Garrettb5b33d02016-06-01 09:37:58 -070070 SyncChrome(gclient_path, options)
71 except cros_build_lib.RunCommandError:
72 # If we have an error resetting, or syncing, we clobber, and fresh sync.
73 logging.warning('Chrome checkout appears corrupt. Clobbering.')
Don Garrettad54ff22016-11-04 12:38:13 -070074 osutils.RmDir(options.chrome_root, ignore_missing=True, sudo=True)
Prathmesh Prabhue149b952016-11-11 10:41:20 -080075 osutils.SafeMakedirsNonRoot(options.chrome_root)
Don Garrettb5b33d02016-06-01 09:37:58 -070076 SyncChrome(gclient_path, options)
Ryan Cui21aa0be2013-05-02 10:34:21 -070077
David Jamesef74b1c2012-11-12 07:47:47 -080078 return 0