Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 2 | # 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 | |
| 7 | The chroot version hooks provide manually created update files to handle |
| 8 | tasks that need to be manually run. See also `update_chroot` for updating |
| 9 | the toolchain and some other packages on the chroot. The `update_chroot` script |
| 10 | also calls this script. The chroot version and installed package versions are |
| 11 | not strongly correlated. |
| 12 | """ |
| 13 | |
Alex Klein | 61ec251 | 2022-10-21 11:02:09 -0600 | [diff] [blame] | 14 | import logging |
| 15 | |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 16 | from chromite.lib import commandline |
| 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.lib import cros_sdk_lib |
| 19 | |
| 20 | |
| 21 | def GetParser(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | """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 Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | return parser |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 34 | |
| 35 | |
| 36 | def _ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | """Parse arguments.""" |
| 38 | parser = GetParser() |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | opts = parser.parse_args(argv) |
| 41 | opts.Freeze() |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | return opts |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | """Main function.""" |
Alex Klein | 61ec251 | 2022-10-21 11:02:09 -0600 | [diff] [blame] | 48 | commandline.RunInsideChroot() |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | opts = _ParseArgs(argv) |
Alex Klein | e66c64d | 2018-09-11 10:27:49 -0600 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | if opts.init_latest: |
| 53 | cros_sdk_lib.InitLatestVersion() |
| 54 | else: |
| 55 | try: |
| 56 | cros_sdk_lib.RunChrootVersionHooks() |
Alex Klein | 61ec251 | 2022-10-21 11:02:09 -0600 | [diff] [blame] | 57 | except cros_sdk_lib.InvalidChrootVersionError as e: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | cros_build_lib.Die(e) |
Alex Klein | 61ec251 | 2022-10-21 11:02:09 -0600 | [diff] [blame] | 59 | 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 |