forked from tmeckel/pulumi-tf-provider-cookiecutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_extensions.py
128 lines (90 loc) · 3.3 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
import re
from subprocess import PIPE, Popen
import requests
from jinja2.environment import Environment
from jinja2.ext import Extension
from packaging import version
def is_truthy(v):
return v.lower().strip() in ["true", "1", "yes", "y"]
def capitalize(v):
return v.capitalize()
def is_commit_hash(v):
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):
return re.sub("/v[0-9]+$", "", v)
def version_major(v):
return version.parse(v).major
def version_minor(v):
return version.parse(v).minor
def get_latest_release(repo, owner="pulumi"):
release_url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
response = requests.get(release_url)
data = response.json()
return data["tag_name"]
def get_latest_release_commit(repo, owner="pulumi"):
tag = get_latest_release(repo, owner)
tag_info_url = f"https://api.github.com/repos/{owner}/{repo}/git/ref/tags/{tag}"
response = requests.get(tag_info_url)
tag_info = response.json()
return tag_info["object"]["sha"]
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}")
class LocalExtension(Extension):
def __init__(self, environment: Environment) -> None:
super().__init__(environment)
environment.globals["get_latest_release"] = get_latest_release
environment.globals["get_latest_release_commit"] = get_latest_release_commit
environment.globals["get_go_version"] = get_go_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