1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2023-2024 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import json 21 22 23class BaseRule(object): 24 RULE_NAME = "" 25 26 def __init__(self, mgr, args): 27 self._args = args 28 self._mgr = mgr 29 self._white_lists = self.__load_files__("whitelist.json") 30 31 # To be override 32 def __check__(self): 33 # Default pass 34 return True 35 36 def __load_files__(self, name): 37 rules_dir = [] 38 rules_dir.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../rules")) 39 if self._args and self._args.rules: 40 rules_dir = rules_dir + self._args.rules 41 42 res = [] 43 for d in rules_dir: 44 rules_file = os.path.join(d, self.__class__.RULE_NAME, name) 45 try: 46 with open(rules_file, "r") as f: 47 jsonstr = "".join([line.strip() for line in f if not line.strip().startswith("//")]) 48 res = res + json.loads(jsonstr) 49 except: 50 pass 51 52 return res 53 54 def get_mgr(self): 55 return self._mgr 56 57 def get_white_lists(self): 58 return self._white_lists 59 60 def log(self, info): 61 print(info) 62 63 def warn(self, info): 64 print("\033[35m[WARNING]\x1b[0m: %s" % info) 65 66 def error(self, info): 67 print("\033[91m[NOT ALLOWED]\x1b[0m: %s" % info) 68 69 def get_help_url(self): 70 return "https://gitee.com/openharmony/developtools_integration_verification/tree/master/tools/startup_guard/rules/%s/README.md" % self.__class__.RULE_NAME 71