diff --git a/localCoverage/resident_service/init_gcov.py b/localCoverage/resident_service/init_gcov.py index 10c0c8a627041ee2388b3425c6bf4a13af0f9207..1be7927333a40265732cdf493643c66a7bf83983 100644 --- a/localCoverage/resident_service/init_gcov.py +++ b/localCoverage/resident_service/init_gcov.py @@ -21,7 +21,7 @@ import subprocess import json import sys import time - +import xml.etree.ElementTree as ET from public_method import get_server_dict, get_config_ip, get_sn_list @@ -106,46 +106,145 @@ def modify_faultloggerd_file(developer_path, hdc_str): return -def split_foundation_services(developer_path, system_info_dict, home_path, hdc_str): +def modify_foundation_xml(serv, config_path, origin_xml) -> str: + """ + 修改foundation.xml文件,删去拆分的进程相关 + :param serv: 拆分进程 + :param config_path: 配置文件路径 + :param origin_xml: 原foundation.xml + :return: 修改后foundation.xml路径 + """ + lib_list = foundation_server_lib(serv) + + tree = ET.parse(origin_xml) + root = tree.getroot() + loadlibs = root.find("loadlibs") + + for lib in lib_list: + for sa in root.findall('systemability'): + if lib in sa.find('libpath').text: + root.remove(sa) + for ll in loadlibs.findall('libpath'): + if lib in ll.text: + loadlibs.remove(ll) + + tree.write(os.path.join(config_path, 'foundation.xml'), encoding='utf-8', xml_declaration=True) + return os.path.join(config_path, 'foundation.xml') + + +def create_service_xml(serv, config_path, origin_xml) -> str: + """ + 创建进程xml + :param serv: 进程名 + :param config_path:配置文件所在目录 + :param origin_xml: 原foundation.xml + :return: xml文件路径 + """ + lib_list = foundation_server_lib(serv) + + tree = ET.parse(origin_xml) + root = tree.getroot() + loadlibs = root.find("loadlibs") + + for lib in lib_list: + for sa in root.findall('systemability'): + if lib not in sa.find('libpath').text: + root.remove(sa) + for lp in loadlibs.findall('libpath'): + if lib not in lp.text: + loadlibs.remove(lp) + + tree.write(os.path.join(config_path, '{}.xml'.format(serv)), encoding='utf-8', xml_declaration=True) + return os.path.join(config_path, '{}.xml'.format(serv)) + + +def create_service_cfg(serv, config_path, origin_cfg) -> str: + """ + 创建进程cfg文件 + :param serv: 进程名 + :param config_path:配置文件所在目录 + :param origin_cfg: 原foundation.cfg + :return: cfg文件路径 + """ + with open(origin_cfg, "r") as jf: + json_obj = json.load(jf) + json_obj["jobs"][0]["name"] = "services:{}".format("wms") + + json_obj["services"][0]["name"] = "services:{}".format("wms") + + path_list = json_obj["services"][0]["path"] + path_list.remove("/system/profile/foundation.xml") + path_list.append("/system/profile/{}.xml".format(serv)) + json_obj["services"][0]["path"] = path_list + + json_obj["services"][0]["jobs"]["on-start"] = "services:{}".format("wms") + + cfg_path = os.path.join(config_path, "{}.cfg".format(serv)) + with open(cfg_path, 'w') as r: + json.dump(json_obj, r, indent=4) + return cfg_path + + +def remove_configs(config_path): + """ + 清理配置文件目录下的xml和cfg文件 + :param config_path: 配置文件目录 + :return: + """ + logger("Clear xml and cfg...", "INFO") + + file_list = os.listdir(config_path) + for file in file_list: + if file.endswith(".xml"): + os.remove(os.path.join(config_path, file)) + logger("remove {}".format(os.path.join(config_path, file)), "INFO") + if file.endswith(".cfg"): + os.remove(os.path.join(config_path, file)) + logger("remove {}".format(os.path.join(config_path, file)), "INFO") + + +def split_foundation_services(developer_path, system_info_dict, home_path, hdc_dict): """ foundation.xml、XXX.xml文件推送到 /system/profile XXX.cfg文件推送到/etc/init/ reboot设备,可以将服务从foundation中拆分出来,成为一个独立服务进程 """ - resident_service_path = os.path.join( - developer_path, "localCoverage/resident_service") - config_path = os.path.join(resident_service_path, "config") - restores_path = os.path.join( - resident_service_path, "resources", "restores_environment") - xml_restores_path = os.path.join(restores_path, "foundation.xml") - - if not os.path.exists(xml_restores_path): - print("%s file recv /system/profile/foundation.xml %s" % (hdc_str, restores_path)) - coverage_command("%s file recv /system/profile/foundation.xml %s" % (hdc_str, restores_path)) - - # 推送xml数据 - for key, value_list in system_info_dict.items(): + config_path = os.path.join(developer_path, "localCoverage", "resident_service") + remove_configs(config_path) + + device_ip = hdc_dict["device_ip"] + hdc_port = hdc_dict["device_port"] + device_sn = hdc_dict["device_sn_str"] + + hdc_command(device_ip, hdc_port, device_sn, "file recv /system/profile/foundation.xml {}".format(config_path)) + hdc_command(device_ip, hdc_port, device_sn, "file recv /etc/init/foundation.cfg {}".format(config_path)) + + if os.path.exists(os.path.join(config_path, "foundation.xml")): + origin_xml = os.path.join(config_path, "foundation_origin.xml") + os.rename(os.path.join(config_path, "foundation.xml"), origin_xml) + else: + logger("{} not exist, Cannot modify.".format(os.path.join(config_path, "foundation.xml")), "ERROR") + return + + if os.path.exists(os.path.join(config_path, "foundation.cfg")): + origin_cfg = os.path.join(config_path, "foundation_origin.cfg") + os.rename(os.path.join(config_path, "foundation.cfg"), origin_cfg) + else: + logger("{} not exist, Cannot modify.".format(os.path.join(config_path, "foundation.cfg")), "ERROR") + return + + # 推送配置文件 + for _, value_list in system_info_dict.items(): for process_str in value_list: - foundation_xml_path = os.path.join(config_path, process_str, "foundation.xml") - print("%s shell mount -o rw,remount /" % hdc_str) - coverage_command("%s shell mount -o rw,remount /" % hdc_str) - if os.path.exists(foundation_xml_path): - coverage_command("%s shell rm -rf /lost+found" % hdc_str) - coverage_command("%s shell rm -rf /log" % hdc_str) - coverage_command("%s shell rm -rf %s" % (hdc_str, home_path)) - print("%s file send %s %s" % (hdc_str, foundation_xml_path, "/system/profile/")) - coverage_command("%s file send %s %s" % ( - hdc_str, foundation_xml_path, "/system/profile/")) - - process_xml_path = os.path.join(config_path, process_str, f"{process_str}.xml") - print("%s file send %s %s" % (hdc_str, process_xml_path, "/system/profile/")) - coverage_command("%s file send %s %s" % ( - hdc_str, process_xml_path, "/system/profile/")) - - process_cfg_path = os.path.join(config_path, process_str, f"{process_str}.cfg") - print("%s file send %s %s" % (hdc_str, process_cfg_path, "/etc/init/")) - coverage_command("%s file send %s %s" % ( - hdc_str, process_cfg_path, "/etc/init/")) + foundation_xml = modify_foundation_xml(process_str, config_path, origin_xml) + service_xml = create_service_xml(process_str, config_path, origin_xml) + service_cfg = create_service_cfg(process_str, config_path, origin_cfg) + + hdc_command(device_ip, hdc_port, device_sn, "shell rm -rf {}".format(home_path)) + hdc_command(device_ip, hdc_port, device_sn, "file send {} /system/profile/".format(foundation_xml)) + hdc_command(device_ip, hdc_port, device_sn, "file send {} /system/profile/".format(service_xml)) + hdc_command(device_ip, hdc_port, device_sn, "file send {} /etc/init/".format(service_cfg)) + return @@ -154,13 +253,13 @@ def modify_cfg_xml_file(developer_path, device_ip, device_sn_list, if device_ip and len(device_sn_list) >= 1: for device_sn_str in device_sn_list: hdc_str = "hdc -s %s:%s -t %s" % (device_ip, device_port, device_sn_str) + hdc_dict = {"device_ip": device_ip, "device_port": device_port, "device_sn_str": device_sn_str} modify_init_file(developer_path, hdc_str) modify_faultloggerd_file( developer_path, hdc_str) # 推送服务对应的xml文件 - split_foundation_services( - developer_path, system_info_dict, home_path, hdc_str) - print("%s shell reboot" % hdc_str) + split_foundation_services(developer_path, system_info_dict, home_path, hdc_dict) + logger("{} shell reboot".format(hdc_str), "INFO") coverage_command("%s shell reboot > /dev/null 2>&1" % hdc_str) while True: after_sn_list = get_sn_list("hdc -s %s:%s list targets" % (device_ip, device_port)) @@ -169,7 +268,7 @@ def modify_cfg_xml_file(developer_path, device_ip, device_sn_list, break coverage_command("%s shell getenforce" % hdc_str) else: - print("user_config.xml device ip not config") + logger("user_config.xml device ip not config", "ERROR") if __name__ == '__main__': @@ -177,7 +276,8 @@ if __name__ == '__main__': command_str = command_args.split("command_str=")[1].replace(",", " ") current_path = os.getcwd() _init_sys_config() - from localCoverage.utils import coverage_command + from localCoverage.utils import coverage_command, \ + logger, hdc_command, tree_find_file_endswith, foundation_server_lib root_path = current_path.split("/test/testfwk/developer_test")[0] developer_test_path = os.path.join(root_path, "test/testfwk/developer_test") diff --git a/localCoverage/utils.py b/localCoverage/utils.py index 458cbbb825ab28a952e6a7aa80c1c4ba073fce12..4e9f769fcd9511066ea3c0936d7c0e47e01fe25c 100644 --- a/localCoverage/utils.py +++ b/localCoverage/utils.py @@ -20,7 +20,7 @@ import os import json import subprocess import time -from subprocess import Popen, PIPE, STDOUT +from subprocess import Popen, PIPE, STDOUT, TimeoutExpired def logger(content, level): @@ -71,11 +71,14 @@ def shell_command(command_list: list): :return: """ process = Popen(command_list, stdout=PIPE, stderr=STDOUT) - with process.stdout: - for line in iter(process.stdout.readline, b""): - logger(line.decode().strip(), "INFO") - exitcode = process.wait() - return process, exitcode + try: + outs, errs = process.communicate(timeout=900) + except TimeoutExpired: + process.kill() + outs, errs = process.communicate() + logger(outs.decode("utf-8").strip(), "INFO") + + return errs, process.returncode def hdc_command(device_ip, device_port, device_sn, command): @@ -124,3 +127,25 @@ def tree_find_file_endswith(path, suffix, file_list=None): if os.path.isdir(full_path): tree_find_file_endswith(full_path, suffix, file_list) return file_list +def foundation_server_lib(serv: str) -> list: + """ + foundation拆分的进程和其对应的so之间的对应关系 + :param serv:需要拆分的进程名 + :return: 进程所属的so列表 + """ + lib_dict = { + "ams": ["libabilityms.z.so", "libdataobsms.z.so", "libupms.z.so", "libappms.z.so"], + "bms": ["libbms.z.so"], + "call": ["libtel_call_manager.z.so"], + "dms": ["libdistributed_ability_manager_svr.z.so"], + "fms": ["libfms.z.so"], + "notification": ["libcesfwk_services.z.so", "libans.z.so"], + "power": ["libbatteryservice.z.so", "libdisplaymgrservice.z.so", "libpowermgrservice.z.so", + "libthermalservice.z.so", "libbatterystats_service.z.so"], + "state": ["libtel_state_registry.z.so"], + "wms": ["libwms.z.so"] + } + if lib_dict.get(serv): + return lib_dict[serv] + else: + return []