blob: 26b61854eaa7a1ecc66b9f0a9e3c5c64c1f64cdf [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():
Alex Klein1699fab2022-09-08 08:46:06 -060019 """Creates the argparse parser."""
20 parser = commandline.ArgumentParser(description=__doc__)
David Jamesef74b1c2012-11-12 07:47:47 -080021
Alex Klein1699fab2022-09-08 08:46:06 -060022 version = parser.add_mutually_exclusive_group()
23 version.add_argument(
24 "--tag", help="Sync to specified Chrome release", dest="version"
25 )
26 version.add_argument(
27 "--revision", help="Sync to specified git revision", dest="version"
28 )
David Jamesef74b1c2012-11-12 07:47:47 -080029
Alex Klein1699fab2022-09-08 08:46:06 -060030 parser.add_argument(
31 "--internal",
32 help="Sync internal version of Chrome",
33 action="store_true",
34 default=False,
35 )
36 parser.add_argument(
37 "--reset",
38 help="Revert local changes",
39 action="store_true",
40 default=False,
41 )
42 parser.add_argument("--gclient", help=argparse.SUPPRESS, default=None)
43 parser.add_argument(
44 "--gclient_template", help="Template gclient input file"
45 )
46 parser.add_argument(
47 "--skip_cache",
48 help="Skip using git cache",
49 dest="use_cache",
50 action="store_false",
51 )
52 parser.add_argument(
53 "--git_cache_dir", type="path", help="Define explicit git cache."
54 )
55 parser.add_argument("chrome_root", help="Directory to sync chrome in")
David Jamesef74b1c2012-11-12 07:47:47 -080056
Alex Klein1699fab2022-09-08 08:46:06 -060057 return parser
David Jamesef74b1c2012-11-12 07:47:47 -080058
59
Don Garrettb5b33d02016-06-01 09:37:58 -070060def SyncChrome(gclient_path, options):
Alex Klein1699fab2022-09-08 08:46:06 -060061 """Sync new Chrome."""
62 gclient.WriteConfigFile(
63 gclient_path,
64 options.chrome_root,
65 options.internal,
66 options.version,
67 options.gclient_template,
68 options.use_cache,
69 git_cache_dir=options.git_cache_dir,
70 )
71 try:
72 gclient.Sync(gclient_path, options.chrome_root, reset=options.reset)
73 except cros_build_lib.RunCommandError as e:
74 cros_build_lib.Die(f"gclient sync exited {e.returncode}")
Don Garrettb5b33d02016-06-01 09:37:58 -070075
76
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040077def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060078 parser = GetParser()
79 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080080
Alex Klein1699fab2022-09-08 08:46:06 -060081 if options.gclient == "":
82 parser.error("--gclient can not be an empty string!")
83 gclient_path = options.gclient or osutils.Which("gclient")
84 if not gclient_path:
85 gclient_path = os.path.join(constants.DEPOT_TOOLS_DIR, "gclient")
Simran Basi7d7dbd82015-01-14 14:01:55 -080086
Alex Klein1699fab2022-09-08 08:46:06 -060087 try:
88 if options.reset:
89 # Revert any lingering local changes.
90 gclient.Revert(gclient_path, options.chrome_root)
David Jamesef74b1c2012-11-12 07:47:47 -080091
Alex Klein1699fab2022-09-08 08:46:06 -060092 SyncChrome(gclient_path, options)
93 except cros_build_lib.RunCommandError:
94 # If we have an error resetting, or syncing, we clobber, and fresh sync.
95 logging.warning("Chrome checkout appears corrupt. Clobbering.")
96 osutils.RmDir(options.chrome_root, ignore_missing=True, sudo=True)
97 osutils.SafeMakedirsNonRoot(options.chrome_root)
98 SyncChrome(gclient_path, options)
Ryan Cui21aa0be2013-05-02 10:34:21 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 return 0