forked from googleapis/python-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owlbot.py
441 lines (370 loc) · 15.1 KB
/
owlbot.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright 2022 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.
import json
from pathlib import Path
import re
import shutil
import textwrap
import synthtool as s
import synthtool.gcp as gcp
from synthtool.languages import python
# ----------------------------------------------------------------------------
# Copy the generated client from the owl-bot staging directory
# ----------------------------------------------------------------------------
clean_up_generated_samples = True
# Load the default version defined in .repo-metadata.json.
default_version = json.load(open(".repo-metadata.json", "rt")).get(
"default_version"
)
for library in s.get_staging_dirs(default_version):
if clean_up_generated_samples:
shutil.rmtree("samples/generated_samples", ignore_errors=True)
clean_up_generated_samples = False
# DEFAULT SCOPES and SERVICE_ADDRESS are being used. so let's force them in.
s.replace(
library / f"google/pubsub_{library.name}/services/*er/*client.py",
r"""DEFAULT_ENDPOINT = \"pubsub\.googleapis\.com\"""",
"""
# The scopes needed to make gRPC calls to all of the methods defined in
# this service
_DEFAULT_SCOPES = (
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/pubsub',
)
SERVICE_ADDRESS = "pubsub.googleapis.com:443"
\"""The default address of the service.\"""
\g<0>""",
)
# Modify GRPC options in transports.
count = s.replace(
[
library / f"google/pubsub_{library.name}/services/*/transports/grpc*",
library / f"tests/unit/gapic/pubsub_{library.name}/*",
],
"options=\[.*?\]",
"""options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
("grpc.max_metadata_size", 4 * 1024 * 1024),
("grpc.keepalive_time_ms", 30000),
]""",
flags=re.MULTILINE | re.DOTALL,
)
if count < 15:
raise Exception("Expected replacements for gRPC channel options not made.")
# If the emulator is used, force an insecure gRPC channel to avoid SSL errors.
clients_to_patch = [
library / f"google/pubsub_{library.name}/services/publisher/client.py",
library / f"google/pubsub_{library.name}/services/subscriber/client.py",
library / f"google/pubsub_{library.name}/services/schema_service/client.py",
]
err_msg = (
"Expected replacements for gRPC channel to use with the emulator not made."
)
count = s.replace(clients_to_patch, r"import os", "import functools\n\g<0>")
if count < len(clients_to_patch):
raise Exception(err_msg)
count = s.replace(
clients_to_patch,
f"from \.transports\.base",
"\nimport grpc\n\g<0>",
)
if count < len(clients_to_patch):
raise Exception(err_msg)
count = s.replace(
clients_to_patch,
r"Transport = type\(self\)\.get_transport_class\(transport\)",
"""\g<0>
emulator_host = os.environ.get("PUBSUB_EMULATOR_HOST")
if emulator_host:
if issubclass(Transport, type(self)._transport_registry["grpc"]):
channel = grpc.insecure_channel(target=emulator_host)
else:
channel = grpc.aio.insecure_channel(target=emulator_host)
Transport = functools.partial(Transport, channel=channel)
""",
)
if count < len(clients_to_patch):
raise Exception(err_msg)
# Monkey patch the streaming_pull() GAPIC method to disable pre-fetching stream
# results.
s.replace(
library / f"google/pubsub_{library.name}/services/subscriber/client.py",
(
r"# Wrap the RPC method.*\n"
r"\s+# and friendly error.*\n"
r"\s+rpc = self\._transport\._wrapped_methods\[self\._transport\.streaming_pull\]"
),
"""
# Wrappers in api-core should not automatically pre-fetch the first
# stream result, as this breaks the stream when re-opening it.
# https://github.com/googleapis/python-pubsub/issues/93#issuecomment-630762257
self._transport.streaming_pull._prefetch_first_result_ = False
\g<0>""",
)
# Emit deprecation warning if return_immediately flag is set with synchronous pull.
s.replace(
library / f"google/pubsub_{library.name}/services/subscriber/*client.py",
r"from google.pubsub_v1 import gapic_version as package_version",
"import warnings\n\g<0>",
)
count = s.replace(
library / f"google/pubsub_{library.name}/services/subscriber/*client.py",
r"""
([^\n\S]+(?:async\ )?def\ pull\(.*?->\ pubsub\.PullResponse:.*?)
((?P<indent>[^\n\S]+)\#\ Wrap\ the\ RPC\ method)
""",
textwrap.dedent(
"""
\g<1>
\g<indent>if request.return_immediately:
\g<indent> warnings.warn(
\g<indent> "The return_immediately flag is deprecated and should be set to False.",
\g<indent> category=DeprecationWarning,
\g<indent> )
\g<2>"""
),
flags=re.MULTILINE | re.DOTALL | re.VERBOSE,
)
if count != 2:
raise Exception("Too many or too few replacements in pull() methods.")
# Silence deprecation warnings in pull() method flattened parameter tests.
s.replace(
library / f"tests/unit/gapic/pubsub_{library.name}/test_subscriber.py",
"import os",
"\g<0>\nimport warnings",
)
count = s.replace(
library / f"tests/unit/gapic/pubsub_{library.name}/test_subscriber.py",
textwrap.dedent(
r"""
([^\n\S]+# Call the method with a truthy value for each flattened field,
[^\n\S]+# using the keyword arguments to the method\.)
\s+(client\.pull\(.*?\))"""
),
"""\n\g<1>
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
\g<2>""",
flags=re.MULTILINE | re.DOTALL,
)
if count < 1:
raise Exception("Catch warnings replacement failed.")
count = s.replace(
library / f"tests/unit/gapic/pubsub_{library.name}/test_subscriber.py",
textwrap.dedent(
r"""
([^\n\S]+# Call the method with a truthy value for each flattened field,
[^\n\S]+# using the keyword arguments to the method\.)
\s+response = (await client\.pull\(.*?\))"""
),
"""\n\g<1>
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
\g<2>""",
flags=re.MULTILINE | re.DOTALL,
)
if count < 1:
raise Exception("Catch warnings replacement failed.")
# Make sure that client library version is present in user agent header.
count = s.replace(
[
library
/ f"google/pubsub_{library.name}/services/publisher/async_client.py",
library / f"google/pubsub_{library.name}/services/publisher/client.py",
library
/ f"google/pubsub_{library.name}/services/publisher/transports/base.py",
library
/ f"google/pubsub_{library.name}/services/schema_service/async_client.py",
library / f"google/pubsub_{library.name}/services/schema_service/client.py",
library
/ f"google/pubsub_{library.name}/services/schema_service/transports/base.py",
library
/ f"google/pubsub_{library.name}/services/subscriber/async_client.py",
library / f"google/pubsub_{library.name}/services/subscriber/client.py",
library
/ f"google/pubsub_{library.name}/services/subscriber/transports/base.py",
],
r"""gapic_version=package_version.__version__""",
"client_library_version=package_version.__version__",
)
if count < 1:
raise Exception("client_library_version replacement failed.")
# Allow timeout to be an instance of google.api_core.timeout.*
count = s.replace(
library / f"google/pubsub_{library.name}/types/__init__.py",
r"from \.pubsub import \(",
"from typing import Union\n\n\g<0>",
)
if count < 1:
raise Exception("Catch timeout replacement 1 failed.")
count = s.replace(
library / f"google/pubsub_{library.name}/types/__init__.py",
r"__all__ = \(\n",
textwrap.dedent(
'''\
TimeoutType = Union[
int,
float,
"google.api_core.timeout.ConstantTimeout",
"google.api_core.timeout.ExponentialTimeout",
]
"""The type of the timeout parameter of publisher client methods."""
\g<0> "TimeoutType",'''
),
)
if count < 1:
raise Exception("Catch timeout replacement 2 failed.")
count = s.replace(
library / f"google/pubsub_{library.name}/services/publisher/*client.py",
r"from google.api_core import retry as retries.*\n",
"\g<0>from google.api_core import timeout as timeouts # type: ignore\n",
)
if count < 1:
raise Exception("Catch timeout replacement 3 failed.")
count = s.replace(
library / f"google/pubsub_{library.name}/services/publisher/*client.py",
f"from google\.pubsub_{library.name}\.types import pubsub",
f"\g<0>\nfrom google.pubsub_{library.name}.types import TimeoutType",
)
if count < 1:
raise Exception("Catch timeout replacement 4 failed.")
count = s.replace(
library / f"google/pubsub_{library.name}/services/publisher/*client.py",
r"(\s+)timeout: Union\[float, object\] = gapic_v1.method.DEFAULT.*\n",
f"\g<1>timeout: TimeoutType = gapic_{library.name}.method.DEFAULT,",
)
if count < 1:
raise Exception("Catch timeout replacement 5 failed.")
count = s.replace(
library / f"google/pubsub_{library.name}/services/publisher/*client.py",
r"([^\S\r\n]+)timeout \(float\): (.*)\n",
("\g<1>timeout (TimeoutType):\n" "\g<1> \g<2>\n"),
)
if count < 1:
raise Exception("Catch timeout replacement 6 failed.")
# Override the default max retry deadline for publisher methods.
count = s.replace(
library / f"google/pubsub_{library.name}/services/publisher/transports/base.py",
r"deadline=60\.0",
"deadline=600.0",
)
if count < 9:
raise Exception(
"Default retry deadline not overriden for all publisher methods."
)
# The namespace package declaration in google/cloud/__init__.py should be excluded
# from coverage.
count = s.replace(
library / ".coveragerc",
"google/pubsub/__init__.py",
"""google/cloud/__init__.py
google/pubsub/__init__.py""",
)
if count < 1:
raise Exception(".coveragerc replacement failed.")
s.move([library], excludes=["**/gapic_version.py", "README.rst", "docs/**/*", "setup.py", "testing/constraints-3.7.txt"])
s.remove_staging_dirs()
# ----------------------------------------------------------------------------
# Add templated files
# ----------------------------------------------------------------------------
templated_files = gcp.CommonTemplates().py_library(
microgenerator=True,
samples=True,
cov_level=100,
versions=gcp.common.detect_versions(path="./google", default_first=True),
unit_test_python_versions=["3.7", "3.8", "3.9", "3.10", "3.11"],
system_test_python_versions=["3.10"],
system_test_external_dependencies=["psutil"],
)
s.move(templated_files, excludes=[".coveragerc", ".github/release-please.yml", "README.rst", "docs/index.rst"])
# ----------------------------------------------------------------------------
# Add mypy nox session.
# ----------------------------------------------------------------------------
s.replace(
"noxfile.py",
r"LINT_PATHS = \[.*?\]",
'\g<0>\n\nMYPY_VERSION = "mypy==0.910"',
)
s.replace(
"noxfile.py", r'"blacken",', '\g<0>\n "mypy",',
)
s.replace(
"noxfile.py", r'"mock"', '"mock==5.0.0"',
)
s.replace(
"noxfile.py",
r"nox\.options\.error_on_missing_interpreters = True",
textwrap.dedent(
''' \g<0>
@nox.session(python=DEFAULT_PYTHON_VERSION)
def mypy(session):
"""Run type checks with mypy."""
session.install("-e", ".[all]")
session.install(MYPY_VERSION)
# Just install the type info directly, since "mypy --install-types" might
# require an additional pass.
session.install("types-protobuf", "types-setuptools")
# Version 2.1.1 of google-api-core version is the first type-checked release.
# Version 2.2.0 of google-cloud-core version is the first type-checked release.
session.install(
"google-api-core[grpc]>=2.1.1",
"google-cloud-core>=2.2.0",
)
# TODO: Only check the hand-written layer, the generated code does not pass
# mypy checks yet.
# https://github.com/googleapis/gapic-generator-python/issues/1092
session.run("mypy", "google/cloud")'''
),
)
# ----------------------------------------------------------------------------
# Add mypy_samples nox session.
# ----------------------------------------------------------------------------
s.replace(
"noxfile.py",
r' "mypy",',
'\g<0>\n # https://github.com/googleapis/python-pubsub/pull/552#issuecomment-1016256936'
'\n # "mypy_samples", # TODO: uncomment when the check passes',
)
s.replace(
"noxfile.py",
r'session\.run\("mypy", "google/cloud"\)',
textwrap.dedent(
''' \g<0>
@nox.session(python=DEFAULT_PYTHON_VERSION)
def mypy_samples(session):
"""Run type checks with mypy."""
session.install("-e", ".[all]")
session.install("pytest")
session.install(MYPY_VERSION)
# Just install the type info directly, since "mypy --install-types" might
# require an additional pass.
session.install("types-mock", "types-protobuf", "types-setuptools")
session.run(
"mypy",
"--config-file",
str(CURRENT_DIRECTORY / "samples" / "snippets" / "mypy.ini"),
"--no-incremental", # Required by warn-unused-configs from mypy.ini to work
"samples/",
)'''
),
)
# Only consider the hand-written layer when assessing the test coverage.
s.replace(
"noxfile.py", "--cov=google", "--cov=google/cloud",
)
python.py_samples(skip_readmes=True)
# run format session for all directories which have a noxfile
for noxfile in Path(".").glob("**/noxfile.py"):
s.shell.run(["nox", "-s", "blacken"], cwd=noxfile.parent, hide_output=False)