-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
178 lines (153 loc) · 4.93 KB
/
main.py
File metadata and controls
178 lines (153 loc) · 4.93 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
import os
import traceback
from subprocess import Popen, PIPE
from pathlib import Path
from typing import Optional, Union
from github import Github, Auth
from dotenv import load_dotenv
load_dotenv() # take environment variables
def parse_input() -> dict:
gh_input = {
"username": "Scientific-Python-Translations",
# Provided by organization secrets
"token": os.environ["TOKEN"],
# Provided by user action input
"translations_repo": os.environ["INPUT_TRANSLATIONS-REPO"],
"translations_ref": os.environ["INPUT_TRANSLATIONS-REF"],
# Provided by gpg action based on organization secrets
"name": os.environ["GPG_NAME"],
"email": os.environ["GPG_EMAIL"],
"run_local": os.environ.get("RUN_LOCAL", "False").lower() == "true",
}
return gh_input
def run(
cmds: list[str], cwd: Optional[Union[str, Path]] = None
) -> tuple[str, str, int]:
"""Run a command in the shell and print the standard output, error and return code.
Parameters
----------
cmds : list
List of commands to run.
cwd : str, optional
Current working directory to run the command in. If None, use the current working directory.
Returns
-------
out : str
Output of the command.
err : str
Error of the command.
rc : int
Return code of the command.
"""
p = Popen(cmds, stdout=PIPE, stderr=PIPE, cwd=cwd)
out, err = p.communicate()
stdout = out.decode()
stderr = err.decode()
print("\n\n\nCmd: \n" + " ".join(cmds))
print("Cwd: \n", cwd or os.getcwd())
print("Out: \n", stdout)
print("Err: \n", stderr)
print("Code: \n", p.returncode)
return stdout, stderr, p.returncode
def configure_git_and_checkout_repos(
username: str,
token: str,
translations_repo: str,
translations_ref: str,
name: str,
email: str,
) -> None:
"""
Configure git information and checkout repositories.
Parameters
----------
username : str
Username of the source repository.
token : str
Personal access token of the source repository.
translations_repo : str
.
translations_ref : str
.
name : str
Name of the bot account.
email : str
Email of the bot account.
"""
os.environ["GITHUB_TOKEN"] = token
print("\n\n### Configure git information and checkout repositories")
base_path = Path(os.getcwd())
base_translations_path = base_path / translations_repo.split("/")[-1]
print("\n\nBase path:\n", base_path)
print("\n\nBase translations path:\n", base_translations_path)
if translations_ref:
cmds = [
"git",
"clone",
"-b",
translations_ref,
f"https://{username}:{token}@github.com/{translations_repo}.git",
]
else:
cmds = [
"git",
"clone",
f"https://{username}:{token}@github.com/{translations_repo}.git",
]
run(cmds, cwd=base_path)
# Configure git information
for path in [base_translations_path]:
run(["git", "config", "user.name", f'"{name}"'], cwd=path)
run(["git", "config", "user.email", f'"{email}"'], cwd=path)
def clean_up_branches(
token: str,
translations_repo: str,
):
"""
Clean up branches in the translations repository.
Parameters
----------
token : str
Personal access token of the source repository.
translations_repo : str
Translations repository name.
"""
base_path = Path(os.getcwd())
base_translations_path = base_path / translations_repo.split("/")[-1]
auth = Auth.Token(token)
g = Github(auth=auth)
branches = g.get_repo(translations_repo).get_branches()
branch_names = [branch.name for branch in branches]
branch_names.remove("main")
for branch in branch_names:
if branch.startswith(
("content-sync-", "l10n_main", "add/translators-file", "add/status-file")
):
print(f"### Deleting branch {branch}")
run(["git", "branch", "-d", branch], cwd=base_translations_path)
run(
["git", "push", "origin", "--delete", branch],
cwd=base_translations_path,
)
def main() -> None:
"""Main function to run the script."""
try:
gh_input = parse_input()
configure_git_and_checkout_repos(
username=gh_input["username"],
token=gh_input["token"],
translations_repo=gh_input["translations_repo"],
translations_ref=gh_input["translations_ref"],
name=gh_input["name"],
email=gh_input["email"],
)
clean_up_branches(
token=gh_input["token"],
translations_repo=gh_input["translations_repo"],
)
except Exception as e:
print("Error: ", e)
print(traceback.format_exc())
raise e
if __name__ == "__main__":
main()