1#!/usr/bin/env python3
2#  Copyright (C) 2023 The Android Open Source Project
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15import sys
16import os
17import subprocess
18import re
19
20from tempfile import NamedTemporaryFile
21
22# example Usage: python3 /sdd/master2/packages/services/Car/tools/GenericCarApiBuilder/scripts
23# /compare_aded_removed_Apis_across_releases.py "/sdd/tm-qpr-dev" "/sdd/master2"
24
25def strip_param_names(api):
26    argGroup = re.search("\((.*)\)",api)
27    if argGroup is None:
28        return api
29    arg = argGroup.group(0)
30    new_arg = re.sub('[^ (]*?(?=\))|[^ ]*?(?=,)', "", arg)
31    return re.sub("\((.*)\)", new_arg, api)
32
33if (len(sys.argv) < 3):
34    print("Need two arguments: <old repo location> <new repo location>")
35    sys.exit(1)
36oldDir = sys.argv[1]
37newDir = sys.argv[2]
38
39
40java_cmd= "java -jar " + newDir + "/packages/services/Car/tools/GenericCarApiBuilder" \
41                                    "/GenericCarApiBuilder.jar --print-hidden-apis " \
42                                    "--root-dir " + oldDir
43
44java_cmd_2= "java -jar " + newDir + "/packages/services/Car/tools/GenericCarApiBuilder" \
45                                    "/GenericCarApiBuilder.jar --print-hidden-apis " \
46                                    "--root-dir " + newDir
47
48java_cmd_3= "java -jar " + newDir + "/packages/services/Car/tools/GenericCarApiBuilder" \
49                                    "/GenericCarApiBuilder.jar --print-hidden-apis-with-constr " \
50                                    "--root-dir " + oldDir
51
52java_cmd_4= "java -jar " + newDir + "/packages/services/Car/tools/GenericCarApiBuilder" \
53                                    "/GenericCarApiBuilder.jar --print-all-apis-with-constr " \
54                                    "--root-dir " + newDir
55
56processes = []
57cmds = [java_cmd, java_cmd_2, java_cmd_3, java_cmd_4]
58for cmd in cmds:
59    f = NamedTemporaryFile()
60    p = subprocess.Popen(cmd, shell=True, stdout=f)
61    processes.append((p, f))
62
63results = []
64for p, f in processes:
65    p.wait()
66    f.seek(0)
67    results.append(f.read().decode('utf-8').strip().split("\n"))
68    f.close()
69
70old_hidden_apis, new_hidden_apis , old_hidden_apis_with_constr, new_hidden_apis_with_constr = results[0], results[1], results[2], results[3]
71old_hidden_apis = [strip_param_names(i) for i in old_hidden_apis]
72new_hidden_apis = [strip_param_names(i) for i in new_hidden_apis]
73old_hidden_apis_with_constr = [strip_param_names(i) for i in old_hidden_apis_with_constr]
74new_hidden_apis_with_constr = [strip_param_names(i) for i in new_hidden_apis_with_constr]
75extra_new_hidden_apis = [i for i in new_hidden_apis if i not in old_hidden_apis]
76
77tracked_hidden_apis = []
78with open( newDir + "/packages/services/Car/tools/GenericCarApiBuilder/scripts/tracked_hidden_apis.txt") as f:
79    tracked_hidden_apis.extend(f.read().splitlines())
80
81print("**********Hidden APIs ADDED************")
82extra_new_hidden_apis = [i for i in extra_new_hidden_apis if i not in tracked_hidden_apis]
83print("\n".join(extra_new_hidden_apis))
84print("\n\n")
85print("**********Hidden APIs REMOVED************")
86removed_hidden_apis = [i for i in old_hidden_apis_with_constr if i not in new_hidden_apis_with_constr]
87print("\n".join(removed_hidden_apis))
88