-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
new-client.py
178 lines (150 loc) · 4.87 KB
/
new-client.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Script for enabling generation of a new library
Creates initial files and appends a new library to generation_config.yaml
Some of the logic is copied from new-client.py as
a transition step while we test the hermetic build scripts
"""
import click
import os
import re
import sys
from ruamel.yaml import YAML
yaml = YAML()
script_dir = os.path.dirname(os.path.realpath(__file__))
@click.group(invoke_without_command=False)
@click.pass_context
@click.version_option(message="%(version)s")
def main(ctx):
pass
@main.command()
@click.option(
"--api_shortname",
required=True,
type=str,
prompt="Service name? (e.g. automl)",
help="Name for the new directory name and (default) artifact name",
)
@click.option(
"--name-pretty",
required=True,
type=str,
prompt="Pretty name? (e.g. 'Cloud AutoML')",
help="The human-friendly name that appears in README.md",
)
@click.option(
"--proto-path",
required=True,
type=str,
default=None,
help="Path to proto file from the root of the googleapis repository to the"
"directory that contains the proto files (without the version)."
"For example, to generate the library for 'google/maps/routing/v2', "
"then you specify this value as 'google/maps/routing'",
)
@click.option(
"--product-docs",
required=True,
type=str,
prompt="Product Documentation URL",
help="Documentation URL that appears in README.md",
)
@click.option(
"--rest-docs",
type=str,
help="If it exists, link to the REST Documentation for a service",
)
@click.option(
"--rpc-docs",
type=str,
help="If it exists, link to the RPC Documentation for a service",
)
@click.option(
"--api-description",
required=True,
type=str,
prompt="Description for README. The first sentence is prefixed by the "
"pretty name",
help="Description that appears in README.md",
)
@click.option(
"--transport",
type=click.Choice(["grpc", "http", "both"]),
default="grpc",
show_default=True,
help="A label that appears in repo-metadata.json",
)
@click.option(
"--destination-name",
type=str,
default=None,
help="The directory name of the new library. By default it's "
"java-<api_shortname>",
)
@click.option(
"--distribution-name",
type=str,
help="Maven coordinates of the generated library. By default it's "
"com.google.cloud:google-cloud-<api_shortname>",
)
def add_new_library(
api_shortname,
name_pretty,
proto_path,
product_docs,
rest_docs,
rpc_docs,
api_description,
transport,
destination_name,
distribution_name,
):
output_name = destination_name if destination_name else api_shortname
if distribution_name is None:
distribution_name = f"com.google-cloud:google-cloud-{output_name}"
distribution_name_short = re.split(r"[:\/]", distribution_name)[-1]
api_id = f"{api_shortname}.googleapis.com"
if not product_docs.startswith("https"):
sys.exit(
f"product_docs must starts with 'https://' - actual value is {product_docs}"
)
client_documentation = f"https://cloud.google.com/java/docs/reference/{distribution_name_short}/latest/overview"
if destination_name is None:
destination_name = f"java-{api_shortname}"
if api_shortname == "":
sys.exit("api_shortname is empty")
path_to_yaml = os.path.join(script_dir, "..", "..", "generation_config.yaml")
with open(path_to_yaml, "r") as file_stream:
config = yaml.load(file_stream)
# confirm library doesn't exist both in the monorepo and config yaml
path_to_library = os.path.join(script_dir, "..", "..", destination_name)
for library in config["libraries"]:
if __compute_destination_name(library) == destination_name:
print(f"library {destination_name} already exists")
sys.exit(1)
new_library = {
"api_shortname": api_shortname,
"name_pretty": name_pretty,
"product_documentation": product_docs,
"api_description": api_description,
"transport": transport,
"destination_name": destination_name,
"GAPICs": [{
"proto_path": proto_path
}]
}
__add_item_if_set(new_library, "rest_docs", rest_docs)
__add_item_if_set(new_library, "rpc_docs", rpc_docs)
__add_item_if_set(new_library, "distribution_name", distribution_name)
config["libraries"].append(new_library)
config["libraries"] = sorted(config["libraries"], key=__compute_destination_name)
with open(path_to_yaml, "w") as file_stream:
yaml.dump(config, file_stream)
def __add_item_if_set(target, key, value):
if value is not None:
target[key] = value
def __compute_destination_name(library: dict) -> str:
if "library_name" in library:
return f'java-{library["library_name"]}'
return f'java-{library["api_shortname"]}'
if __name__ == "__main__":
main()