blob: d9408c0a10ab044e0edcc18338622c00bf531a76 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2012 The ChromiumOS Authors
David Jamesef74b1c2012-11-12 07:47:47 -08002# 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
Ryan Cui21aa0be2013-05-02 10:34:21 -07009
David Jamesef74b1c2012-11-12 07:47:47 -080010from chromite.lib import commandline
Chris McDonald59650c32021-07-20 15:29:28 -060011from chromite.lib import constants
David Jamesef74b1c2012-11-12 07:47:47 -080012from chromite.lib import cros_build_lib
13from chromite.lib import gclient
14from chromite.lib import osutils
15
16
17def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060018 """Creates the argparse parser."""
19 parser = commandline.ArgumentParser(description=__doc__)
David Jamesef74b1c2012-11-12 07:47:47 -080020
Alex Klein1699fab2022-09-08 08:46:06 -060021 version = parser.add_mutually_exclusive_group()
22 version.add_argument(
23 "--tag", help="Sync to specified Chrome release", dest="version"
24 )
25 version.add_argument(
26 "--revision", help="Sync to specified git revision", dest="version"
27 )
David Jamesef74b1c2012-11-12 07:47:47 -080028
Alex Klein1699fab2022-09-08 08:46:06 -060029 parser.add_argument(
30 "--internal",
31 help="Sync internal version of Chrome",
32 action="store_true",
33 default=False,
34 )
35 parser.add_argument(
36 "--reset",
37 help="Revert local changes",
38 action="store_true",
39 default=False,
40 )
41 parser.add_argument("--gclient", help=argparse.SUPPRESS, default=None)
42 parser.add_argument(
43 "--gclient_template", help="Template gclient input file"
44 )
45 parser.add_argument(
46 "--skip_cache",
47 help="Skip using git cache",
48 dest="use_cache",
49 action="store_false",
50 )
51 parser.add_argument(
52 "--git_cache_dir", type="path", help="Define explicit git cache."
53 )
54 parser.add_argument("chrome_root", help="Directory to sync chrome in")
David Jamesef74b1c2012-11-12 07:47:47 -080055
Alex Klein1699fab2022-09-08 08:46:06 -060056 return parser
David Jamesef74b1c2012-11-12 07:47:47 -080057
58
Don Garrettb5b33d02016-06-01 09:37:58 -070059def SyncChrome(gclient_path, options):
Alex Klein1699fab2022-09-08 08:46:06 -060060 """Sync new Chrome."""
61 gclient.WriteConfigFile(
62 gclient_path,
63 options.chrome_root,
64 options.internal,
65 options.version,
66 options.gclient_template,
67 options.use_cache,
68 git_cache_dir=options.git_cache_dir,
69 )
70 try:
71 gclient.Sync(gclient_path, options.chrome_root, reset=options.reset)
72 except cros_build_lib.RunCommandError as e:
73 cros_build_lib.Die(f"gclient sync exited {e.returncode}")
Don Garrettb5b33d02016-06-01 09:37:58 -070074
75
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040076def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060077 parser = GetParser()
78 options = parser.parse_args(argv)
David Jamesef74b1c2012-11-12 07:47:47 -080079
Alex Klein1699fab2022-09-08 08:46:06 -060080 if options.gclient == "":
81 parser.error("--gclient can not be an empty string!")
82 gclient_path = options.gclient or osutils.Which("gclient")
83 if not gclient_path:
Mike Frysingera95870e2023-03-27 15:45:34 -040084 gclient_path = constants.DEPOT_TOOLS_DIR / "gclient"
Simran Basi7d7dbd82015-01-14 14:01:55 -080085
Alex Klein1699fab2022-09-08 08:46:06 -060086 try:
87 if options.reset:
88 # Revert any lingering local changes.
89 gclient.Revert(gclient_path, options.chrome_root)
David Jamesef74b1c2012-11-12 07:47:47 -080090
Alex Klein1699fab2022-09-08 08:46:06 -060091 SyncChrome(gclient_path, options)
92 except cros_build_lib.RunCommandError:
93 # If we have an error resetting, or syncing, we clobber, and fresh sync.
94 logging.warning("Chrome checkout appears corrupt. Clobbering.")
95 osutils.RmDir(options.chrome_root, ignore_missing=True, sudo=True)
96 osutils.SafeMakedirsNonRoot(options.chrome_root)
97 SyncChrome(gclient_path, options)
Ryan Cui21aa0be2013-05-02 10:34:21 -070098
Alex Klein1699fab2022-09-08 08:46:06 -060099 return 0