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