Skip to content
This repository has been archived by the owner on Sep 16, 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 (#34)
  • Loading branch information
chingor13 committed Sep 18, 2020
1 parent 73fb8ec commit a354468
Showing 1 changed file with 63 additions and 36 deletions.
99 changes: 63 additions & 36 deletions synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,78 @@

"""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
import synthtool.gcp as gcp
import synthtool.languages.java as java
from synthtool.languages import java
from synthtool.sources import git
from synthtool import logger, shell

service = 'iam'
versions = ['v1']

gapic = gcp.GAPICBazel()
GOOGLEAPIS_URL = git.make_repo_clone_url("googleapis/googleapis")

for version in versions:
library = gapic.java_library(
service=service,
version=version,
proto_path=f'google/iam/{version}',
bazel_target=f'//google/iam/{version}:google-iam-{version}-java',
)
library = library / f"google-iam-{version}-java"
java.fix_proto_headers(library / f"proto-google-iam-{version}-java")
s.copy(
[library / f"proto-google-iam-{version}-java/src"],
f"proto-google-iam-{version}/src",
required=True,
)
java.fix_grpc_headers(library / f"grpc-google-iam-{version}-java", package_name=None)
s.copy(
[library / f"grpc-google-iam-{version}-java/src"],
f"grpc-google-iam-{version}/src",
required=True,
)
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)

output_dir = Path(f"bazel-bin{os.path.sep}{target[2:].split(':')[0]}").resolve()
os.chdir(old_cwd)

# logging protos
logging = gapic.java_library(
service=service,
version=version,
proto_path=f'google/iam/{version}/logging',
bazel_target=f'//google/iam/{version}/logging:google-iam-{version}-logging-java',
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,
)
logging = logging / f"google-iam-{version}-logging-java"
java.fix_proto_headers(logging / f"proto-google-iam-{version}-logging-java")
s.copy(
[logging / f"proto-google-iam-{version}-logging-java/src"],
f"proto-google-iam-{version}/src",
required=True,

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-iam-v1/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,
)

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-iam-v1/src/main/java/com")

for version in versions:
build_proto(f"//google/iam/{version}:iam_java_proto")
build_proto(f"//google/iam/{version}/logging:logging_java_proto")
build_grpc(f"//google/iam/{version}:iam_java_grpc")

java.format_code(f"proto-google-iam-{version}/src")
java.format_code(f"grpc-google-iam-{version}/src")

Expand Down

0 comments on commit a354468

Please sign in to comment.