|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.13" |
| 3 | +# dependencies = [ |
| 4 | +# "ruyaml", |
| 5 | +# ] |
| 6 | +# /// |
| 7 | + |
1 | 8 | import os |
2 | 9 | from datetime import date, timedelta |
| 10 | +from io import StringIO |
3 | 11 | from pathlib import Path |
4 | 12 | from typing import Dict, NotRequired, TypedDict |
5 | 13 |
|
6 | 14 | import ruyaml |
7 | 15 |
|
| 16 | + |
8 | 17 | class RefDetails(TypedDict): |
9 | 18 | """ |
10 | 19 | Type definition for reference details of GitHub Actions for actions.yml |
@@ -66,6 +75,11 @@ def write_yaml(path: Path, yaml_dict: dict | list): |
66 | 75 | yaml = ruyaml.YAML() |
67 | 76 | yaml.dump(yaml_dict, file) |
68 | 77 |
|
| 78 | +def to_yaml_string(yaml_dict: dict | list): |
| 79 | + yaml = ruyaml.YAML() |
| 80 | + stream = StringIO() |
| 81 | + yaml.dump(yaml_dict, stream) |
| 82 | + return stream.getvalue() |
69 | 83 |
|
70 | 84 | def write_str(path: Path, content: str): |
71 | 85 | with open(path, "w") as file: |
@@ -182,10 +196,9 @@ def update_actions(dummy_path: Path, actions_path: Path): |
182 | 196 | actions: ActionsYAML = load_yaml(actions_path) |
183 | 197 |
|
184 | 198 | update_refs(steps, actions) |
185 | | - gha_print(yaml.safe_dump(actions), "Generated List") |
| 199 | + gha_print(to_yaml_string(actions), "Generated List") |
186 | 200 | write_yaml(actions_path, actions) |
187 | 201 |
|
188 | | - |
189 | 202 | def create_pattern(actions: ActionsYAML) -> list[str]: |
190 | 203 | """ |
191 | 204 | Create a pattern list of valid action references. |
@@ -218,8 +231,8 @@ def update_patterns(pattern_path: Path, list_path: Path): |
218 | 231 | """ |
219 | 232 | actions: ActionsYAML = load_yaml(list_path) |
220 | 233 | patterns = create_pattern(actions) |
221 | | - comment = f"# This file was generated from {pattern_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n" |
222 | | - patterns_str = comment + yaml.safe_dump(patterns) |
| 234 | + comment = f"# This file was generated from {list_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n" |
| 235 | + patterns_str = comment + to_yaml_string(patterns) |
223 | 236 | gha_print(patterns_str, "Generated Patterns") |
224 | 237 | write_str(pattern_path, patterns_str) |
225 | 238 |
|
@@ -273,5 +286,5 @@ def clean_actions(actions_path: Path): |
273 | 286 | """ |
274 | 287 | actions: ActionsYAML = load_yaml(actions_path) |
275 | 288 | remove_expired_refs(actions) |
276 | | - gha_print(yaml.safe_dump(actions), "Cleaned Actions") |
| 289 | + gha_print(to_yaml_string(actions), "Cleaned Actions") |
277 | 290 | write_yaml(actions_path, actions) |
0 commit comments