blob: fdcdc4ff734d43d2d7fd907729b53a32d96d84b2 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2018 The ChromiumOS Authors
Alex Kleine66c64d2018-09-11 10:27:49 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Update or initialize the chroot version.
6
7The chroot version hooks provide manually created update files to handle
8tasks that need to be manually run. See also `update_chroot` for updating
9the toolchain and some other packages on the chroot. The `update_chroot` script
10also calls this script. The chroot version and installed package versions are
11not strongly correlated.
12"""
13
Alex Klein61ec2512022-10-21 11:02:09 -060014import logging
15
Alex Kleine66c64d2018-09-11 10:27:49 -060016from chromite.lib import commandline
17from chromite.lib import cros_build_lib
18from chromite.lib import cros_sdk_lib
19
20
21def GetParser():
Alex Klein1699fab2022-09-08 08:46:06 -060022 """Build the ArgumentParser."""
23 parser = commandline.ArgumentParser(description=__doc__)
24 parser.add_argument(
25 "-i",
26 "--init-latest",
27 action="store_true",
28 default=False,
29 help="Create the version file with the latest version "
30 "if it doesn't exist.",
31 )
Alex Kleine66c64d2018-09-11 10:27:49 -060032
Alex Klein1699fab2022-09-08 08:46:06 -060033 return parser
Alex Kleine66c64d2018-09-11 10:27:49 -060034
35
36def _ParseArgs(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060037 """Parse arguments."""
38 parser = GetParser()
Alex Kleine66c64d2018-09-11 10:27:49 -060039
Alex Klein1699fab2022-09-08 08:46:06 -060040 opts = parser.parse_args(argv)
41 opts.Freeze()
Alex Kleine66c64d2018-09-11 10:27:49 -060042
Alex Klein1699fab2022-09-08 08:46:06 -060043 return opts
Alex Kleine66c64d2018-09-11 10:27:49 -060044
45
46def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060047 """Main function."""
Alex Klein61ec2512022-10-21 11:02:09 -060048 commandline.RunInsideChroot()
Alex Kleine66c64d2018-09-11 10:27:49 -060049
Alex Klein1699fab2022-09-08 08:46:06 -060050 opts = _ParseArgs(argv)
Alex Kleine66c64d2018-09-11 10:27:49 -060051
Alex Klein1699fab2022-09-08 08:46:06 -060052 if opts.init_latest:
53 cros_sdk_lib.InitLatestVersion()
54 else:
55 try:
56 cros_sdk_lib.RunChrootVersionHooks()
Alex Klein61ec2512022-10-21 11:02:09 -060057 except cros_sdk_lib.InvalidChrootVersionError as e:
Alex Klein1699fab2022-09-08 08:46:06 -060058 cros_build_lib.Die(e)
Alex Klein61ec2512022-10-21 11:02:09 -060059 except cros_sdk_lib.Error as e:
60 logging.error(e)
61 logging.warning(
62 "Chroot version hooks failed. Please file a bug with the CrOS "
63 "Build team with the output above (go/cros-build-bug). "
64 "If you are willing to lose all built boards, you can bypass "
65 "the issue by replacing your chroot with:\n"
66 "\tcros_sdk --replace"
67 )
68 return 1