Skip to content

Commit 927c775

Browse files
committed
f
1 parent 50520f6 commit 927c775

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

scripts/translator.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@
1818
DISALLOWED_SPECIAL = "<|endoftext|>"
1919
REPLACEMENT_TOKEN = "<END_OF_TEXT>"
2020

21+
def run_git_command_with_retry(cmd, max_retries=1, delay=5, **kwargs):
22+
"""
23+
Run a git command with retry logic.
24+
25+
Args:
26+
cmd: Command to run (list or string)
27+
max_retries: Number of additional retries after first failure
28+
delay: Delay in seconds between retries
29+
**kwargs: Additional arguments to pass to subprocess.run
30+
31+
Returns:
32+
subprocess.CompletedProcess result
33+
"""
34+
last_exception = None
35+
for attempt in range(max_retries + 1):
36+
try:
37+
result = subprocess.run(cmd, **kwargs)
38+
return result
39+
except Exception as e:
40+
last_exception = e
41+
if attempt < max_retries:
42+
print(f"Git command failed (attempt {attempt + 1}/{max_retries + 1}): {e}")
43+
print(f"Retrying in {delay} seconds...")
44+
time.sleep(delay)
45+
else:
46+
print(f"Git command failed after {max_retries + 1} attempts: {e}")
47+
48+
# If we get here, all attempts failed, re-raise the last exception
49+
if last_exception:
50+
raise last_exception
51+
else:
52+
raise RuntimeError("Unexpected error in git command retry logic")
53+
2154
def _sanitize(text: str) -> str:
2255
"""
2356
Replace the reserved tiktoken token with a harmless placeholder.
@@ -42,7 +75,7 @@ def check_git_dir(path):
4275
def get_branch_files(branch):
4376
"""Get a list of all files in a branch."""
4477
command = f"git ls-tree -r --name-only {branch}"
45-
result = subprocess.run(command.split(), stdout=subprocess.PIPE)
78+
result = run_git_command_with_retry(command.split(), stdout=subprocess.PIPE)
4679
files = result.stdout.decode().splitlines()
4780
return set(files)
4881

@@ -63,12 +96,12 @@ def cp_translation_to_repo_dir_and_check_gh_branch(branch, temp_folder, translat
6396
Get the translated files from the temp folder and copy them to the repo directory in the expected branch.
6497
Also remove all the files that are not in the master branch.
6598
"""
66-
branch_exists = subprocess.run(['git', 'show-ref', '--verify', '--quiet', 'refs/heads/' + branch])
99+
branch_exists = run_git_command_with_retry(['git', 'show-ref', '--verify', '--quiet', 'refs/heads/' + branch])
67100
# If branch doesn't exist, create it
68101
if branch_exists.returncode != 0:
69-
subprocess.run(['git', 'checkout', '-b', branch])
102+
run_git_command_with_retry(['git', 'checkout', '-b', branch])
70103
else:
71-
subprocess.run(['git', 'checkout', branch])
104+
run_git_command_with_retry(['git', 'checkout', branch])
72105

73106
# Get files to delete
74107
files_to_delete = get_unused_files(branch)
@@ -108,7 +141,7 @@ def commit_and_push(translate_files, branch):
108141
]
109142

110143
for cmd in commands:
111-
result = subprocess.run(cmd, capture_output=True, text=True)
144+
result = run_git_command_with_retry(cmd, capture_output=True, text=True)
112145

113146
# Print stdout and stderr (if any)
114147
if result.stdout:

0 commit comments

Comments
 (0)