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 sys
21
22from containers.arg import Arg
23from containers.arg import ModuleType
24from resolver.interface.args_resolver_interface import ArgsResolverInterface
25from modules.interface.push_module_interface import PushModuleInterface
26from util.component_util import ComponentUtil
27from exceptions.ohos_exception import OHOSException
28from services.hdc import CMDTYPE
29
30
31class PushArgsResolver(ArgsResolverInterface):
32
33    def __init__(self, args_dict: dict):
34        super().__init__(args_dict)
35
36    @staticmethod
37    def resolve_part(target_arg: Arg, push_module: PushModuleInterface):
38        part_name = target_arg.arg_value
39        if len(sys.argv) > 2 and not sys.argv[2].startswith("-"): # 第一个部件名参数
40            part_name = sys.argv[2]
41        if part_name:
42            push_module.hdc.regist_flag('part_name', part_name)
43
44    @staticmethod
45    def resolve_target(target_arg: Arg, push_module: PushModuleInterface):
46        if target_arg.arg_value:
47            push_module.hdc.regist_flag('target', target_arg.arg_value)
48        else:
49            raise OHOSException('ERROR argument "hb push no target set". ')
50
51    @staticmethod
52    def resolve_list_targets(target_arg: Arg, push_module: PushModuleInterface):
53        if target_arg.arg_value:
54            push_module.hdc.execute_hdc_cmd(CMDTYPE.LIST_TARGETS)
55            Arg.write_args_file("list_targets", False, ModuleType.PUSH)
56
57    @staticmethod
58    def resolve_reboot(target_arg: Arg, push_module: PushModuleInterface):
59        if target_arg.arg_value:
60            push_module.hdc.regist_flag('reboot', True)
61
62    @staticmethod
63    def resolve_src(target_arg: Arg, push_module: PushModuleInterface):
64        if target_arg.arg_value:
65            push_module.hdc.regist_flag('src', target_arg.arg_value)
66            Arg.write_args_file("src", "", ModuleType.PUSH)