1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2022 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._mgr = mgr 28 self._args = args 29 self.__white_lists = self.load_files("whitelist.json") 30 31 def load_files(self, name): 32 rules_dir = [] 33 rules_dir.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../rules")) 34 if self._args and self._args.rules: 35 rules_dir = rules_dir + self._args.rules 36 37 res = [] 38 for d in rules_dir: 39 rules_file = os.path.join(d, self.__class__.RULE_NAME, name) 40 try: 41 with open(rules_file, "r") as f: 42 jsonstr = "".join([line.strip() for line in f if not line.strip().startswith("//")]) 43 res = res + json.loads(jsonstr) 44 except: 45 pass 46 47 return res 48 49 def get_mgr(self): 50 return self._mgr 51 52 def get_white_lists(self): 53 return self.__white_lists 54 55 def log(self, info): 56 print(info) 57 58 def warn(self, info): 59 print("\033[35m[WARNING]\x1b[0m: %s" % info) 60 61 def error(self, info): 62 print("\033[91m[NOT ALLOWED]\x1b[0m: %s" % info) 63 64 def get_help_url(self): 65 return "https://gitee.com/openharmony/developtools_integration_verification/tree/master/tools/deps_guard/rules/%s/README.md" % self.__class__.RULE_NAME 66 67 # To be override 68 def check(self): 69 # Default pass 70 return True 71