This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
/
synth.py
109 lines (88 loc) · 3.89 KB
/
synth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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
#
# http://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.
"""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.languages import java
from synthtool.sources import git
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)
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")
proto_path = (target.split(":")[0])[2:]
s.copy(googleapis / proto_path / f"*.proto", "proto-google-common-protos/src/main/proto/"+ proto_path )
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-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")
build_proto("//google/cloud:extended_operations_java_proto")
build_grpc("//google/longrunning:longrunning_java_grpc")
java.format_code("proto-google-common-protos/src")
java.format_code("grpc-google-common-protos/src")
os.remove("proto-google-common-protos/src/main/java/com/google/cloud/audit/BigQueryAuditMetadata.java")
os.remove("proto-google-common-protos/src/main/java/com/google/cloud/audit/BigQueryAuditMetadataOrBuilder.java")
os.remove("proto-google-common-protos/src/main/java/com/google/cloud/audit/BigQueryAuditMetadataProto.java")
os.remove("proto-google-common-protos/src/main/proto/google/cloud/audit/bigquery_audit_metadata.proto")
os.remove("proto-google-common-protos/src/main/proto/google/cloud/common_resources.proto")
java.common_templates(excludes=[
'README.md',
'samples/*',
'.github/workflows/samples.yaml',
])