forked from AleksaC/circleci-cli-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-new-versions.py
executable file
·171 lines (137 loc) · 4.69 KB
/
add-new-versions.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
#!/usr/bin/env python
import ast
import base64
import io
import json
import os
import re
import tokenize
from urllib.request import Request
from urllib.request import urlopen
from typing import Dict
from typing import Iterable
from typing import List
from typing import Tuple
from typing import Union
def get(url: str, headers: dict) -> dict:
req = Request(url, headers=headers)
resp = urlopen(req, timeout=30)
return json.loads(resp.read())
def get_releases() -> List[str]:
gh_token = os.environ["GH_TOKEN"]
auth = base64.b64encode(f"AleksaC:{gh_token}".encode()).decode()
base_url = "https://api.github.com/repos/{}/{}/{}"
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": f"Basic {auth}",
}
circleci_cli_releases = get(
base_url.format("CircleCI-Public", "circleci-cli", "releases"), headers=headers
)
hook_tags = get(
base_url.format("AleksaC", "circleci-validation-pre-commit", "tags"),
headers=headers,
)
new_releases = []
latest = hook_tags[0]["name"]
for release in circleci_cli_releases:
version = release["tag_name"]
if version == latest:
break
new_releases.append(version)
return new_releases
def get_checksums(version: str) -> dict:
checksum_url = (
f"https://github.com/CircleCI-Public/circleci-cli/releases/download/"
f"v{version}/circleci-cli_{version}_checksums.txt"
)
req = Request(checksum_url)
resp = urlopen(req, timeout=30)
checksums = resp.read().decode()
checksum_re = re.compile(
r"(?P<sha>[A-Fa-f0-9]{64}) "
r"(?P<file>circleci-cli_[0-9]+\.[0-9]+\.[0-9]+_"
r"(?P<platform>darwin|windows|linux)_amd64(\.tar\.gz|\.zip))\n"
)
archive_sha = {}
for match in re.findall(checksum_re, checksums):
sha, archive, platform, _ = match
archive_sha[platform] = (archive, sha)
return archive_sha
def update_file(
file_path: str, replacements: Iterable[Tuple[Union[re.Pattern, str], str]]
) -> None:
with open(file_path, "r+") as f:
old = f.read()
new = old
for pattern, replacement in replacements:
new = re.sub(
pattern,
replacement,
new,
)
f.seek(0)
f.write(new)
f.truncate()
def update_file_tokenize(file_path: str) -> str:
with open(file_path, "r+") as f:
contents = io.StringIO(f.read())
tokens = tokenize.generate_tokens(contents.readline)
lines = []
while True:
token = next(tokens)
if token.type == 1 and token.string == "ARCHIVE_SHA256":
while True:
lines.append(token.line)
next_token = next(tokens)
while next_token.line == token.line:
next_token = next(tokens)
token = next_token
if token.type == 54 and token.string == "}":
lines.append(token.string)
return "".join(lines)
def update_archive_sha(version: str) -> None:
archive_sha256_str = update_file_tokenize("setup.py")
archive_sha256_dict: Dict[str, Tuple[str, str]] = ast.literal_eval(
archive_sha256_str[archive_sha256_str.find("{") :]
)
checksums = get_checksums(version)
archive_sha256_new = archive_sha256_str
for platform in checksums:
old_archive, old_sha = archive_sha256_dict[
"win32" if platform == "windows" else platform
]
new_archive, new_sha = checksums[platform]
archive_sha256_new = archive_sha256_new.replace(old_archive, new_archive)
archive_sha256_new = archive_sha256_new.replace(old_sha, new_sha)
with open("setup.py", "r+") as f:
contents = f.read()
f.seek(0)
f.write(contents.replace(archive_sha256_str, archive_sha256_new))
f.truncate()
def update_version(version: str) -> None:
update_archive_sha(version)
update_file(
"setup.py",
[
(
r"CIRCLECI_CLI_VERSION = \"[0-9]+\.[0-9]+\.[0-9]+\"\n",
f'CIRCLECI_CLI_VERSION = "{version}"\n',
)
],
)
update_file(
"README.md",
[
(r"rev: v[0-9]+\.[0-9]+\.[0-9]+", f"rev: v{version}"),
(r"@v[0-9]+\.[0-9]+\.[0-9]+", f"@v{version}"),
],
)
def push_tag(version: str) -> None:
os.system(f"./tag.sh {version}")
if __name__ == "__main__":
releases = get_releases()
for release in reversed(releases):
print(f"Adding new release: {release}")
update_version(release.replace("v", ""))
push_tag(release)