feat: Make GAPIC Bazel rules production ready by vam-google · Pull Request #402 · googleapis/gapic-generator-python · GitHub
Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
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
62 changes: 57 additions & 5 deletions BUILD.bazel
39 changes: 35 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ workspace(name = "gapic_generator_python")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

#
# Import rules_python
#
http_archive(
name = "bazel_skylib",
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/0.9.0/bazel_skylib-0.9.0.tar.gz"],
)

http_archive(
name = "rules_python",
strip_prefix = "rules_python-748aa53d7701e71101dfd15d800e100f6ff8e5d1",
Expand All @@ -22,14 +24,43 @@ pip_repositories()
#
# Import gapic-generator-python specific dependencies
#
load("//:repositories.bzl", "gapic_generator_python")
load("//:repositories.bzl",
"gapic_generator_python",
"gapic_generator_register_toolchains"
)

gapic_generator_python()

gapic_generator_register_toolchains()

load("@gapic_generator_python_pip_deps//:requirements.bzl", "pip_install")

pip_install()

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

#
# Import grpc as a native bazel dependency. This avoids duplication and also
# speeds up loading phase a lot (otherwise python_rules will be building grpcio
# from sources in a single-core speed, which takes around 5 minutes on a regular
# workstation)
#
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")

grpc_deps()

load("@upb//bazel:repository_defs.bzl", "bazel_version_repository")

bazel_version_repository(
name = "bazel_version",
)

load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")

apple_rules_dependencies()

load("@build_bazel_apple_support//lib:repositories.bzl", "apple_support_dependencies")

apple_support_dependencies()
9 changes: 9 additions & 0 deletions gapic/cli/generate_with_pandoc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

from gapic.cli import generate

if __name__ == '__main__':
os.environ['PYPANDOC_PANDOC'] = os.path.join(
os.path.abspath(__file__).rsplit("gapic", 1)[0], "pandoc")
os.environ['LC_ALL'] = 'C.UTF-8'
generate.generate()
62 changes: 62 additions & 0 deletions gapic_generator_python.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
def _pandoc_binary_impl(ctx):
toolchain = ctx.toolchains["@gapic_generator_python//:pandoc_toolchain_type"]
output = ctx.actions.declare_file(ctx.attr.binary_name)

script = """
cp {input} {output}
chmod +x {output}
""".format(
input = toolchain.pandoc.files.to_list()[0].path,
output = output.path,
)
ctx.actions.run_shell(
command = script,
inputs = toolchain.pandoc.files,
outputs = [output],
)
return [DefaultInfo(files = depset(direct = [output]), executable = output)]

pandoc_binary = rule(
attrs = {
"binary_name": attr.string(default = "pandoc")
},
executable = True,
toolchains = ["@gapic_generator_python//:pandoc_toolchain_type"],
implementation = _pandoc_binary_impl,
)

#
# Toolchains
#
def _pandoc_toolchain_info_impl(ctx):
return [
platform_common.ToolchainInfo(
pandoc = ctx.attr.pandoc,
),
]

_pandoc_toolchain_info = rule(
attrs = {
"pandoc": attr.label(
allow_single_file = True,
cfg = "host",
executable = True,
),
},
implementation = _pandoc_toolchain_info_impl,
)

def pandoc_toolchain(platform, exec_compatible_with):
toolchain_info_name = "pandoc_toolchain_info_%s" % platform
_pandoc_toolchain_info(
name = toolchain_info_name,
pandoc = "@pandoc_%s//:pandoc" % platform,
visibility = ["//visibility:public"],
)

native.toolchain(
name = "pandoc_toolchain_%s" % platform,
exec_compatible_with = exec_compatible_with,
toolchain = toolchain_info_name,
toolchain_type = ":pandoc_toolchain_type",
)
4 changes: 4 additions & 0 deletions pyenv3wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

HOME_DIR=$(getent passwd "$(whoami)" | cut -d: -f6)
Comment thread
software-dov marked this conversation as resolved.
exec "$HOME_DIR/.pyenv/shims/python3" "$@"
36 changes: 36 additions & 0 deletions repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@rules_python//python:pip.bzl", "pip_import")

_PANDOC_BUILD_FILE = """
filegroup(
name = "pandoc",
srcs = ["bin/pandoc"],
visibility = ["//visibility:public"],
)"""

def gapic_generator_python():
_maybe(
pip_import,
Expand All @@ -25,13 +32,42 @@ def gapic_generator_python():
urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"],
)

_maybe(
http_archive,
name = "com_github_grpc_grpc",
strip_prefix = "grpc-8347f4753568b5b66e49111c60ae2841278d3f33", # this is 1.25.0 with fixes
urls = ["https://github.com/grpc/grpc/archive/8347f4753568b5b66e49111c60ae2841278d3f33.zip"],
)

_maybe(
http_archive,
name = "pandoc_linux",
build_file_content = _PANDOC_BUILD_FILE,
strip_prefix = "pandoc-2.2.1",
url = "https://github.com/jgm/pandoc/releases/download/2.2.1/pandoc-2.2.1-linux.tar.gz",
)

_maybe(
http_archive,
name = "pandoc_macOS",
build_file_content = _PANDOC_BUILD_FILE,
strip_prefix = "pandoc-2.2.1",
url = "https://github.com/jgm/pandoc/releases/download/2.2.1/pandoc-2.2.1-macOS.zip",
)

_maybe(
http_archive,
name = "com_google_api_codegen",
strip_prefix = "gapic-generator-b32c73219d617f90de70bfa6ff0ea0b0dd638dfe",
urls = ["https://github.com/googleapis/gapic-generator/archive/b32c73219d617f90de70bfa6ff0ea0b0dd638dfe.zip"],
)

def gapic_generator_register_toolchains():
native.register_toolchains(
"@gapic_generator_python//:pandoc_toolchain_linux",
"@gapic_generator_python//:pandoc_toolchain_macOS",
)

def _maybe(repo_rule, name, strip_repo_prefix = "", **kwargs):
if not name.startswith(strip_repo_prefix):
return
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
click==7.1.2
google-api-core==1.17.0
googleapis-common-protos==1.51.0
grpcio==1.28.1
jinja2==2.11.2
MarkupSafe==1.1.1
protobuf==3.11.3
pypandoc==1.5
PyYAML==5.3.1
dataclasses==0.6
4 changes: 2 additions & 2 deletions rules_python_gapic/py_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

load("@com_google_api_codegen//rules_gapic:gapic.bzl", "proto_custom_library")

def py_gapic_library(name, srcs, **kwargs):
def py_gapic_library(name, srcs, plugin_args = [], **kwargs):
# srcjar_target_name = "%s_srcjar" % name
srcjar_target_name = name
srcjar_output_suffix = ".srcjar"
Expand All @@ -23,7 +23,7 @@ def py_gapic_library(name, srcs, **kwargs):
name = srcjar_target_name,
deps = srcs,
plugin = Label("@gapic_generator_python//:gapic_plugin"),
plugin_args = [],
plugin_args = plugin_args,
plugin_file_args = {},
output_type = "python_gapic",
output_suffix = srcjar_output_suffix,
Expand Down
70 changes: 70 additions & 0 deletions rules_python_gapic/py_gapic_pkg.bzl