1#!/usr/bin/env python3 2 3# Copyright (C) 2023 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import sys 18import os 19import subprocess 20import re 21 22from tempfile import NamedTemporaryFile 23from pathlib import Path 24 25# Helper method that strips out the parameter names of methods. This will allow users to change 26# parameter names for hidden apis without mistaking them as having been removed. 27# [^ ]* --> Negation set on SPACE character. This wll match everything until a SPACE. 28# *?(?=\)) --> This means the character ')' will not be included in the match. 29# [^ (]*?(?=\)) --> This will handle the last parameter at the end of a method signature. 30# It excludes matching any '(' characters when there are no parameters, i.e. method(). 31# [^ ]*?(?=,) --> This will handle multiple parameters delimited by commas. 32def strip_param_names(api): 33 # get the arguments first 34 argGroup = re.search("\((.*)\)", api) 35 if argGroup is None: 36 return api 37 arg = argGroup.group(0) 38 new_arg = re.sub('[^ (]*?(?=\))|[^ ]*?(?=,)', "", arg) 39 return re.sub("\((.*)\)", new_arg, api) 40 41rootDir = os.getenv("ANDROID_BUILD_TOP") 42if rootDir is None or rootDir == "": 43 # env variable is not set. Then use the arg passed as Git root 44 rootDir = sys.argv[1] 45 46javaHomeDir = os.getenv("JAVA_HOME") 47if javaHomeDir is None or javaHomeDir == "": 48 if Path(rootDir + '/prebuilts/jdk/jdk17/linux-x86').is_dir(): 49 javaHomeDir = rootDir + "/prebuilts/jdk/jdk17/linux-x86" 50 else: 51 print("$JAVA_HOME is not set. Please use source build/envsetup.sh` in $ANDROID_BUILD_TOP") 52 sys.exit(1) 53 54# Marker is set in GenerateApi.java class and should not be changed. 55marker = "Start-" 56options = ["--print-non-hidden-classes-CSHS", 57 "--print-addedin-without-requires-api-in-CSHS"] 58 59java_cmd = javaHomeDir + "/bin/java -jar " + rootDir + \ 60 "/packages/services/Car/tools/GenericCarApiBuilder" \ 61 "/GenericCarApiBuilder.jar --root-dir " + rootDir + " " + " ".join(options) 62 63all_data = subprocess.check_output(java_cmd, shell=True).decode('utf-8').strip().split("\n") 64all_results = [] 65marker_index = [] 66for i in range(len(all_data)): 67 if all_data[i].replace(marker, "") in options: 68 marker_index.append(i) 69 70previous_mark = 0 71for mark in marker_index: 72 if mark > previous_mark: 73 all_results.append(all_data[previous_mark+1:mark]) 74 previous_mark = mark 75all_results.append(all_data[previous_mark+1:]) 76 77# Update this line when adding more options 78new_class_list = all_results[0] 79incorrect_addedin_api_usage_in_CSHS_errors = all_results[1] 80 81existing_CSHS_classes_path = rootDir + "/frameworks/opt/car/services/builtInServices/tests/" \ 82 "res/raw/CSHS_classes.txt" 83existing_class_list = [] 84with open(existing_CSHS_classes_path) as f: 85 existing_class_list.extend(f.read().splitlines()) 86 87# Find the diff in both class list 88extra_new_classes = [i for i in new_class_list if i not in existing_class_list] 89extra_deleted_classes = [i for i in existing_class_list if i not in new_class_list] 90 91# Print error is there is any class added or removed without changing test 92error = "" 93if len(extra_deleted_classes) > 0: 94 error = error + "Following Classes are deleted \n" + "\n".join(extra_deleted_classes) 95if len(extra_new_classes) > 0: 96 error = error + "\n\nFollowing new classes are added \n" + "\n".join(extra_new_classes) 97 98if error != "": 99 print(error) 100 print("\nRun following command to generate classlist for annotation test") 101 print("cd $ANDROID_BUILD_TOP && m -j GenericCarApiBuilder && GenericCarApiBuilder " 102 "--update-non-hidden-classes-CSHS") 103 print("\nThen run following test to make sure classes are properly annotated") 104 print("atest com.android.server.wm.AnnotationTest") 105 sys.exit(1) 106 107if len(incorrect_addedin_api_usage_in_CSHS_errors) > 0: 108 print("\nFollowing APIs are missing RequiresAPI annotations. See " 109 "go/car-api-version-annotation#using-requiresapi-for-version-check") 110 print("\n".join(incorrect_addedin_api_usage_in_CSHS_errors)) 111 sys.exit(1) 112