|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024 Google LLC |
| 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. |
| 15 | + |
| 16 | +from pathlib import Path |
| 17 | +import re |
| 18 | + |
| 19 | +proto_library_pattern = r""" |
| 20 | +proto_library_with_info\( |
| 21 | +(.*?) |
| 22 | +\) |
| 23 | +""" |
| 24 | +gapic_pattern = r""" |
| 25 | +java_gapic_library\( |
| 26 | +(.*?) |
| 27 | +\) |
| 28 | +""" |
| 29 | +assembly_pattern = r""" |
| 30 | +java_gapic_assembly_gradle_pkg\( |
| 31 | +(.*?) |
| 32 | +\) |
| 33 | +""" |
| 34 | +resource_pattern = r"//google/cloud:common_resources_proto" |
| 35 | +location_pattern = r"//google/cloud/location:location_proto" |
| 36 | +iam_pattern = r"//google/iam/v1:iam_policy_proto" |
| 37 | +transport_pattern = r"transport = \"(.*?)\"" |
| 38 | +rest_pattern = r"rest_numeric_enums = True" |
| 39 | +gapic_yaml_pattern = r"gapic_yaml = \"(.*?)\"" |
| 40 | +service_config_pattern = r"grpc_service_config = \"(.*?)\"" |
| 41 | +service_yaml_pattern = r"service_yaml = \"(.*?)\"" |
| 42 | +include_samples_pattern = r"include_samples = True" |
| 43 | + |
| 44 | + |
| 45 | +class ClientInput: |
| 46 | + """ |
| 47 | + A data class containing inputs to invoke generate_library.sh to generate |
| 48 | + a GAPIC library. |
| 49 | + """ |
| 50 | + def __init__( |
| 51 | + self, |
| 52 | + proto_only="true", |
| 53 | + additional_protos="google/cloud/common_resources.proto", |
| 54 | + transport="", |
| 55 | + rest_numeric_enum="", |
| 56 | + gapic_yaml="", |
| 57 | + service_config="", |
| 58 | + service_yaml="", |
| 59 | + include_samples="true", |
| 60 | + ): |
| 61 | + self.proto_only = proto_only |
| 62 | + self.additional_protos = additional_protos |
| 63 | + self.transport = transport |
| 64 | + self.rest_numeric_enum = rest_numeric_enum |
| 65 | + self.gapic_yaml = gapic_yaml |
| 66 | + self.service_config = service_config |
| 67 | + self.service_yaml = service_yaml |
| 68 | + self.include_samples = include_samples |
| 69 | + |
| 70 | + |
| 71 | +def parse( |
| 72 | + build_path: Path, |
| 73 | + versioned_path: str, |
| 74 | +) -> ClientInput: |
| 75 | + """ |
| 76 | + Utility function to parse inputs of generate_library.sh from BUILD.bazel. |
| 77 | + :param build_path: the file path of BUILD.bazel |
| 78 | + :param versioned_path: a versioned path in googleapis repository, e.g., |
| 79 | + google/cloud/asset/v1. |
| 80 | + :return: an ClientInput object. |
| 81 | + """ |
| 82 | + with open(f"{build_path}/BUILD.bazel") as build: |
| 83 | + content = build.read() |
| 84 | + |
| 85 | + proto_library_target = re.compile( |
| 86 | + proto_library_pattern, re.DOTALL | re.VERBOSE |
| 87 | + ).findall(content)[0] |
| 88 | + additional_protos = __parse_additional_protos(proto_library_target) |
| 89 | + gapic_target = re.compile(gapic_pattern, re.DOTALL | re.VERBOSE)\ |
| 90 | + .findall(content) |
| 91 | + assembly_target = re.compile(assembly_pattern, re.DOTALL | re.VERBOSE)\ |
| 92 | + .findall(content) |
| 93 | + include_samples = __parse_include_samples(assembly_target[0]) |
| 94 | + if len(gapic_target) == 0: |
| 95 | + return ClientInput( |
| 96 | + include_samples=include_samples |
| 97 | + ) |
| 98 | + |
| 99 | + transport = __parse_transport(gapic_target[0]) |
| 100 | + rest_numeric_enum = __parse_rest_numeric_enums(gapic_target[0]) |
| 101 | + gapic_yaml = __parse_gapic_yaml(gapic_target[0], versioned_path) |
| 102 | + service_config = __parse_service_config(gapic_target[0], versioned_path) |
| 103 | + service_yaml = __parse_service_yaml(gapic_target[0], versioned_path) |
| 104 | + |
| 105 | + return ClientInput( |
| 106 | + proto_only="false", |
| 107 | + additional_protos=additional_protos, |
| 108 | + transport=transport, |
| 109 | + rest_numeric_enum=rest_numeric_enum, |
| 110 | + gapic_yaml=gapic_yaml, |
| 111 | + service_config=service_config, |
| 112 | + service_yaml=service_yaml, |
| 113 | + include_samples=include_samples, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +def __parse_additional_protos(proto_library_target: str) -> str: |
| 118 | + res = [" "] |
| 119 | + if len(re.findall(resource_pattern, proto_library_target)) != 0: |
| 120 | + res.append("google/cloud/common_resources.proto") |
| 121 | + if len(re.findall(location_pattern, proto_library_target)) != 0: |
| 122 | + res.append("google/cloud/location/locations.proto") |
| 123 | + if len(re.findall(iam_pattern, proto_library_target)) != 0: |
| 124 | + res.append("google/iam/v1/iam_policy.proto") |
| 125 | + return " ".join(res) |
| 126 | + |
| 127 | + |
| 128 | +def __parse_transport(gapic_target: str) -> str: |
| 129 | + transport = re.findall(transport_pattern, gapic_target) |
| 130 | + return transport[0] if len(transport) != 0 else "grpc" |
| 131 | + |
| 132 | + |
| 133 | +def __parse_rest_numeric_enums(gapic_target: str) -> str: |
| 134 | + rest_numeric_enums = re.findall(rest_pattern, gapic_target) |
| 135 | + return "true" if len(rest_numeric_enums) != 0 else "false" |
| 136 | + |
| 137 | + |
| 138 | +def __parse_gapic_yaml(gapic_target: str, versioned_path: str) -> str: |
| 139 | + gapic_yaml = re.findall(gapic_yaml_pattern, gapic_target) |
| 140 | + return f"{versioned_path}/{gapic_yaml[0]}" if len(gapic_yaml) != 0 else "" |
| 141 | + |
| 142 | + |
| 143 | +def __parse_service_config(gapic_target: str, versioned_path: str) -> str: |
| 144 | + service_config = re.findall(service_config_pattern, gapic_target) |
| 145 | + return f"{versioned_path}/{service_config[0]}" if len(service_config) != 0 \ |
| 146 | + else "" |
| 147 | + |
| 148 | + |
| 149 | +def __parse_service_yaml(gapic_target: str, versioned_path: str) -> str: |
| 150 | + service_yaml = re.findall(service_yaml_pattern, gapic_target) |
| 151 | + return f"{versioned_path}/{service_yaml[0]}" if len(service_yaml) != 0 \ |
| 152 | + else "" |
| 153 | + |
| 154 | + |
| 155 | +def __parse_include_samples(assembly_target: str) -> str: |
| 156 | + include_samples = re.findall(include_samples_pattern, assembly_target) |
| 157 | + return "true" if len(include_samples) != 0 else "false" |
0 commit comments