1# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Provide common implementation of image sources."""
15
16import logging
17
18from gsi_util.mounters import composite_mounter
19
20_DESCRIPTION = """The image sources to be mounted targets.
21
22An image source could be:
23
24 adb[:SERIAL_NUM]: form the device which be connected with adb
25  image file name: from the given image file, e.g. the file name of a GSI.
26                   If a image file is assigned to be the source of system
27                   image, gsi_util will detect system-as-root automatically.
28      folder name: from the given folder, e.g. the system/vendor folder in an
29                   Android build out folder.
30"""
31
32
33def create_composite_mounter_by_args(args):
34  """Creates a CompositeMounter by the images in given args."""
35
36  logging.info('Mount images...')
37  mounter = composite_mounter.CompositeMounter()
38  for partition in composite_mounter.SUPPORTED_PARTITIONS:
39    image_source = vars(args)[partition]
40    if image_source:
41      logging.info('  %s=%s', partition, image_source)
42      mounter.add_by_mount_target(partition, image_source)
43
44  if mounter.is_empty():
45    raise RuntimeError('Must give at least one image source.')
46
47  return mounter
48
49
50def add_argument_group(parser, required_images=None):
51  """Add a argument group into the given parser for image sources.
52
53  Args:
54    parser: The parser to be added the argument group.
55    required_images: A list contains the required images. e.g.
56      ['system', 'vendor']. Default is no required images.
57  """
58  # To avoid pylint W0102
59  required_images = required_images or []
60
61  group = parser.add_argument_group('image sources', _DESCRIPTION)
62  for partition in composite_mounter.SUPPORTED_PARTITIONS:
63    group.add_argument(
64        '--' + partition,
65        type=str,
66        required=partition in required_images,
67        help='{} image file name, folder name or "adb"'.format(partition))
68