• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import os
20import subprocess
21
22from containers.arg import Arg, ModuleType
23from resolver.interface.args_resolver_interface import ArgsResolverInterface
24from modules.interface.env_module_interface import EnvModuleInterface
25from resources.global_var import ENV_SETUP_FILE, ROOT_CONFIG_FILE, BUILD_CONFIG_FILE
26from exceptions.ohos_exception import OHOSException
27from util.log_util import LogUtil
28from scripts.tools_checker import check_os_version, check_build_requried_packages
29from containers.status import throw_exception
30from util.io_util import IoUtil
31
32
33class EnvArgsResolver(ArgsResolverInterface):
34
35    def __init__(self, args_dict: dict):
36        super().__init__(args_dict)
37
38    @staticmethod
39    def resolve_check(target_arg: Arg, set_module: EnvModuleInterface):
40        if target_arg.arg_value:
41            host_info = check_os_version()
42            packages_info = check_build_requried_packages(
43                host_info[1], check=False)
44            for package in packages_info:
45                if isinstance(package, list):
46                    package.sort()
47            LogUtil.hb_info('Necessary package: {}'.format(
48                ', '.join(packages_info[0])))
49            LogUtil.hb_info('Installed package: {}'.format(
50                ', '.join(packages_info[1])))
51            LogUtil.hb_info('Uninstalled package: {}'.format(
52                ', '.join(packages_info[2])))
53            if len(packages_info[2]) > 0:
54                LogUtil.hb_info(
55                    'Run "sudo apt-get install {}" to install dependencies'.format(' '.join(packages_info[2])))
56
57            if os.path.exists(ROOT_CONFIG_FILE):
58                config_json = ROOT_CONFIG_FILE
59            else:
60                config_json = BUILD_CONFIG_FILE
61            json_data = IoUtil.read_json_file(config_json)
62            root_path = json_data.get('root_path', 'not set')
63            board = json_data.get('board', 'not set')
64            kernel = json_data.get('kernel', 'not set')
65            product = json_data.get('product', 'not set')
66            product_path = json_data.get('product_path', 'not set')
67            device_path = json_data.get('device_path', 'not set')
68            device_company = json_data.get('device_company', 'not set')
69
70            LogUtil.hb_info('root path: {}'.format(root_path))
71            LogUtil.hb_info('board: {}'.format(board))
72            LogUtil.hb_info('kernel: {}'.format(kernel))
73            LogUtil.hb_info('product: {}'.format(product))
74            LogUtil.hb_info('product path: {}'.format(product_path))
75            LogUtil.hb_info('device path: {}'.format(device_path))
76            LogUtil.hb_info('device company: {}'.format(device_company))
77
78    @staticmethod
79    @throw_exception
80    def resolve_install(target_arg: Arg, set_module: EnvModuleInterface):
81        if target_arg.arg_value:
82            if os.path.exists(ENV_SETUP_FILE):
83                LogUtil.hb_info('Starting install all dependencies...')
84                LogUtil.hb_info(
85                    'Please enter your sudo password for running apt-install command')
86                subprocess.run('bash {}'.format(ENV_SETUP_FILE))
87            else:
88                raise OHOSException("There is no {} file", "0000")
89
90    @staticmethod
91    def resolve_target_cpu(target_arg: Arg, env_module: EnvModuleInterface):
92        pass
93
94    @staticmethod
95    def resolve_target_os(target_arg: Arg, env_module: EnvModuleInterface):
96        pass
97
98    @staticmethod
99    def resolve_part(target_arg: Arg, env_module: EnvModuleInterface):
100        pass
101
102    @staticmethod
103    def resolve_clean(target_arg: Arg, env_module: EnvModuleInterface):
104        if target_arg.arg_value:
105            Arg.clean_args_file_by_type(ModuleType.ENV)