1#  Copyright (C) 2023 The Android Open Source Project
2#
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.
14
15import re
16
17import soong_lint_fix
18
19CHECK = "AnnotatedAidlCounter"
20LINT_MODULE = "AndroidUtilsLintChecker"
21
22class EnforcePermissionMigratedCounter(soong_lint_fix.SoongLintWrapper):
23    """Wrapper around lint_fix to count the number of AIDL methods annotated."""
24
25    def __init__(self):
26        super().__init__(check=CHECK, lint_module=LINT_MODULE)
27
28    def run(self):
29        self._setup()
30
31        # Analyze the dependencies of the "services" module and the module
32        # "services.core.unboosted".
33        service_module = self._find_module("services")
34        dep_modules = self._find_module_java_deps(service_module) + \
35                      [self._find_module("services.core.unboosted")]
36
37        # Skip dependencies that are not services. Skip the "services.core"
38        # module which is analyzed via "services.core.unboosted".
39        modules = []
40        for module in dep_modules:
41            if "frameworks/base/services" not in module.path:
42                continue
43            if module.name == "services.core":
44                continue
45            modules.append(module)
46
47        self._lint(modules)
48
49        counts = { "unannotated": 0, "enforced": 0, "notRequired": 0 }
50        for module in modules:
51            with open(module.lint_report, "r") as f:
52                content = f.read()
53                keys = dict(re.findall(r'(\w+)=(\d+)', content))
54                for key in keys:
55                    counts[key] += int(keys[key])
56        print(counts)
57        total = sum(counts.values())
58        annotated_percent = (1 - (counts["unannotated"] / total)) * 100
59        print("Annotated methods = %.2f%%" % (annotated_percent))
60
61
62if __name__ == "__main__":
63    EnforcePermissionMigratedCounter().run()
64