Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2027c13e authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Updated translate_aidl_enums.py with complete translation logic" into main

parents 6d314919 4d9c95f2
Loading
Loading
Loading
Loading
+93 −29
Original line number Diff line number Diff line
@@ -21,14 +21,16 @@
   ENUM_NAMETest.java files in cts/tests/tests/car/src/android/car/cts and
   packages/services/Car/tests/android_car_api_test/src/android/car/apitest

   Also needs a flag name e.g. FLAG_ANDROID_VIC_VEHICLE_PROPERTIES

   Usage:
   $ python translate_aidl_enums.py ENUM_NAME.aidl
   $ python translate_aidl_enums.py ENUM_NAME.aidl FLAG_NAME
"""
import os
import sys

LICENSE = """/*
 * Copyright (C) 2023 The Android Open Source Project
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -45,15 +47,20 @@ LICENSE = """/*
"""

class EnumParser:
    def __init__(self, file_path, file_name):
    def __init__(self, file_path, file_name, flag_name):
        self.filePath = file_path
        self.fileName = file_name
        self.flagName = flag_name
        self.lowerFileName = self.fileName[0].lower() + self.fileName[1:]
        self.enumNames = []
        self.enums = []
        self.outputMsg = []
        self.outputMsg.append(LICENSE)
        self.outputMsg.append("\npackage android.car.hardware.property;\n")
        self.outputMsg.append("""
import static android.car.feature.Flags.{};

import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;

@@ -61,26 +68,61 @@ import com.android.car.internal.util.ConstantDebugUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
""")
""".format(self.flagName))

        comment_block = []
        in_comment = False

        with open(self.filePath, 'r') as f:
            for line in f.readlines()[16:]:
                if line in ["package android.hardware.automotive.vehicle;\n",
                            "@VintfStability\n",
                            '@Backing(type="int")\n']:
            lines = f.readlines()
            for line in lines:
                line = line.rstrip('\n')
                if line.strip() in ["package android.hardware.automotive.vehicle;",
                                    "@VintfStability",
                                    '@Backing(type="int")']:
                    continue

                msg = line
                if line.strip().startswith('/**') or line.strip().startswith('/*'):
                    in_comment = True
                    comment_block.append(line + '\n')
                    continue
                elif in_comment:
                    comment_block.append(line + '\n')
                    if line.strip().endswith('*/'):
                        in_comment = False
                    continue
                elif line.strip().startswith('*'):
                    comment_block.append(line + '\n')
                    continue

                msg = line + '\n'
                msgSplit = msg.strip().split()
                if len(msgSplit) > 0 and msgSplit[0] == "enum":
                    if comment_block:
                        self.outputMsg.extend(comment_block)
                        comment_block = []
                    self.outputMsg.append("@FlaggedApi({})\n".format(self.flagName))
                    msgSplit[0] = "public final class"
                    msg = " ".join(msgSplit) + "\n"
                    self.outputMsg.append(msg)
                elif len(msgSplit) > 1 and msgSplit[1] == '=':
                    if comment_block:
                        indented_comment_block = [line for line in comment_block]
                        self.outputMsg.extend(indented_comment_block)
                        comment_block = []
                    msgSplit.insert(0, "    public static final int")
                    self.enums.append(msgSplit[1])
                    msgSplit[-1] = msgSplit[-1][:-1] + ";\n"
                    msg = " ".join(msgSplit)
                elif msg == "}\n":
                    enum_name = msgSplit[1].strip()
                    self.enumNames.append(enum_name)
                    enum = msgSplit[3].strip(",")
                    self.enums.append(enum)
                    if msgSplit[-1].endswith(','):
                        msgSplit[-1] = msgSplit[-1][:-1] + ";"
                    msg = " ".join(msgSplit) + "\n"
                    self.outputMsg.append(msg)
                elif line.strip() == '}':
                    if comment_block:
                        self.outputMsg.extend(comment_block)
                        comment_block = []
                    self.outputMsg.append("""
    private {2}() {{}}

@@ -101,17 +143,23 @@ import java.lang.annotation.RetentionPolicy;
    @Retention(RetentionPolicy.SOURCE)
    public @interface {2}Int {{}}\n""".format(self.lowerFileName, "{" + ", ".join(self.enums) + "}",
                                              self.fileName))
                self.outputMsg.append(msg)
        self.outputMsg.append("TODO: delete this line and manually update this file with app-facing documentation and necessary tags.\n")
        self.outputMsg.append("}")

        self.outputMsgApiTest = []
        self.outputMsgApiTest.append(LICENSE)
        self.outputMsgApiTest.append("""package android.car.apitest;

import static android.car.feature.Flags.{1};

import static com.google.common.truth.Truth.assertWithMessage;

import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;

import androidx.test.filters.SmallTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -122,6 +170,8 @@ import java.util.Collection;
@SmallTest
@RunWith(Parameterized.class)
public class {0}Test {{
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
    private final int mJavaConstantValue;
    private final int mHalConstantValue;

@@ -133,56 +183,68 @@ public class {0}Test {{
    @Parameterized.Parameters
    public static Collection constantValues() {{
        return Arrays.asList(
                new Object[][] {{""".format(self.fileName))
        for enum in self.enums:
                new Object[][] {{""".format(self.fileName, self.flagName))
        for enum in self.enumNames:
            self.outputMsgApiTest.append("""
                        {{
                                android.car.hardware.property.{0}.{1},
                                android.hardware.automotive.vehicle.{0}.{1}
                        }},""".format(self.fileName, enum))
        self.outputMsgApiTest.append("""
                });
    }
                }});
    }}

    @Test
    public void testMatchWithVehicleHal() {
    @RequiresFlagsEnabled({})
    public void testMatchWithVehicleHal() {{
        assertWithMessage("Java constant")
                .that(mJavaConstantValue)
                .isEqualTo(mHalConstantValue);
    }
}
""")
    }}
}}
""".format(self.flagName))

        self.outputMsgCtsTest = []
        self.outputMsgCtsTest.append(LICENSE)
        self.outputMsgCtsTest.append("""
package android.car.cts;

import static android.car.feature.Flags.{1};

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import android.car.cts.utils.VehiclePropertyUtils;
import android.car.hardware.property.{0};
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;

import org.junit.Rule;
import org.junit.Test;

import java.util.List;

public class {0}Test {{
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    @Test
    public void testToString() {{""".format(self.fileName))
        for enum in self.enums:
    @RequiresFlagsEnabled({1})
    public void testToString() {{""".format(self.fileName, self.flagName))
        for enum in self.enumNames:
            self.outputMsgCtsTest.append("""
        assertThat({0}.toString(
                {0}.{1}))
                .isEqualTo("{1}");""".format(self.fileName, enum))
        max_enum_value = len(self.enums)
        self.outputMsgCtsTest.append("""
        assertThat({0}.toString({1})).isEqualTo("{2}");
        assertThat({0}.toString(12)).isEqualTo("0xc");
    }}

    @Test
    @RequiresFlagsEnabled({4})
    public void testAll{0}sAreMappedInToString() {{
        List<Integer> {3}s =
                VehiclePropertyUtils.getIntegersFromDataEnums({0}.class);
@@ -194,11 +256,11 @@ public class {0}Test {{
        }}
    }}
}}
""".format(self.fileName, len(self.enums), hex(len(self.enums)), self.lowerFileName))
""".format(self.fileName, len(self.enums), hex(len(self.enums)), self.lowerFileName, self.flagName))

def main():
    if len(sys.argv) != 2:
        print("Usage: {} enum_aidl_file".format(sys.argv[0]))
    if len(sys.argv) != 3:
        print("Usage: {} enum_aidl_file ALL_CAPS_FLAG_NAME".format(sys.argv[0]))
        sys.exit(1)
    print("WARNING: This file only generates the base enum values in the framework layer. The "
          + "generated files must be reviewed by you and edited if any additional changes are "
@@ -207,12 +269,14 @@ def main():
          + "the new property is system API")
    file_path = sys.argv[1]
    file_name = file_path.split('/')[-1][:-5]
    parser = EnumParser(file_path, file_name)
    flag_name = sys.argv[2]
    parser = EnumParser(file_path, file_name, flag_name)

    android_top = os.environ['ANDROID_BUILD_TOP']
    if not android_top:
        print('ANDROID_BUILD_TOP is not in environmental variable, please run source and lunch '
              + 'at the android root')
        sys.exit(1)

    with open(android_top + "/packages/services/Car/car-lib/src/android/car/hardware/property/"
              + file_name + ".java", 'w') as f: