forked from frequenz-floss/frequenz-repo-config-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookiecutter-migrate.template.py
More file actions
318 lines (247 loc) · 9.53 KB
/
cookiecutter-migrate.template.py
File metadata and controls
318 lines (247 loc) · 9.53 KB
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
#!/usr/bin/env python3
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
"""Script to migrate existing projects to new versions of the cookiecutter template.
This script migrates existing projects to new versions of the cookiecutter
template, removing the need to completely regenerate the project from
scratch.
To run it, the simplest way is to fetch it from GitHub and run it directly:
curl -sSL https://raw.githubusercontent.com/frequenz-floss/frequenz-repo-config-python/<tag>/cookiecutter/migrate.py | python3
Make sure to replace the `<tag>` to the version you want to migrate to in the URL.
For jumping multiple versions you should run the script multiple times, once
for each version.
And remember to follow any manual instructions for each run.
""" # noqa: E501
# pylint: disable=too-many-lines, too-many-locals, too-many-branches
import hashlib
import json
import os
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Any, SupportsIndex
_manual_steps: list[str] = [] # pylint: disable=invalid-name
def main() -> None:
"""Run the migration steps."""
# Add a separation line like this one after each migration step.
print("=" * 72)
print()
if _manual_steps:
print(
"\033[5;33m⚠️⚠️⚠️\033[0;33m Remember to check the manual steps: \033[5;33m⚠️⚠️⚠️\033[0m"
)
for n, step in enumerate(_manual_steps, start=1):
print(f"\033[5;33m⚠️⚠️⚠️ \033[0;33m{n}. {step}\033[0m")
print()
print(
"\033[5;31m❌\033[0;31m Migration script finished but requires manual "
"intervention \033[5;31m❌\033[0m"
)
print()
sys.exit(len(_manual_steps))
print("\033[0;32m ✅ Migration script finished successfully ✅\033[0m")
print()
return project_type
def apply_patch(patch_content: str) -> None:
"""Apply a patch using the patch utility."""
subprocess.run(["patch", "-p1"], input=patch_content.encode(), check=True)
def replace_file_atomically( # noqa; DOC501, DOC503
filepath: str | Path, new_content: str
) -> None:
"""Replace a file atomically with the given content.
The replacement is done atomically by writing to a temporary file in the
same directory and then moving it to the target location.
Args:
filepath: The path to the file to replace.
new_content: The content to write to the file.
"""
if isinstance(filepath, str):
filepath = Path(filepath)
tmp_dir = filepath.parent
tmp_dir.mkdir(parents=True, exist_ok=True)
# pylint: disable-next=consider-using-with
tmp = tempfile.NamedTemporaryFile(mode="w", dir=tmp_dir, delete=False)
try:
st = None
try:
st = os.stat(filepath)
except FileNotFoundError:
st = None
tmp.write(new_content)
tmp.flush()
os.fsync(tmp.fileno())
tmp.close()
if st is not None:
os.chmod(tmp.name, st.st_mode)
os.replace(tmp.name, filepath)
except BaseException:
tmp.close()
os.unlink(tmp.name)
raise
def replace_file_contents_atomically( # noqa; DOC501
filepath: str | Path,
old: str,
new: str,
count: SupportsIndex = -1,
*,
content: str | None = None,
) -> None:
"""Replace a file atomically with new content.
The replacement is done atomically by writing to a temporary file and
then moving it to the target location.
Args:
filepath: The path to the file to replace.
old: The string to replace.
new: The string to replace it with.
count: The maximum number of occurrences to replace. If negative, all occurrences are
replaced.
content: The content to replace. If not provided, the file is read from disk.
"""
if isinstance(filepath, str):
filepath = Path(filepath)
if content is None:
content = filepath.read_text(encoding="utf-8")
replace_file_atomically(filepath, content.replace(old, new, count))
def calculate_file_sha256_skip_lines(filepath: Path, skip_lines: int) -> str | None:
"""Calculate SHA256 of file contents excluding the first N lines.
Args:
filepath: Path to the file to hash
skip_lines: Number of lines to skip at the beginning
Returns:
The SHA256 hex digest, or None if the file doesn't exist
"""
if not filepath.exists():
return None
# Read file and normalize line endings to LF
content = filepath.read_text(encoding="utf-8").replace("\r\n", "\n")
# Skip first N lines and ensure there's a trailing newline
remaining_content = "\n".join(content.splitlines()[skip_lines:]) + "\n"
return hashlib.sha256(remaining_content.encode()).hexdigest()
def find_ruleset(name: str) -> dict[str, Any] | None:
"""Find a repository ruleset by name using the GitHub API.
Args:
name: The name of the ruleset to search for.
Returns:
The ruleset summary dict (id, name, …) if found, or ``None`` if not
found or if the API call failed (a diagnostic is printed in the latter
case).
"""
try:
stdout = subprocess.check_output(
["gh", "api", "repos/:owner/:repo/rulesets"],
text=True,
stderr=subprocess.PIPE,
)
except FileNotFoundError:
print(" gh CLI not found; cannot query rulesets via the GitHub API.")
return None
except subprocess.CalledProcessError as exc:
print(f" Failed to list rulesets: {exc.stderr.strip()}")
return None
rulesets: list[dict[str, Any]] = json.loads(stdout)
return next((r for r in rulesets if r.get("name") == name), None)
def get_ruleset(ruleset: str | int) -> dict[str, Any] | None:
"""Fetch the full details of a repository ruleset by name or ID.
Args:
ruleset: The ruleset name (``str``) or numeric ruleset ID (``int``).
Returns:
The full ruleset dict, or ``None`` if the ruleset could not be found
or the API call failed (a diagnostic is printed).
"""
ruleset_id = ruleset
if isinstance(ruleset, str):
entry = find_ruleset(ruleset)
if entry is None:
return None
ruleset_id = entry["id"]
try:
stdout = subprocess.check_output(
["gh", "api", f"repos/:owner/:repo/rulesets/{ruleset_id}"],
text=True,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError as exc:
print(f" Failed to fetch ruleset {ruleset_id}: {exc.stderr.strip()}")
return None
return json.loads(stdout) # type: ignore[no-any-return]
def update_ruleset(ruleset_id: int, config: dict[str, Any]) -> bool:
"""Update a repository ruleset via the GitHub API.
Only ``name``, ``target``, ``enforcement``, ``conditions``, ``rules``,
and ``bypass_actors`` are sent (explicit allowlist to avoid sending
read-only fields back to the API).
Args:
ruleset_id: The numeric ruleset ID to update.
config: The full ruleset dict (as returned by :func:`get_ruleset`)
with the desired changes already applied in-memory.
Returns:
``True`` on success, ``False`` if the API call failed (a diagnostic
is printed).
"""
payload: dict[str, Any] = {
"name": config["name"],
"target": config["target"],
"enforcement": config["enforcement"],
"conditions": config["conditions"],
"rules": config["rules"],
}
if "bypass_actors" in config:
payload["bypass_actors"] = config["bypass_actors"]
try:
subprocess.check_output(
[
"gh",
"api",
"-X",
"PUT",
f"repos/:owner/:repo/rulesets/{ruleset_id}",
"--input",
"-",
],
input=json.dumps(payload),
text=True,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError as exc:
print(f" Failed to update ruleset {ruleset_id}: {exc.stderr.strip()}")
return False
return True
def get_ruleset_settings_url() -> str | None:
"""Return the URL to the repository's ruleset settings page.
Returns:
The URL as a string, or ``None`` if it could not be determined.
"""
try:
stdout = subprocess.check_output(
["gh", "repo", "view", "--json", "owner,name"],
text=True,
stderr=subprocess.PIPE,
)
info: dict[str, Any] = json.loads(stdout)
org = info["owner"]["login"]
repo = info["name"]
return f"https://github.com/{org}/{repo}/settings/rules"
except (subprocess.CalledProcessError, KeyError, json.JSONDecodeError):
return None
def read_cookiecutter_str_var(name: str) -> str | None:
"""Read a cookiecutter variable from the replay file."""
replay_path = Path(".cookiecutter-replay.json")
if not replay_path.exists():
return None
try:
data = json.loads(replay_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
return None
cookiecutter_data = data.get("cookiecutter")
if not isinstance(cookiecutter_data, dict):
return None
value = cookiecutter_data.get(name)
if not isinstance(value, str):
return None
return value
def manual_step(message: str) -> None:
"""Print a manual step message in yellow."""
_manual_steps.append(message)
print(f"\033[0;33m>>> {message}\033[0m")
if __name__ == "__main__":
main()