Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
build: fix synth.py to generate only proto/grpc targets without gapic…
Browse files Browse the repository at this point in the history
… assembly tarball (#32)
  • Loading branch information
chingor13 committed Sep 18, 2020
1 parent eb88e58 commit 7ca08bb
Showing 1 changed file with 69 additions and 20 deletions.
89 changes: 69 additions & 20 deletions synth.py
Expand Up @@ -14,32 +14,81 @@

"""This script is used to synthesize generated parts of this library."""

import os
from pathlib import Path
import tempfile
from typing import Union

import synthtool as s
from synthtool import gcp
from synthtool.languages import java
from synthtool.sources import git
import synthtool.languages.java as java
from pathlib import Path
from synthtool import logger, shell

GOOGLEAPIS_URL = git.make_repo_clone_url("googleapis/googleapis")

logger.debug("Cloning googleapis.")
googleapis = git.clone(GOOGLEAPIS_URL)

def bazel_build(target: str, cwd: Union[Path, str]) -> Path:
"""Build a bazel target and return the output build directory."""
old_cwd = os.getcwd()
os.chdir(str(cwd))

bazel_run_args = [
"bazel",
"--max_idle_secs=240",
"build",
target,
]

logger.debug(f"Generating code for: {target}.")
shell.run(bazel_run_args)

gapic = gcp.GAPICBazel()
googleapis = Path("/home/chingor/code/googleapis")

proto_targets = ["api", "cloud/audit", "geo/type", "logging/type", "longrunning", "rpc", "rpc/context", "type"]
for path in proto_targets:
target = path.replace("/", "-")
target = f"google-{target}-java"
library = gapic.java_library(
service=target,
version="unused",
proto_path=f"google/{path}:", #google-iam-{version}-logging-java',
bazel_target=f"//google/{path}:{target}",
output_dir = Path(f"bazel-bin{os.path.sep}{target[2:].split(':')[0]}").resolve()
os.chdir(old_cwd)

return output_dir

def build_proto(target):
"""Build a proto build target and copy all generate source files."""
output = bazel_build(
target=target,
cwd=googleapis,
)

src_output = Path(tempfile.mkdtemp())
for proto_jar in output.glob("*-speed-src.jar"):
logger.debug(f"unzipping: {os.path.basename(proto_jar)}")
shell.run(["unzip", "-o", proto_jar, "-d", src_output / "src"])

java.fix_proto_headers(src_output)
s.copy(src_output / "src/com", "proto-google-common-protos/src/main/java/com")

def build_grpc(target):
"""Build a grpc build target and copy all generate source files."""
output = bazel_build(
target=target,
cwd=googleapis,
)

library = library / target
java.fix_proto_headers(library / f"proto-{target}")
s.copy(library / f"proto-{target}" / "src", "proto-google-common-protos/src", required=True)
src_output = Path(tempfile.mkdtemp())
for proto_jar in output.glob("*grpc-src.jar"):
logger.debug(f"unzipping: {os.path.basename(proto_jar)}")
shell.run(["unzip", "-o", proto_jar, "-d", src_output / "src"])

java.fix_grpc_headers(src_output, "")
s.copy(src_output / "src/com", "grpc-google-common-protos/src/main/java/com")

build_proto("//google/api:api_java_proto")
build_proto("//google/cloud/audit:audit_java_proto")
build_proto("//google/geo/type:viewport_java_proto")
build_proto("//google/logging/type:type_java_proto")
build_proto("//google/longrunning:longrunning_java_proto")
build_proto("//google/rpc:rpc_java_proto")
build_proto("//google/rpc/context:attribute_context_java_proto")
build_proto("//google/type:type_java_proto")

java.fix_grpc_headers(library / f"grpc-{target}", package_name=None)
s.copy(library / f"grpc-{target}" / "src", "grpc-google-common-protos/src")
build_grpc("//google/longrunning:longrunning_java_grpc")

java.format_code("proto-google-common-protos/src")
java.format_code("grpc-google-common-protos/src")
Expand Down

0 comments on commit 7ca08bb

Please sign in to comment.