1# -*- coding: utf-8 -*- 2# Copyright (c) 2023 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14import enum 15import logging 16import os 17import sys 18from subprocess import Popen, PIPE, STDOUT 19 20# sys.path.append(os.path.dirname(os.path.realpath(__file__)) + os.sep) 21from config import * 22 23log_tag = 'utils' 24 25 26class AclCheckException(Exception): 27 def __init__(self, msg): 28 self.msg = msg 29 30 31def timestamp(): 32 return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 33 34 35class LogLevel(enum.Enum): 36 Error = 1 37 Info = 2 38 39 40logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s %(message)s', 41 datefmt='%Y-%m-%d %H:%M:%S %a') 42 43 44def log(msg): 45 logging.error(msg) 46 47 48def set_log_content(level, tag, msg): 49 log_content = timestamp() + ' {}'.format(level) + ' [{}]'.format(tag) + ' {}'.format(msg) 50 print(log_content) 51 log(log_content) 52 return (log_content) 53 54 55def shell_command(command_list: list): 56 try: 57 print(command_list) 58 process = Popen(command_list, stdout=PIPE, stderr=STDOUT) 59 exitcode = process.wait() 60 set_log_content(LogLevel(2).name, log_tag, '{} operation fuccessful!'.format(command_list)) 61 return process, exitcode 62 except Exception as e: 63 set_log_content(LogLevel(1).name, log_tag, e.msg) 64 raise AclCheckException(e.msg) 65 66 67def hdc_command(command): 68 print(command) 69 command_list = command.split(' ') 70 _, exitcode = shell_command(command_list) 71 return exitcode 72