15ba71b47Sopenharmony_ci#!/usr/bin/env python3 25ba71b47Sopenharmony_ci#-*- coding: utf-8 -*- 35ba71b47Sopenharmony_ci 45ba71b47Sopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd. 55ba71b47Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 65ba71b47Sopenharmony_ci# you may not use this file except in compliance with the License. 75ba71b47Sopenharmony_ci# You may obtain a copy of the License at 85ba71b47Sopenharmony_ci# 95ba71b47Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 105ba71b47Sopenharmony_ci# 115ba71b47Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 125ba71b47Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 135ba71b47Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 145ba71b47Sopenharmony_ci# See the License for the specific language governing permissions and 155ba71b47Sopenharmony_ci# limitations under the License. 165ba71b47Sopenharmony_ci 175ba71b47Sopenharmony_ciimport os 185ba71b47Sopenharmony_cifrom hypium.action.host import host 195ba71b47Sopenharmony_ci 205ba71b47Sopenharmony_ci 215ba71b47Sopenharmony_cidef pulling_disk_dropping_logs(path, sn): 225ba71b47Sopenharmony_ci """ 235ba71b47Sopenharmony_ci @func: Pull and drop logs to disk 245ba71b47Sopenharmony_ci @param: path: Path of log disk storage 255ba71b47Sopenharmony_ci @param: sn: device SN 265ba71b47Sopenharmony_ci """ 275ba71b47Sopenharmony_ci host.shell(f"hdc -t {sn} file recv data/log/hilog/ {path}") 285ba71b47Sopenharmony_ci 295ba71b47Sopenharmony_ci 305ba71b47Sopenharmony_cidef parse_disk_dropping_logs(path): 315ba71b47Sopenharmony_ci """ 325ba71b47Sopenharmony_ci @func: Analyze log storage on disk 335ba71b47Sopenharmony_ci @param path: log path 345ba71b47Sopenharmony_ci """ 355ba71b47Sopenharmony_ci host.shell(f"hilog parse -i {path} -d {path}") 365ba71b47Sopenharmony_ci 375ba71b47Sopenharmony_ci 385ba71b47Sopenharmony_cidef count_keys_disk_dropping_logs(path, keys) -> int: 395ba71b47Sopenharmony_ci """ 405ba71b47Sopenharmony_ci @func: count the number of occurrences of shutdown words in the log disk 415ba71b47Sopenharmony_ci @param path: log download path 425ba71b47Sopenharmony_ci @param keys: Keywords to be queried 435ba71b47Sopenharmony_ci @return: The number of times the keyword to be queried appears 445ba71b47Sopenharmony_ci """ 455ba71b47Sopenharmony_ci list_count = [0] 465ba71b47Sopenharmony_ci dirs = os.listdir(path) 475ba71b47Sopenharmony_ci for file in dirs: 485ba71b47Sopenharmony_ci if file.endswith(".txt"): 495ba71b47Sopenharmony_ci with open(f"{path}/{file}", "r", encoding="utf-8", errors="ignore") as read_file: 505ba71b47Sopenharmony_ci count = read_file.read().count(keys) 515ba71b47Sopenharmony_ci list_count.append(count) 525ba71b47Sopenharmony_ci return sum(list_count) 535ba71b47Sopenharmony_ci 545ba71b47Sopenharmony_ci 555ba71b47Sopenharmony_cidef check_disk_dropping_logs(path, keys) -> bool: 565ba71b47Sopenharmony_ci """ 575ba71b47Sopenharmony_ci @func: judge whether a certain keyword exists 585ba71b47Sopenharmony_ci @param path: log path 595ba71b47Sopenharmony_ci @param keys: Keywords to be queried 605ba71b47Sopenharmony_ci @return: whether a certain keyword exists 615ba71b47Sopenharmony_ci """ 625ba71b47Sopenharmony_ci 635ba71b47Sopenharmony_ci flag = False 645ba71b47Sopenharmony_ci dirs = os.listdir(path) 655ba71b47Sopenharmony_ci for file in dirs: 665ba71b47Sopenharmony_ci if file.endswith(".txt"): 675ba71b47Sopenharmony_ci with open(f"{path}/{file}", "r", encoding="utf-8", errors="ignore") as read_file: 685ba71b47Sopenharmony_ci for line in read_file.readlines(): 695ba71b47Sopenharmony_ci result = line.find(keys) 705ba71b47Sopenharmony_ci if result != -1: 715ba71b47Sopenharmony_ci flag = True 725ba71b47Sopenharmony_ci break 735ba71b47Sopenharmony_ci if flag: 745ba71b47Sopenharmony_ci break 755ba71b47Sopenharmony_ci return flag 76