1#!/usr/bin/python
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#
17"""A script to check whether the generated prop config needs to be updated.
18
19   This script will check whether the current generated prop config is
20   consistent with VehiclePropertyIds.java if it is updated. If so, it will
21   prompt user to generate a new version of prop config file.
22
23   Usage:
24
25   $ python verify_generated_prop_config_hook.py --android_build_top
26   [ANDROID_BUILD_TOP] --preupload_files [MODIFIED_FILES]
27"""
28import argparse
29import filecmp
30import os
31import sys
32import subprocess
33import tempfile
34
35VEHICLE_PROPERTY_IDS_PARSER = ('packages/services/Car/tools/'
36        'vehiclepropertyidsparser/prebuilt/VehiclePropertyIdsParser.jar')
37CAR_LIB = 'packages/services/Car/car-lib/src'
38CAR_SVC_PROPS = ('packages/services/Car/car-lib/generated-prop-config'
39        '/CarSvcProps.json')
40JDK_FOLDER = '/prebuilts/jdk/jdk17/linux-x86'
41
42
43def createTempFile():
44    f = tempfile.NamedTemporaryFile(delete=False);
45    f.close();
46    return f.name
47
48
49def main():
50    parser = argparse.ArgumentParser(
51            description=('Check whether the generated prop config needs to be '
52                    'updated based on VehiclePropertyIds.java'))
53    parser.add_argument('--android_build_top', required=True,
54            help='Path to ANDROID_BUILD_TOP')
55    parser.add_argument('--preupload_files', required=True, nargs='*',
56            help='modified files')
57    args = parser.parse_args()
58
59    needUpdate = False
60    for preupload_file in args.preupload_files:
61        if preupload_file.endswith('VehiclePropertyIds.java'):
62            needUpdate = True
63            break
64        if preupload_file.endswith('VehiclePropertyIdsParser.jar'):
65            # If parser is updated, the generated file needs to be updated.
66            needUpdate = True
67            break
68
69    if not needUpdate:
70        return
71
72    vehiclePropertyIdsParser = os.path.join(args.android_build_top,
73            VEHICLE_PROPERTY_IDS_PARSER)
74    carLib = os.path.join(args.android_build_top, CAR_LIB)
75    javaHomeDir = os.getenv("JAVA_HOME")
76    if javaHomeDir is None or javaHomeDir == "":
77        if os.path.isdir(args.android_build_top + JDK_FOLDER):
78            javaHomeDir = args.android_build_top + JDK_FOLDER
79        else:
80            print('$JAVA_HOME is not set. Please use source build/envsetup.sh` in '
81                    '$ANDROID_BUILD_TOP')
82            sys.exit(1)
83
84    try:
85        tempCarSvcProps = createTempFile()
86        javaBin = os.path.join(javaHomeDir, '/bin/java');
87        subprocess.check_call([javaBin, '-jar', vehiclePropertyIdsParser,
88                carLib, tempCarSvcProps])
89        carSvcProps = os.path.join(args.android_build_top, CAR_SVC_PROPS);
90        if not filecmp.cmp(tempCarSvcProps, carSvcProps):
91            print('The generated CarSvcProps.json requires update because '
92                    'VehiclePropertyIds.java is updated')
93            cmd = ' '.join([javaBin, '-jar', vehiclePropertyIdsParser, carLib,
94                    carSvcProps])
95            print('Run \n\n' + cmd + '\n\nto update')
96            sys.exit(1)
97    finally:
98        os.remove(tempCarSvcProps)
99
100
101if __name__ == '__main__':
102    main()
103