forked from googleapis/google-api-java-client-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synth.py
250 lines (204 loc) · 8.11 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright 2018 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 synthtool as s
from synthtool.__main__ import extra_args
from synthtool import log, shell
from synthtool.sources import git
import logging
from os import path, remove
from pathlib import Path
import glob
import json
from lxml import etree
import re
import sys
import shutil
from typing import List
logging.basicConfig(level=logging.DEBUG)
VERSION_REGEX = r"([^\.]*)\.(.+)\.json$"
TEMPLATE_VERSIONS = [
"1.30.1",
]
discovery_url = "https://github.com/googleapis/discovery-artifact-manager.git"
repository = Path('.')
log.debug(f"Cloning {discovery_url}.")
discovery = git.clone(discovery_url)
log.debug("Cleaning output directory.")
shell.run("rm -rf .cache".split(), cwd=repository)
log.debug("Installing dependencies.")
shell.run(
"python2 -m pip install -e generator/ --user".split(),
cwd=repository
)
def write_metadata_file(name: str, version: str, metadata: dict):
metadata_file = f'clients/{name}/{version}.metadata.json'
print(f"Writing json metadata to {metadata_file}")
metadata = {
"maven": metadata
}
with open(metadata_file, "w") as outfile:
json.dump(metadata, outfile, indent=2)
def write_readme_file(name: str, version: str, metadata: dict):
readme_file = f'clients/{name}/{version}/README.md'
print(f"Writing README to {readme_file}")
with open(readme_file, "w") as outfile:
outfile.write("FIXME")
def maven_metadata(pom_file: str):
tree = etree.parse(pom_file)
root = tree.getroot()
version = root.find("{http://maven.apache.org/POM/4.0.0}version").text
group_id = root.find("{http://maven.apache.org/POM/4.0.0}groupId").text
artifact_id = root.find("{http://maven.apache.org/POM/4.0.0}artifactId").text
return {
"groupId": group_id,
"artifactId": artifact_id,
"version": version
}
def generate_service(disco: str):
m = re.search(VERSION_REGEX, disco)
if m is None:
log.info(f"Skipping {disco}.")
return
name = m.group(1)
version = m.group(2)
log.info(f"Generating {name} {version}.")
library_name = f"google-api-services-{name}"
output_dir = repository / ".cache" / library_name / version
input_file = discovery / "discoveries" / disco
for template in TEMPLATE_VERSIONS:
log.info(f"\t{template}")
command = (
f"python2 -m googleapis.codegen --output_dir={output_dir}" +
f" --input={input_file} --language=java --language_variant={template}" +
f" --package_path=api/services"
)
shell.run(f"mkdir -p {output_dir}".split(), cwd=repository / "generator")
shell.run(command.split(), cwd=repository, hide_output=False)
s.copy(output_dir, f"clients/{library_name}/{version}/{template}")
resource_dir = repository / "clients" / library_name / version / template / "resources"
shell.run(f"mkdir -p {resource_dir}".split())
shutil.copy(input_file, resource_dir / path.basename(disco))
# write the metadata file
latest_version = TEMPLATE_VERSIONS[-1]
metadata = maven_metadata(str(repository / "clients" / library_name / version / latest_version / "pom.xml"))
write_metadata_file(library_name, version, metadata)
# copy the latest README to the main service location
shutil.copy(
repository / "clients" / library_name / version / latest_version / "README.md",
repository / "clients" / library_name / version / "README.md"
)
def all_discoveries():
discos = []
for file in glob.glob(str(discovery / 'discoveries/*.*.json')):
discos.append(path.basename(file))
return discos
class Service:
id: str = None
title: str = None
version: str = None
def __init__(self, discovery_path: str):
match = re.match(VERSION_REGEX, path.basename(discovery_path))
if match is not None:
self.id = match[1]
self.version = match[2]
with open(discovery_path, "r") as f:
data = json.load(f)
self.title = data["title"]
else:
print(path.basename(discovery_path))
def all_services():
services = []
for file in glob.glob(str(discovery / 'discoveries/*.*.json')):
match = re.match(VERSION_REGEX, path.basename(file))
service = Service(file)
services.append(service)
return services
def service_row(services: List[Service]) -> str:
services = sorted(services, key=lambda service: service.version)
links = [f"[{service.version}](clients/google-api-services-{service.id}/{service.version})" for service in services]
link = ", ".join(links)
name = services[0].title
return f"| {name} | {link} |\n"
def replace_content_in_readme(content_rows: List[str]) -> None:
START_MARKER = "[//]: # (API_TABLE_START)"
END_MARKER = "[//]: # (API_TABLE_END)"
newlines = []
repl_open = False
with open("README.md", "r") as f:
for line in f:
if not repl_open:
newlines.append(line)
if line.startswith(START_MARKER):
repl_open = True
newlines = newlines + content_rows
elif line.startswith(END_MARKER):
newlines.append("\n")
newlines.append(line)
repl_open = False
with open("README.md", "w") as f:
for line in newlines:
f.write(line)
def generate_service_list(services: List[Service]) -> None:
services_by_name = {}
for service in services:
if service.title is None:
print(service.id)
if service.title not in services_by_name:
services_by_name[service.title] = []
services_by_name[service.title].append(service)
content_rows = [
"\n",
"| API | Versions |\n",
"| --- | -------- |\n",
]
# print(services_by_name.keys())
content_rows += [service_row(services_by_name[name])
for name in sorted(services_by_name.keys())]
replace_content_in_readme(content_rows)
def remove_unused_services(services: List[Service]) -> None:
print("removing unused service versions")
services_by_id = {}
for service in services:
client_name = f"google-api-services-{service.id}"
if client_name not in services_by_id:
services_by_id[client_name] = []
services_by_id[client_name].append(service.version)
existing_clients = glob.glob("clients/google-api-services-*/*.metadata.json")
for existing_client in existing_clients:
version = path.basename(existing_client).replace(".metadata.json", "")
client_name = existing_client.split("/")[1]
if client_name not in services_by_id:
print(f"deleting entire service: {client_name}")
shutil.rmtree(f"clients/{client_name}", ignore_errors=True)
elif version not in services_by_id[client_name]:
print(f"deleting {version} of {client_name}")
shutil.rmtree(f"clients/{client_name}/{version}")
remove(f"clients/{client_name}/{version}.metadata.json")
def generate_services(services):
for service in services:
generate_service(service)
if extra_args():
api_name = extra_args()[0]
if api_name == "README":
services = all_services()
generate_service_list(services)
remove_unused_services(services)
else:
discoveries = all_discoveries()
discoveries = [discovery for discovery in discoveries if discovery.startswith(api_name)]
generate_services(discoveries)
else:
discoveries = all_discoveries()
generate_services(discoveries)