-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make GAPIC Bazel rules production ready (#402)
Shoud fix #400 and #390, plus a bunch of other not-yet-opened issues. This includes: 1) Fix long initial load time (5+ min). This was caused by python_rules buildling `grpcio` dependency from sources in one core (which was super slow). Switched to using bazel-native `"@com_github_grpc_grpc//src/python/grpcio/grpc:grpcio"` target instead, which is not only much faster, but is also already used in googleapis, so there is no additional cost for reusing it in microgenerator rules. 2) Properly handle `pandoc` dependency (platform-sepcific version of pandoc is properly pulled by bazel itself using toolchains). 3) Add simplistic version of the `py_gapic_assembly_pkg` rule, to make output of microgenerator compatible with `GAPICBazel` class in synthtool. 4) Add `plugin_args` argument for python_gapic_library rule to pass custom argumetns to the plugin (similar to PHP rules). 5) Add compatibility with `python3.6` runtime (otherwise `python3.7` is minimum because of dependency on `dataclasses` module). Python 3.6 compatibility can be enabled by adding `--define=gapic_gen_python=3.6` command line argument to `bazel build` command. 6) Add support for Python runtimes installed with `pyenv`. To tell bazel using Python3 installed via pyenv add `--extra_toolchains=@gapic_generator_python//:pyenv3_toolchain` argument to `bazel build` command.
- Loading branch information
1 parent
e716cb3
commit d18ed41
Showing
9 changed files
with
276 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,73 @@ | ||
load("//:gapic_generator_python.bzl", "pandoc_binary", "pandoc_toolchain") | ||
load("@gapic_generator_python_pip_deps//:requirements.bzl", "requirement") | ||
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair") | ||
|
||
toolchain_type( | ||
name = "pandoc_toolchain_type", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
pandoc_toolchain( | ||
exec_compatible_with = [ | ||
"@bazel_tools//platforms:linux", | ||
"@bazel_tools//platforms:x86_64", | ||
], | ||
platform = "linux", | ||
) | ||
|
||
pandoc_toolchain( | ||
exec_compatible_with = [ | ||
"@bazel_tools//platforms:osx", | ||
"@bazel_tools//platforms:x86_64", | ||
], | ||
platform = "macOS", | ||
) | ||
|
||
pandoc_binary( | ||
name = "pandoc_binary", | ||
) | ||
|
||
config_setting( | ||
name = "gapic_gen_python_3_6", | ||
values = {"define": "gapic_gen_python=3.6"}, | ||
) | ||
|
||
py_runtime( | ||
name = "pyenv3_runtime", | ||
interpreter = ":pyenv3wrapper.sh", | ||
python_version="PY3", | ||
) | ||
|
||
py_runtime_pair( | ||
name = "pyenv3_runtime_pair", | ||
py3_runtime = ":pyenv3_runtime", | ||
) | ||
|
||
toolchain( | ||
name = "pyenv3_toolchain", | ||
toolchain = ":pyenv3_runtime_pair", | ||
toolchain_type = "@bazel_tools//tools/python:toolchain_type", | ||
) | ||
|
||
py_binary( | ||
name = "gapic_plugin", | ||
srcs = glob(["gapic/**/*.py"]), | ||
data = glob(["gapic/**/*.j2"]), | ||
main = "gapic/cli/generate.py", | ||
data = [":pandoc_binary"] + glob(["gapic/**/*.j2"]), | ||
main = "gapic/cli/generate_with_pandoc.py", | ||
python_version = "PY3", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"@com_google_protobuf//:protobuf_python", | ||
"@com_github_grpc_grpc//src/python/grpcio/grpc:grpcio", | ||
requirement("click"), | ||
requirement("google-api-core"), | ||
requirement("googleapis-common-protos"), | ||
requirement("grpcio"), | ||
requirement("jinja2"), | ||
requirement("MarkupSafe"), | ||
requirement("pypandoc"), | ||
requirement("PyYAML"), | ||
], | ||
python_version = "PY3", | ||
] + select({ | ||
":gapic_gen_python_3_6": [requirement("dataclasses")], | ||
"//conditions:default": [], | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
|
||
HOME_DIR=$(getent passwd "$(whoami)" | cut -d: -f6) | ||
exec "$HOME_DIR/.pyenv/shims/python3" "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2020 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 | ||
# | ||
# https://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. | ||
|
||
load("@com_google_api_codegen//rules_gapic:gapic_pkg.bzl", "construct_package_dir_paths") | ||
|
||
def _py_gapic_src_pkg_impl(ctx): | ||
srcjar_srcs = [] | ||
for dep in ctx.attr.deps: | ||
for f in dep.files.to_list(): | ||
if f.extension in ("srcjar", "jar", "zip"): | ||
srcjar_srcs.append(f) | ||
|
||
paths = construct_package_dir_paths(ctx.attr.package_dir, ctx.outputs.pkg, ctx.label.name) | ||
|
||
script = """ | ||
mkdir -p {package_dir_path} | ||
for srcjar_src in {srcjar_srcs}; do | ||
unzip -q -o $srcjar_src -d {package_dir_path} | ||
done | ||
cd {package_dir_path}/.. | ||
tar -zchpf {package_dir}/{package_dir}.tar.gz {package_dir} | ||
cd - | ||
mv {package_dir_path}/{package_dir}.tar.gz {pkg} | ||
rm -rf {package_dir_path} | ||
""".format( | ||
srcjar_srcs = " ".join(["'%s'" % f.path for f in srcjar_srcs]), | ||
package_dir_path = paths.package_dir_path, | ||
package_dir = paths.package_dir, | ||
pkg = ctx.outputs.pkg.path, | ||
package_dir_expr = paths.package_dir_expr, | ||
) | ||
|
||
ctx.actions.run_shell( | ||
inputs = srcjar_srcs, | ||
command = script, | ||
outputs = [ctx.outputs.pkg], | ||
) | ||
|
||
_py_gapic_src_pkg = rule( | ||
attrs = { | ||
"deps": attr.label_list(allow_files = True, mandatory = True), | ||
"package_dir": attr.string(mandatory = True), | ||
}, | ||
outputs = {"pkg": "%{name}.tar.gz"}, | ||
implementation = _py_gapic_src_pkg_impl, | ||
) | ||
|
||
def py_gapic_assembly_pkg(name, deps, assembly_name = None, **kwargs): | ||
package_dir = name | ||
if assembly_name: | ||
package_dir = "%s-%s" % (assembly_name, name) | ||
_py_gapic_src_pkg( | ||
name = name, | ||
deps = deps, | ||
package_dir = package_dir, | ||
**kwargs | ||
) | ||
|
||
|