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
15# The following script will parse two source with different car versions and determine if the
16# correct @ApiRequirements or @AddedIn min car version was used.
17# Example Usage:
18# python3 /sdd/master2/packages/services/Car/tools/GenericCarApiBuilder/scripts/check_car_version.py
19# "/sdd/tm-qpr-dev" "/sdd/master" UPSIDE_DOWN_CAKE_0
20# The third argument specifies the expected car version annotation.
21# It will also determine if the minimum car version is missing.
22
23import sys
24import os
25import subprocess
26import re
27
28from tempfile import NamedTemporaryFile
29
30exempt_apis = [
31    "toString",
32    "equals",
33    "hashCode",
34    "finalize"
35]
36
37def strip_param_names(api):
38    argGroup = re.search("\((.*)\)",api)
39    if argGroup is None:
40        return api
41    arg = argGroup.group(0)
42    new_arg = re.sub('[^ (]*?(?=\))|[^ ]*?(?=,)', "", arg)
43    return re.sub("\((.*)\)", new_arg, api)
44
45# The min car version is separated by a `|` in the string.
46def get_version(api):
47    return version in api.split("|")[1]
48
49if (len(sys.argv) < 4):
50    print("Need three arguments: <old repo location> <new repo location> <car version>")
51    sys.exit(1)
52oldDir = sys.argv[1]
53newDir = sys.argv[2]
54version = sys.argv[3]
55
56
57java_cmd= "java -jar " + newDir + "/packages/services/Car/tools/GenericCarApiBuilder" \
58                                  "/GenericCarApiBuilder.jar " \
59                                  "--print-all-apis-with-car-version " \
60                                  "--root-dir " + oldDir
61
62java_cmd_2= "java -jar " + newDir + "/packages/services/Car/tools/GenericCarApiBuilder" \
63                                    "/GenericCarApiBuilder.jar " \
64                                    "--print-all-apis-with-car-version " \
65                                    "--root-dir " + newDir
66
67processes = []
68cmds = [java_cmd, java_cmd_2]
69for cmd in cmds:
70    f = NamedTemporaryFile()
71    p = subprocess.Popen(cmd, shell=True, stdout=f)
72    processes.append((p, f))
73
74results = []
75for p, f in processes:
76    p.wait()
77    f.seek(0)
78    results.append(f.read().decode('utf-8').strip().split("\n"))
79    f.close()
80
81old_apis, new_apis = results[0], results[1]
82old_apis = [strip_param_names(i) for i in old_apis]
83new_apis = [strip_param_names(i) for i in new_apis]
84
85print("**********THE FOLLOWING APIS DO NOT HAVE " + version + " VERSION ANNOTATION************")
86new_apis = [i for i in new_apis if i not in old_apis and not get_version(i) and not any(exempt in i for exempt in exempt_apis)]
87print("\n".join(new_apis))
88print("\n\n")
89