-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_extensions.py
386 lines (303 loc) · 9.79 KB
/
local_extensions.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
import re
from functools import lru_cache
from http import HTTPStatus
from logging import getLogger
from subprocess import PIPE, Popen
from time import sleep, time
import requests
from jinja2.environment import Environment
from jinja2.ext import Extension
from packaging import version
_logger = getLogger(__name__)
def is_truthy(
v,
):
return (
v.lower().strip() in ["true", "1", "yes", "y"] if isinstance(v, str) else False
)
def capitalize(
v,
):
assert isinstance(v, str)
return v.capitalize()
def is_commit_hash(
v,
):
assert isinstance(v, str)
return re.match("[0-9a-f]{40}", v)
def go_module_version(
v,
):
if not v:
raise ValueError("Value is empty")
try:
i = v.rindex("/v")
v = v[i + 1 :]
except ValueError:
pass
if re.match("[0-9a-f]{40}", v):
return ""
if v.lower().startswith("v"):
v = v[1:]
if not v[0].isdigit():
return ""
version = v.split(".")
major = int(version[0])
if major > 1:
return "/v%s" % major
else:
return ""
def go_module_version_tag(
v,
):
if not v:
raise ValueError("Value is empty")
if not re.match("[0-9a-f]{40}", v) and not v.lower().startswith("v"):
return f"v{v}"
return v
def go_module_name(
v,
):
assert isinstance(v, str)
return re.sub("/v[0-9]+$", "", v)
def version_major(
v,
):
assert isinstance(v, str)
return version.parse(v).major
def version_minor(
v,
):
assert isinstance(v, str)
return version.parse(v).minor
def github_call_api(
url,
*,
headers=None,
call_timeout=60.0,
):
if headers is None:
headers = {}
headers = {
**headers,
**{
"Accept": "application/vnd.github.v3+json",
},
}
attempts = 0
while True:
response = requests.get(
url,
headers=headers,
timeout=call_timeout,
)
if (
response.status_code == HTTPStatus.FORBIDDEN
and "X-RateLimit-Remaining" in response.headers
):
# Check if the rate limit has been exceeded (GitHub-specific)
remaining = int(response.headers["X-RateLimit-Remaining"])
if remaining == 0:
reset_time = int(response.headers["X-RateLimit-Reset"])
current_time = time()
wait_time = reset_time - current_time + 5 # Add a buffer of 5 seconds
_logger.warning(
"GitHub overall rate limit exceeded, waiting %d seconds", wait_time
)
sleep(wait_time)
continue
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
# Handle generic rate limiting with exponential backoff
attempts += 1
if "Retry-After" in response.headers:
wait_time = int(response.headers["Retry-After"])
else:
wait_time = min(
2**attempts, 60
) # Exponential backoff capped at 60 seconds
_logger.warning("GitHub too many requests, waiting %d seconds", wait_time)
sleep(wait_time)
continue
response.raise_for_status()
return response.json()
@lru_cache
def github_get_latest_release(
repo,
owner="pulumi",
):
data = github_call_api(
f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
)
return data["tag_name"]
@lru_cache
def github_get_latest_release_commit(
repo,
owner="pulumi",
):
tag = github_get_latest_release(repo, owner)
tag_info = github_call_api(
url=f"https://api.github.com/repos/{owner}/{repo}/git/ref/tags/{tag}"
)
return tag_info["object"]["sha"]
@lru_cache
def github_download_file(
repo,
file_path,
owner="pulumi",
tag_name=None,
):
if not tag_name:
tag_name = github_get_latest_release(
repo=repo,
)
file_url = (
f"https://raw.githubusercontent.com/{owner}/{repo}/{tag_name}/{file_path}"
)
file_response = requests.get(file_url)
file_response.raise_for_status()
return file_response.text
def get_go_version():
"""
Returns the Go language version installed on the system as a version.Version object.
This function executes the 'go env GOVERSION' command using Popen and parses the version string
into a `packaging.version.Version` object.
Returns:
version.Version: The Go version as a `packaging.version.Version` object.
Raises:
Exception: If the command fails to execute or returns an error.
"""
process = Popen(
["go", "env", "GOVERSION"],
stdout=PIPE,
text=True,
)
stdout, stderr = process.communicate()
if process.returncode == 0:
go_version_str = stdout.strip().lstrip("go")
return version.parse(go_version_str)
else:
raise Exception(f"Failed to get go version: {stderr}")
@lru_cache
def parse_go_mod(
contents,
):
requirements = {}
replacements = {}
require_pattern = re.compile(r"^\s*require\s*(\(\s*)?$")
replace_pattern = re.compile(r"^\s*replace\s*(\(\s*)?$")
module_pattern = re.compile(
r"^\s*(?P<module>\S+)\s+(?P<version>\S+)(?:\s*//.*)?$"
) # Matches and ignores inline comments
replace_module_pattern = re.compile(
r"^\s*(?P<module>\S+)\s*=>\s*(?P<replace_module>\S+)(?:\s+(?P<version>\S+))?(?:\s*//.*)?$"
) # Matches and ignores inline comments
in_require_block = False
in_replace_block = False
for line in contents.splitlines():
line = line.strip() # noqa: PLW2901
if require_pattern.match(line):
in_require_block = True
continue
if replace_pattern.match(line):
in_replace_block = True
continue
if in_require_block and line == ")":
in_require_block = False
continue
if in_replace_block and line == ")":
in_replace_block = False
continue
if (in_require_block or line.startswith("require")) and not line.startswith(
"replace"
):
match = module_pattern.match(line.removeprefix("require").strip())
if match:
module = match.group("module")
version = match.group("version")
requirements[module] = version
if in_replace_block or line.startswith("replace"):
match = replace_module_pattern.match(line.removeprefix("replace").strip())
if match:
module = match.group("module")
replace_module = match.group("replace_module")
version = match.group("version")
replacements[module] = (replace_module, version)
return requirements, replacements
@lru_cache
def parse_terraform_brige_go_mod(
version=None,
):
go_mod = github_download_file(
repo="pulumi-terraform-bridge",
file_path="go.mod",
tag_name=version,
)
return parse_go_mod(contents=go_mod)
@lru_cache
def github_get_latest_semver_tag(
repo,
owner="pulumi",
prefix="",
):
base_url = f"https://api.github.com/repos/{owner}/{repo}/tags"
# Escape the prefix for use in regex
escaped_prefix = re.escape(prefix)
semver_pattern = re.compile(rf"^{escaped_prefix}(?P<version>\d+\.\d+\.\d+)$")
semver_tags = []
page = 1
while True:
url = f"{base_url}?page={page}&per_page=100"
tags = github_call_api(
url=url,
)
if not tags:
break
for tag in tags:
match = semver_pattern.match(tag["name"])
if match:
semver_tags.append((tag["name"], version.parse(match.group("version"))))
page += 1
if not semver_tags:
return None
# Sort by semantic versioning using the captured version part
semver_tags.sort(key=lambda t: t[1])
# The last item will be the latest version
return semver_tags[-1]
def get_terraform_plugin_sdkv2_version():
_, replacements = parse_terraform_brige_go_mod(
version=github_get_latest_release("pulumi-terraform-bridge"),
)
return replacements["github.com/hashicorp/terraform-plugin-sdk/v2"][1]
def get_pulumi_sdk_version():
requirements, _ = parse_terraform_brige_go_mod(
version=github_get_latest_release("pulumi-terraform-bridge"),
)
return requirements["github.com/pulumi/pulumi/sdk/v3"]
def get_terraform_bridge_pf_version():
result = github_get_latest_semver_tag(
repo="pulumi-terraform-bridge",
prefix="pf/v",
)
return f"v{result[1]!s}"
class LocalExtension(Extension):
def __init__(self, environment: Environment) -> None:
environment.globals["github_get_latest_release"] = github_get_latest_release
environment.globals[
"github_get_latest_release_commit"
] = github_get_latest_release_commit
environment.globals["get_go_version"] = get_go_version
environment.globals[
"get_terraform_plugin_sdkv2_version"
] = get_terraform_plugin_sdkv2_version
environment.globals[
"get_terraform_bridge_pf_version"
] = get_terraform_bridge_pf_version
environment.globals["get_pulumi_sdk_version"] = get_pulumi_sdk_version
environment.filters["truthy"] = is_truthy
environment.filters["capitalize"] = capitalize
environment.filters["go_module_version"] = go_module_version
environment.filters["go_module_name"] = go_module_name
environment.filters["go_module_version_tag"] = go_module_version_tag
environment.filters["version_major"] = version_major
environment.filters["version_minor"] = version_minor
environment.filters["is_commit_hash"] = is_commit_hash