Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """SDK chroot operations.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from chromite.lib import cros_build_lib |
| 13 | from chromite.service import sdk |
| 14 | |
| 15 | |
| 16 | def Create(input_proto, output_proto): |
| 17 | """Chroot creation, includes support for replacing an existing chroot. |
| 18 | |
| 19 | Args: |
| 20 | input_proto (CreateRequest): The input proto. |
| 21 | output_proto (CreateResponse): The output proto. |
| 22 | """ |
| 23 | replace = not input_proto.flags.no_replace |
| 24 | bootstrap = input_proto.flags.bootstrap |
| 25 | use_image = not input_proto.flags.no_use_image |
| 26 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 27 | chroot_path = input_proto.chroot.path |
| 28 | cache_dir = input_proto.chroot.cache_dir |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 29 | |
| 30 | if chroot_path and not os.path.isabs(chroot_path): |
| 31 | cros_build_lib.Die('The chroot path must be absolute.') |
| 32 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 33 | paths = sdk.ChrootPaths(cache_dir=cache_dir, chroot_path=chroot_path) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 34 | args = sdk.CreateArguments(replace=replace, bootstrap=bootstrap, |
| 35 | use_image=use_image, paths=paths) |
| 36 | |
| 37 | version = sdk.Create(args) |
| 38 | |
| 39 | if version: |
| 40 | output_proto.version.version = version |
| 41 | else: |
| 42 | # This should be very rare, if ever used, but worth noting. |
| 43 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 44 | 'error creating the chroot that was not detected.') |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 45 | |
| 46 | |
| 47 | def Update(input_proto, output_proto): |
| 48 | """Update the chroot. |
| 49 | |
| 50 | Args: |
| 51 | input_proto (UpdateRequest): The input proto. |
| 52 | output_proto (UpdateResponse): The output proto. |
| 53 | """ |
| 54 | build_source = input_proto.flags.build_source |
| 55 | targets = [target.name for target in input_proto.toolchain_targets] |
| 56 | |
| 57 | args = sdk.UpdateArguments(build_source=build_source, |
| 58 | toolchain_targets=targets) |
| 59 | version = sdk.Update(args) |
| 60 | |
| 61 | if version: |
| 62 | output_proto.version.version = version |
| 63 | else: |
| 64 | # This should be very rare, if ever used, but worth noting. |
| 65 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 66 | 'error creating the chroot that was not detected.') |