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 | |
| 27 | chrome_root = input_proto.paths.chrome |
| 28 | cache_dir = input_proto.paths.cache |
| 29 | chroot_path = input_proto.paths.chroot |
| 30 | |
| 31 | if chroot_path and not os.path.isabs(chroot_path): |
| 32 | cros_build_lib.Die('The chroot path must be absolute.') |
| 33 | |
| 34 | paths = sdk.ChrootPaths(cache_dir=cache_dir, chrome_root=chrome_root, |
| 35 | chroot_path=chroot_path) |
| 36 | args = sdk.CreateArguments(replace=replace, bootstrap=bootstrap, |
| 37 | use_image=use_image, paths=paths) |
| 38 | |
| 39 | version = sdk.Create(args) |
| 40 | |
| 41 | if version: |
| 42 | output_proto.version.version = version |
| 43 | else: |
| 44 | # This should be very rare, if ever used, but worth noting. |
| 45 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 46 | 'error creating the chroot that was not detected.') |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def Update(input_proto, output_proto): |
| 50 | """Update the chroot. |
| 51 | |
| 52 | Args: |
| 53 | input_proto (UpdateRequest): The input proto. |
| 54 | output_proto (UpdateResponse): The output proto. |
| 55 | """ |
| 56 | build_source = input_proto.flags.build_source |
| 57 | targets = [target.name for target in input_proto.toolchain_targets] |
| 58 | |
| 59 | args = sdk.UpdateArguments(build_source=build_source, |
| 60 | toolchain_targets=targets) |
| 61 | version = sdk.Update(args) |
| 62 | |
| 63 | if version: |
| 64 | output_proto.version.version = version |
| 65 | else: |
| 66 | # This should be very rare, if ever used, but worth noting. |
| 67 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 68 | 'error creating the chroot that was not detected.') |