1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Action words of Application launch. 18""" 19 20import logging 21import subprocess 22import time 23 24 25class Application(object): 26 @classmethod 27 def stop(cls, bundle_name): 28 stop_cmd = ['hdc', 'shell', 'aa', 'force-stop', bundle_name] 29 logging.info('force stop application: ' + ' '.join(stop_cmd)) 30 stop_result = subprocess.run(stop_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 31 logging.info(stop_result.stdout.strip()) 32 assert stop_result.returncode == 0 33 34 @classmethod 35 def uninstall(cls, bundle_name): 36 uninstall_cmd = ['hdc', 'uninstall', bundle_name] 37 logging.info('uninstall application: ' + ' '.join(uninstall_cmd)) 38 uninstall_result = subprocess.run(uninstall_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 39 logging.info(uninstall_result.stdout.strip()) 40 assert uninstall_result.returncode == 0 41 42 @classmethod 43 def install(cls, hap_path): 44 install_cmd = ['hdc', 'install', '-r', hap_path] 45 logging.info('install application: ' + ' '.join(install_cmd)) 46 install_result = subprocess.run(install_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 47 logging.info(install_result.stdout) 48 assert 'successfully' in install_result.stdout.decode('utf-8') 49 50 @classmethod 51 def start(cls, bundle_name, start_mode=None): 52 start_cmd = (['hdc', 'shell', 'aa', 'start', '-a', 'EntryAbility', '-b', bundle_name] + 53 ([start_mode] if start_mode else [])) 54 logging.info('start application: ' + ' '.join(start_cmd)) 55 start_result = subprocess.run(start_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 56 logging.info(start_result.stdout) 57 assert start_result.stdout.decode('utf-8').strip() == 'start ability successfully.' 58 59 @classmethod 60 def get_pid(cls, bundle_name): 61 ps_cmd = ['hdc', 'shell', 'ps', '-ef'] 62 ps_result = subprocess.run(ps_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 63 ps_result_out = ps_result.stdout.decode('utf-8') 64 for line in ps_result_out.strip().split('\r\n'): 65 if bundle_name in line: 66 logging.info(f'pid of {bundle_name}: ' + line) 67 return line.strip().split()[1] 68 return 0 69 70 @classmethod 71 def launch_application(cls, bundle_name, hap_path, start_mode=None): 72 cls.stop(bundle_name) 73 cls.uninstall(bundle_name) 74 cls.install(hap_path) 75 cls.start(bundle_name, start_mode) 76 time.sleep(3) 77 pid = cls.get_pid(bundle_name) 78 return int(pid) 79