blob: dabb0f882c90b3d9f901fc748788d77157852b37 [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# -*- 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
8from __future__ import print_function
9
10import os
11
12from chromite.lib import cros_build_lib
13from chromite.service import sdk
14
15
16def 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 Klein00aa8072019-04-15 16:36:00 -060027 chroot_path = input_proto.chroot.path
28 cache_dir = input_proto.chroot.cache_dir
Alex Klein19c4cc42019-02-27 14:47:57 -070029
30 if chroot_path and not os.path.isabs(chroot_path):
31 cros_build_lib.Die('The chroot path must be absolute.')
32
Alex Klein00aa8072019-04-15 16:36:00 -060033 paths = sdk.ChrootPaths(cache_dir=cache_dir, chroot_path=chroot_path)
Alex Klein19c4cc42019-02-27 14:47:57 -070034 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 Kleinaa5c4172019-02-27 17:12:20 -070045
46
47def 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.')