13f085823Sopenharmony_ci#!/usr/bin/env python3
23f085823Sopenharmony_ci# coding=utf-8
33f085823Sopenharmony_ci
43f085823Sopenharmony_ci#
53f085823Sopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
63f085823Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
73f085823Sopenharmony_ci# you may not use this file except in compliance with the License.
83f085823Sopenharmony_ci# You may obtain a copy of the License at
93f085823Sopenharmony_ci#
103f085823Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
113f085823Sopenharmony_ci#
123f085823Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
133f085823Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
143f085823Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153f085823Sopenharmony_ci# See the License for the specific language governing permissions and
163f085823Sopenharmony_ci# limitations under the License.
173f085823Sopenharmony_ci#
183f085823Sopenharmony_ciimport json
193f085823Sopenharmony_ciimport os
203f085823Sopenharmony_ciimport shutil
213f085823Sopenharmony_ciimport platform
223f085823Sopenharmony_ciimport subprocess
233f085823Sopenharmony_ci
243f085823Sopenharmony_ci##############################################################################
253f085823Sopenharmony_ci##############################################################################
263f085823Sopenharmony_ci
273f085823Sopenharmony_ci__all__ = ["DeviceShell"]
283f085823Sopenharmony_ci
293f085823Sopenharmony_ciimport zipfile
303f085823Sopenharmony_ci
313f085823Sopenharmony_ciif platform.system() != 'Windows':
323f085823Sopenharmony_ci    QUOTATION_MARKS = "'"
333f085823Sopenharmony_cielse:
343f085823Sopenharmony_ci    QUOTATION_MARKS = "\""
353f085823Sopenharmony_ci
363f085823Sopenharmony_ciHDC_TOOLS = "hdc"
373f085823Sopenharmony_ci
383f085823Sopenharmony_ci
393f085823Sopenharmony_ci##############################################################################
403f085823Sopenharmony_ci##############################################################################
413f085823Sopenharmony_ci
423f085823Sopenharmony_ci
433f085823Sopenharmony_cidef get_package_name(hap_filepath):
443f085823Sopenharmony_ci    package_name = ""
453f085823Sopenharmony_ci
463f085823Sopenharmony_ci    if os.path.exists(hap_filepath):
473f085823Sopenharmony_ci        filename = os.path.basename(hap_filepath)
483f085823Sopenharmony_ci
493f085823Sopenharmony_ci        # unzip the hap file
503f085823Sopenharmony_ci        hap_bak_path = os.path.abspath(os.path.join(
513f085823Sopenharmony_ci            os.path.dirname(hap_filepath),
523f085823Sopenharmony_ci            "%s.bak" % filename))
533f085823Sopenharmony_ci        zf_desc = zipfile.ZipFile(hap_filepath)
543f085823Sopenharmony_ci        try:
553f085823Sopenharmony_ci            zf_desc.extractall(path=hap_bak_path)
563f085823Sopenharmony_ci        except RuntimeError as error:
573f085823Sopenharmony_ci            print("Unzip error: ", hap_bak_path)
583f085823Sopenharmony_ci        zf_desc.close()
593f085823Sopenharmony_ci
603f085823Sopenharmony_ci        # verify config.json file
613f085823Sopenharmony_ci        app_profile_path = os.path.join(hap_bak_path, "config.json")
623f085823Sopenharmony_ci        if os.path.isfile(app_profile_path):
633f085823Sopenharmony_ci            load_dict = {}
643f085823Sopenharmony_ci            with open(app_profile_path, 'r') as json_file:
653f085823Sopenharmony_ci                load_dict = json.load(json_file)
663f085823Sopenharmony_ci            profile_list = load_dict.values()
673f085823Sopenharmony_ci            for profile in profile_list:
683f085823Sopenharmony_ci                package_name = profile.get("package")
693f085823Sopenharmony_ci                if not package_name:
703f085823Sopenharmony_ci                    continue
713f085823Sopenharmony_ci                break
723f085823Sopenharmony_ci
733f085823Sopenharmony_ci        # delete hap_bak_path
743f085823Sopenharmony_ci        if os.path.exists(hap_bak_path):
753f085823Sopenharmony_ci            shutil.rmtree(hap_bak_path)
763f085823Sopenharmony_ci    else:
773f085823Sopenharmony_ci        print("file %s not exists" % hap_filepath)
783f085823Sopenharmony_ci
793f085823Sopenharmony_ci    return package_name
803f085823Sopenharmony_ci
813f085823Sopenharmony_ci##############################################################################
823f085823Sopenharmony_ci##############################################################################
833f085823Sopenharmony_ci
843f085823Sopenharmony_ci
853f085823Sopenharmony_ciclass DeviceShell:
863f085823Sopenharmony_ci    def __init__(self, conn_type, remote_ip="", device_sn="", repote_port="", name=""):
873f085823Sopenharmony_ci        self.conn_type = conn_type
883f085823Sopenharmony_ci        self.device_sn = device_sn
893f085823Sopenharmony_ci        self.name = name
903f085823Sopenharmony_ci        self.test_path = "data/test"
913f085823Sopenharmony_ci        if conn_type:
923f085823Sopenharmony_ci            self.device_params = self.get_device_hdc_para(
933f085823Sopenharmony_ci                device_sn
943f085823Sopenharmony_ci            )
953f085823Sopenharmony_ci        else:
963f085823Sopenharmony_ci            self.device_params = self.get_device_para(
973f085823Sopenharmony_ci                remote_ip, repote_port, device_sn)
983f085823Sopenharmony_ci        self.init_device()
993f085823Sopenharmony_ci
1003f085823Sopenharmony_ci    @classmethod
1013f085823Sopenharmony_ci    def get_device_para(cls, remote_ip="", remote_port="",
1023f085823Sopenharmony_ci                        device_sn=""):
1033f085823Sopenharmony_ci        if "" == remote_ip or "" == remote_port:
1043f085823Sopenharmony_ci            if "" == device_sn:
1053f085823Sopenharmony_ci                device_para = ""
1063f085823Sopenharmony_ci            else:
1073f085823Sopenharmony_ci                device_para = "-s %s" % device_sn
1083f085823Sopenharmony_ci        else:
1093f085823Sopenharmony_ci            if "" == device_sn:
1103f085823Sopenharmony_ci                device_para = "-H %s -P %s" % (remote_ip, remote_port)
1113f085823Sopenharmony_ci            else:
1123f085823Sopenharmony_ci                device_para = "-H %s -P %s -t %s" % (
1133f085823Sopenharmony_ci                    remote_ip, remote_port, device_sn)
1143f085823Sopenharmony_ci        return device_para
1153f085823Sopenharmony_ci
1163f085823Sopenharmony_ci    @classmethod
1173f085823Sopenharmony_ci    def get_device_hdc_para(cls, device_sn=""):
1183f085823Sopenharmony_ci        if " " == device_sn:
1193f085823Sopenharmony_ci            device_para = ""
1203f085823Sopenharmony_ci        else:
1213f085823Sopenharmony_ci            device_para = "-t %s" % device_sn
1223f085823Sopenharmony_ci
1233f085823Sopenharmony_ci        return device_para
1243f085823Sopenharmony_ci
1253f085823Sopenharmony_ci    @classmethod
1263f085823Sopenharmony_ci    def execute_command(cls, command, print_flag=True, timeout=900):
1273f085823Sopenharmony_ci        try:
1283f085823Sopenharmony_ci            if print_flag:
1293f085823Sopenharmony_ci                print("command: " + command)
1303f085823Sopenharmony_ci            if subprocess.call(command, shell=True, timeout=timeout) == 0:
1313f085823Sopenharmony_ci                print("results: successed")
1323f085823Sopenharmony_ci                return True
1333f085823Sopenharmony_ci        except Exception as error:
1343f085823Sopenharmony_ci            print("Exception: %s" % str(error))
1353f085823Sopenharmony_ci        print("results: failed")
1363f085823Sopenharmony_ci        return False
1373f085823Sopenharmony_ci
1383f085823Sopenharmony_ci    @classmethod
1393f085823Sopenharmony_ci    def execute_command_with_output(cls, command, print_flag=True):
1403f085823Sopenharmony_ci        if print_flag:
1413f085823Sopenharmony_ci            print("command: " + command)
1423f085823Sopenharmony_ci
1433f085823Sopenharmony_ci        proc = subprocess.Popen(command,
1443f085823Sopenharmony_ci                                stdout=subprocess.PIPE,
1453f085823Sopenharmony_ci                                stderr=subprocess.PIPE,
1463f085823Sopenharmony_ci                                shell=True)
1473f085823Sopenharmony_ci
1483f085823Sopenharmony_ci        result = ""
1493f085823Sopenharmony_ci        try:
1503f085823Sopenharmony_ci            data, _ = proc.communicate()
1513f085823Sopenharmony_ci            if isinstance(data, bytes):
1523f085823Sopenharmony_ci                result = data.decode('utf-8', 'ignore')
1533f085823Sopenharmony_ci        finally:
1543f085823Sopenharmony_ci            proc.stdout.close()
1553f085823Sopenharmony_ci            proc.stderr.close()
1563f085823Sopenharmony_ci        return result if result else data
1573f085823Sopenharmony_ci
1583f085823Sopenharmony_ci    @classmethod
1593f085823Sopenharmony_ci    def check_path_legal(cls, path):
1603f085823Sopenharmony_ci        if path and " " in path:
1613f085823Sopenharmony_ci            return "\"%s\"" % path
1623f085823Sopenharmony_ci        return path
1633f085823Sopenharmony_ci
1643f085823Sopenharmony_ci    def remount(self):
1653f085823Sopenharmony_ci        if self.conn_type:
1663f085823Sopenharmony_ci            remount = "target mount"
1673f085823Sopenharmony_ci        else:
1683f085823Sopenharmony_ci            remount = "remount"
1693f085823Sopenharmony_ci        command = "%s %s %s" % (HDC_TOOLS, self.device_params, remount)
1703f085823Sopenharmony_ci        self.execute_command(command)
1713f085823Sopenharmony_ci
1723f085823Sopenharmony_ci    def init_device(self):
1733f085823Sopenharmony_ci        self.remount()
1743f085823Sopenharmony_ci        self.shell('rm -rf %s' % self.test_path)
1753f085823Sopenharmony_ci        self.shell('mkdir -p %s' % self.test_path)
1763f085823Sopenharmony_ci        self.shell('chmod 777 -R %s' % self.test_path)
1773f085823Sopenharmony_ci        self.shell("mount -o rw,remount,rw /%s" % "system")
1783f085823Sopenharmony_ci
1793f085823Sopenharmony_ci    def unlock_screen(self):
1803f085823Sopenharmony_ci        self.shell("svc power stayon true")
1813f085823Sopenharmony_ci
1823f085823Sopenharmony_ci    def unlock_device(self):
1833f085823Sopenharmony_ci        self.shell("input keyevent 82")
1843f085823Sopenharmony_ci        self.shell("wm dismiss-keyguard")
1853f085823Sopenharmony_ci
1863f085823Sopenharmony_ci    def push_file(self, srcpath, despath):
1873f085823Sopenharmony_ci        if self.conn_type:
1883f085823Sopenharmony_ci            push_args = "file send"
1893f085823Sopenharmony_ci        else:
1903f085823Sopenharmony_ci            push_args = "push"
1913f085823Sopenharmony_ci        command = "%s %s %s %s %s" % (
1923f085823Sopenharmony_ci            HDC_TOOLS,
1933f085823Sopenharmony_ci            self.device_params,
1943f085823Sopenharmony_ci            push_args,
1953f085823Sopenharmony_ci            srcpath,
1963f085823Sopenharmony_ci            despath)
1973f085823Sopenharmony_ci        return self.execute_command(command)
1983f085823Sopenharmony_ci
1993f085823Sopenharmony_ci    def pull_file(self, srcpath, despath):
2003f085823Sopenharmony_ci        if self.conn_type:
2013f085823Sopenharmony_ci            pull_args = "file recv"
2023f085823Sopenharmony_ci        else:
2033f085823Sopenharmony_ci            pull_args = "pull"
2043f085823Sopenharmony_ci        command = "%s %s %s %s %s" % (
2053f085823Sopenharmony_ci            HDC_TOOLS,
2063f085823Sopenharmony_ci            self.device_params,
2073f085823Sopenharmony_ci            pull_args,
2083f085823Sopenharmony_ci            srcpath,
2093f085823Sopenharmony_ci            despath)
2103f085823Sopenharmony_ci        return self.execute_command(command)
2113f085823Sopenharmony_ci
2123f085823Sopenharmony_ci    def lock_screen(self):
2133f085823Sopenharmony_ci        self.shell("svc power stayon false")
2143f085823Sopenharmony_ci
2153f085823Sopenharmony_ci    def disable_keyguard(self):
2163f085823Sopenharmony_ci        self.unlock_screen()
2173f085823Sopenharmony_ci        self.unlock_device()
2183f085823Sopenharmony_ci
2193f085823Sopenharmony_ci    def shell(self, command=""):
2203f085823Sopenharmony_ci        return self.execute_command("%s %s shell %s%s%s" % (
2213f085823Sopenharmony_ci            HDC_TOOLS,
2223f085823Sopenharmony_ci            self.device_params,
2233f085823Sopenharmony_ci            QUOTATION_MARKS,
2243f085823Sopenharmony_ci            command,
2253f085823Sopenharmony_ci            QUOTATION_MARKS))
2263f085823Sopenharmony_ci
2273f085823Sopenharmony_ci    def shell_with_output(self, command=""):
2283f085823Sopenharmony_ci        return self.execute_command_with_output("%s %s shell %s%s%s" % (
2293f085823Sopenharmony_ci            HDC_TOOLS,
2303f085823Sopenharmony_ci            self.device_params,
2313f085823Sopenharmony_ci            QUOTATION_MARKS,
2323f085823Sopenharmony_ci            command,
2333f085823Sopenharmony_ci            QUOTATION_MARKS))
2343f085823Sopenharmony_ci