Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions .generator/parse_googleapis_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import starlark as sl
Comment thread
parthea marked this conversation as resolved.


_PY_CALLABLES = (
"py_gapic_assembly_pkg",
"py_gapic_library",
"py_test",
"py_proto_library",
"py_grpc_library",
"py_import",
)

_JAVA_CALLABLES = (
"java_gapic_assembly_gradle_pkg",
"java_gapic_library",
"java_gapic_test",
"java_grpc_library",
"java_proto_library",
)

_GO_CALLABLES = (
"go_gapic_assembly_pkg",
"go_gapic_library",
"go_proto_library",
"go_grpc_library",
)

_PHP_CALLABLES = (
"php_gapic_assembly_pkg",
"php_gapic_library",
"php_grpc_library",
"php_proto_library",
)

_NODEJS_CALLABLES = ("nodejs_gapic_assembly_pkg", "nodejs_gapic_library")

_RUBY_CALLABLES = (
"ruby_ads_gapic_library",
"ruby_cloud_gapic_library",
"ruby_gapic_assembly_pkg",
"ruby_grpc_library",
"ruby_proto_library",
)

_CSHARP_CALLABLES = (
"csharp_gapic_assembly_pkg",
"csharp_gapic_library",
"csharp_grpc_library",
"csharp_proto_library",
)

_CC_CALLABLES = ("cc_grpc_library", "cc_gapic_library", "cc_proto_library")

_MISC_CALLABLES = (
"moved_proto_library",
"proto_library",
"proto_library_with_info",
"upb_c_proto_library",
)

_CALLABLE_MAP = {
"@rules_proto//proto:defs.bzl": ("proto_library",),
"@com_google_googleapis_imports//:imports.bzl": (
_PY_CALLABLES
+ _JAVA_CALLABLES
+ _GO_CALLABLES
+ _PHP_CALLABLES
+ _NODEJS_CALLABLES
+ _RUBY_CALLABLES
+ _CSHARP_CALLABLES
+ _CC_CALLABLES
+ _MISC_CALLABLES
),
}

_NOOP_CALLABLES = (
"package",
"alias",
"py_test",
"sh_binary",
"proto_library",
"java_proto_library",
"genrule",
)


def parse_content(content: str) -> dict:
"""Parses content from BUILD.bazel and returns a dictionary
containing bazel rules and arguments.

Args:
content(str): contents of a BUILD.bazel.

Returns: Dictionary containing bazel rules and arguments.

"""
glb = sl.Globals.standard()
mod = sl.Module()
packages = {}

def bazel_target(**args):
if args["name"] is not None:
packages[args["name"]] = args

def noop_bazel_rule(**args):
pass

for noop_callable in _NOOP_CALLABLES:
mod.add_callable(noop_callable, noop_bazel_rule)

def load(name):
mod = sl.Module()
for callable_name in _CALLABLE_MAP.get(name, []):
mod.add_callable(callable_name, bazel_target)
Comment thread
parthea marked this conversation as resolved.
return mod.freeze()

ast = sl.parse("BUILD.bazel", content)

sl.eval(mod, ast, glb, sl.FileLoader(load))

return packages
1 change: 1 addition & 0 deletions .generator/requirements-test.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pytest
pytest-cov
pytest-mock
gcp-synthtool @ git+https://github.com/googleapis/synthtool@5aa438a342707842d11fbbb302c6277fbf9e4655
starlark-pyo3>=2025.1
35 changes: 35 additions & 0 deletions .generator/test-resources/librarian/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

package(default_visibility = ["//visibility:public"])
load("@rules_proto//proto:defs.bzl", "proto_library")
load(
"@com_google_googleapis_imports//:imports.bzl",
"py_gapic_library",
)

proto_library(
name = "language_proto",
)

py_gapic_library(
name = "language_py_gapic",
srcs = [":language_proto"],
grpc_service_config = "language_grpc_service_config.json",
rest_numeric_enums = True,
service_yaml = "language_v1.yaml",
transport = "grpc+rest",
deps = [
],
)
40 changes: 40 additions & 0 deletions .generator/test_parse_googleapis_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import parse_googleapis_content
from pathlib import Path

GENERATOR_DIR = Path(__file__).resolve().parent
BUILD_BAZEL_PATH = f"{GENERATOR_DIR}/test-resources/librarian/BUILD.bazel"


def test_parse_build_bazel():
Comment thread
parthea marked this conversation as resolved.
expected_result = {
"language_proto": {"name": "language_proto"},
"language_py_gapic": {
"deps": [],
"grpc_service_config": "language_grpc_service_config.json",
"name": "language_py_gapic",
"rest_numeric_enums": True,
"service_yaml": "language_v1.yaml",
"srcs": [":language_proto"],
"transport": "grpc+rest",
},
}
with open(BUILD_BAZEL_PATH, "r") as f:
content = f.read()
result = parse_googleapis_content.parse_content(content)

assert result == expected_result
Loading