• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
72    def check_if_deps_correctly(self, check_modules, valid_mod_tags, valid_dep_tags, white_lists):
73        # check if mod and callee have wrong innerapi tags
74        passed = True
75        for mod in check_modules:
76            innerapi_tags = mod["innerapi_tags"]
77            if innerapi_tags and all(item in valid_mod_tags for item in innerapi_tags):
78                for dep in mod["deps"]:
79                    callee = dep["callee"]
80
81                    dep_innerapi_tags = callee["innerapi_tags"]
82                    if dep_innerapi_tags and all(item in valid_dep_tags for item in dep_innerapi_tags):
83                        continue
84                    elif not dep_innerapi_tags:
85                        continue
86
87                    if callee["name"] in white_lists:
88                        continue
89
90                    passed = True
91                    wrong_tags = [item for item in dep_innerapi_tags if item not in valid_dep_tags]
92                    self.warn("NEED MODIFY: %s's dep file %s with %s contains wrong dep innerapi_tags [%s] in innerapi_tags [%s]"
93                        %(mod["name"], callee["name"], callee["labelPath"], ",".join(wrong_tags), ",".join(dep_innerapi_tags)))
94            else:
95                wrong_tags = [item for item in innerapi_tags if item not in valid_mod_tags]
96                self.warn("NEED MODIFY: module %s with %s contains wrong mod innerapi_tags [%s] in innerapi_tags [%s]"
97                           %(mod["name"], mod["labelPath"], ",".join(wrong_tags), ",".join(innerapi_tags)))
98                return True
99
100        return passed