-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcommon.py
65 lines (47 loc) · 1.94 KB
/
common.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
from typing import Any, TypedDict
import json
from pathlib import Path
import sys
import subprocess
class RecipeFailure(TypedDict):
failed_at: str
def load_failed_compatibility(file_path: Path) -> dict[str, RecipeFailure]:
if file_path.exists():
with file_path.open("r") as file:
return dict(json.load(file))
return {}
def save_failed_compatibility(file_path: Path, data: dict[str, RecipeFailure]) -> None:
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
with file_path.open("w") as file:
json.dump(data, file, indent=4)
def eprint(*args: Any, **kwargs: Any) -> None:
print(*args, file=sys.stderr, **kwargs)
def commit_push_changes(message: str, branch_name: str) -> None:
"""
Commit and push changes to the specified branch with a given commit message.
If there are no changes, do nothing.
"""
# Switch to branch
run_command(["git", "switch", branch_name])
# Check if there are changes to commit
result = run_command_unchecked(["git", "diff-index", "--quiet", "HEAD"])
if result.returncode == 0:
eprint("No changes to commit.")
return
# Commit, pull and push the changes
run_command(["git", "pull", "origin", branch_name])
run_command(["git", "commit", "--message", message, "--no-verify"])
run_command(["git", "push", "--set-upstream", "origin", branch_name])
def run_command_unchecked(command: list[str]) -> subprocess.CompletedProcess[Any]:
eprint(f'Run command: {" ".join(command)}')
result = subprocess.run(command, capture_output=True, text=True)
return result
def run_command(command: list[str]) -> subprocess.CompletedProcess[Any]:
result = run_command_unchecked(command)
if result.returncode != 0:
eprint("Command failed")
print(f"stdout: {result.stdout}")
eprint(f"stderr: {result.stderr}")
sys.exit(result.returncode)
return result