1#!/usr/bin/env python3
2#  Copyright (C) 2022 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
21from pathlib import Path
22
23# Helper method that strips out the parameter names of methods. This will allow users to change
24# parameter names for hidden apis without mistaking them as having been removed.
25# [^ ]* --> Negation set on SPACE character. This wll match everything until a SPACE.
26# *?(?=\)) --> This means the character ')' will not be included in the match.
27# [^ (]*?(?=\)) --> This will handle the last parameter at the end of a method signature.
28# It excludes matching any '(' characters when there are no parameters, i.e. method().
29# [^ ]*?(?=,) --> This will handle multiple parameters delimited by commas.
30def strip_param_names(api):
31    # get the arguments first
32    argGroup = re.search("\((.*)\)",api)
33    if argGroup is None:
34        return api
35    arg = argGroup.group(0)
36    new_arg = re.sub('[^ (]*?(?=\))|[^ ]*?(?=,)', "", arg)
37    return re.sub("\((.*)\)", new_arg, api)
38
39
40rootDir = os.getenv("ANDROID_BUILD_TOP")
41if rootDir is None or rootDir == "":
42    # env variable is not set. Then use the arg passed as Git root
43    rootDir = sys.argv[1]
44
45javaHomeDir = os.getenv("JAVA_HOME")
46if javaHomeDir is None or javaHomeDir == "":
47    if Path(rootDir + '/prebuilts/jdk/jdk17/linux-x86').is_dir():
48        javaHomeDir = rootDir + "/prebuilts/jdk/jdk17/linux-x86"
49    else:
50        print("$JAVA_HOME is not set. Please use source build/envsetup.sh` in $ANDROID_BUILD_TOP")
51        sys.exit(1)
52
53options = ["--print-hidden-apis"]
54
55java_cmd = javaHomeDir + "/bin/java -jar " + rootDir + \
56           "/packages/services/Car/tools/GenericCarApiBuilder" \
57           "/GenericCarApiBuilder.jar --root-dir " + rootDir + " " + " ".join(options)
58
59new_hidden_apis = subprocess.check_output(java_cmd, shell=True).decode('utf-8').strip().split("\n")
60
61# read existing hidden APIs
62existing_hidden_apis_path = rootDir + "/packages/services/Car/tests/carservice_unit_test/res/raw" \
63                                      "/car_hidden_apis.txt"
64
65existing_hidden_apis = []
66with open(existing_hidden_apis_path) as f:
67    existing_hidden_apis = f.read().splitlines()
68
69modified_hidden_api = set(new_hidden_apis)^set(existing_hidden_apis)
70
71if len(modified_hidden_api) > 0:
72    print("\nFollowing Hidden APIs are modified in this CL:")
73    print("\n".join(modified_hidden_api))
74    print("\n")
75    print("\nPlease run following command to update hidden API list:")
76    print("\ncd $ANDROID_BUILD_TOP && m -j GenericCarApiBuilder && GenericCarApiBuilder "
77          "--update-hidden-apis")
78    print("\n")
79    sys.exit(1)