1#!/usr/bin/env python
2#
3# Copyright 2018 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16r"""BaseAVDCreate class.
17
18Parent class that will hold common logic for AVD creation use cases.
19"""
20
21from acloud.internal import constants
22from acloud.internal.lib import utils
23
24
25class BaseAVDCreate(object):
26    """Base class for all AVD intance creation classes."""
27
28    def _CreateAVD(self, avd_spec, no_prompts):
29        """Do the actual creation work, should be overridden by child classes.
30
31        Args:
32            avd_spec: AVDSpec object that tells us what we're going to create.
33            no_prompts: Boolean, True to skip all prompts.
34        """
35        raise NotImplementedError
36
37    def Create(self, avd_spec, no_prompts):
38        """Create the AVD.
39
40        Args:
41            avd_spec: AVDSpec object that tells us what we're going to create.
42            no_prompts: Boolean, True to skip all prompts.
43        """
44        self.PrintAvdDetails(avd_spec)
45        results = self._CreateAVD(avd_spec, no_prompts)
46        utils.PrintDeviceSummary(results)
47        return results
48
49    @staticmethod
50    def PrintAvdDetails(avd_spec):
51        """Display spec information to user.
52
53        Example:
54            Creating remote AVD instance with the following details:
55            Image:
56              aosp/master - aosp_cf_x86_phone-userdebug [1234]
57            hw config:
58              cpu - 2
59              ram - 2GB
60              disk - 10GB
61              display - 1024x862 (224 DPI)
62
63        Args:
64            avd_spec: AVDSpec object that tells us what we're going to create.
65        """
66        utils.PrintColorString(
67            "Creating %s AVD instance with the following details:" %
68            avd_spec.instance_type)
69        if avd_spec.image_source == constants.IMAGE_SRC_LOCAL:
70            utils.PrintColorString("Image (local):")
71            utils.PrintColorString("  %s" % (avd_spec.local_image_dir or
72                                             avd_spec.local_image_artifact))
73        elif avd_spec.image_source == constants.IMAGE_SRC_REMOTE:
74            utils.PrintColorString("Image:")
75            utils.PrintColorString(
76                "  %s - %s [%s]" %
77                (avd_spec.remote_image[constants.BUILD_BRANCH],
78                 avd_spec.remote_image[constants.BUILD_TARGET],
79                 avd_spec.remote_image[constants.BUILD_ID]))
80        utils.PrintColorString("hw config:")
81        utils.PrintColorString("  cpu - %s" % (avd_spec.hw_property[constants.HW_ALIAS_CPUS]))
82        utils.PrintColorString("  ram - %dGB" % (
83            int(avd_spec.hw_property[constants.HW_ALIAS_MEMORY]) / 1024))
84        if constants.HW_ALIAS_DISK in avd_spec.hw_property:
85            utils.PrintColorString("  disk - %dGB" % (
86                int(avd_spec.hw_property[constants.HW_ALIAS_DISK]) / 1024))
87        utils.PrintColorString(
88            "  display - %sx%s (%s DPI)" %
89            (avd_spec.hw_property[constants.HW_X_RES],
90             avd_spec.hw_property[constants.HW_Y_RES],
91             avd_spec.hw_property[constants.HW_ALIAS_DPI]))
92        utils.PrintColorString("\n")
93