1#!/usr/bin/env python3 2#-*- coding: utf-8 -*- 3 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os 18from hypium.action.host import host 19 20 21def pulling_disk_dropping_logs(path, sn): 22 """ 23 @func: Pull and drop logs to disk 24 @param: path: Path of log disk storage 25 @param: sn: device SN 26 """ 27 host.shell(f"hdc -t {sn} file recv data/log/hilog/ {path}") 28 29 30def parse_disk_dropping_logs(path): 31 """ 32 @func: Analyze log storage on disk 33 @param path: log path 34 """ 35 host.shell(f"hilog parse -i {path} -d {path}") 36 37 38def count_keys_disk_dropping_logs(path, keys) -> int: 39 """ 40 @func: count the number of occurrences of shutdown words in the log disk 41 @param path: log download path 42 @param keys: Keywords to be queried 43 @return: The number of times the keyword to be queried appears 44 """ 45 list_count = [0] 46 dirs = os.listdir(path) 47 for file in dirs: 48 if file.endswith(".txt"): 49 with open(f"{path}/{file}", "r", encoding="utf-8", errors="ignore") as read_file: 50 count = read_file.read().count(keys) 51 list_count.append(count) 52 return sum(list_count) 53 54 55def check_disk_dropping_logs(path, keys) -> bool: 56 """ 57 @func: judge whether a certain keyword exists 58 @param path: log path 59 @param keys: Keywords to be queried 60 @return: whether a certain keyword exists 61 """ 62 63 flag = False 64 dirs = os.listdir(path) 65 for file in dirs: 66 if file.endswith(".txt"): 67 with open(f"{path}/{file}", "r", encoding="utf-8", errors="ignore") as read_file: 68 for line in read_file.readlines(): 69 result = line.find(keys) 70 if result != -1: 71 flag = True 72 break 73 if flag: 74 break 75 return flag 76