-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Make GAPIC Bazel rules production ready #402
Changes from 1 commit
84f91a6
b0b240b
1172bba
a72432d
bb5e605
8066385
6fae027
b69b8fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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 | ||
# spedds 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wat? Why is it building single-cored? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont know how exactly it does it, but based on time it takes and the CPU load on the build machine I assumed it must be single core |
||
# | ||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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") | ||
generate.generate() |
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", | ||
) |
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) | ||
Comment on lines
+18
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does skylark allow list comprehensions? Personal preference, feel free to ignore, but I think srcjar_srcs = [
dep_file
for dep in ctx.attr.deps
for dep_file in dep.files.to_list()
if dep_file.extension in ("srcjar", "jar", "zip")
] is more readable because of the indentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a reduced verison of more complicated for loops copied from the other rules. Starlak support list comprehension. For sake of style consistency with the other rules lets keep it like that. |
||
|
||
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 | ||
) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: speeds