Skip to content

Commit

Permalink
feat: 新增 开发工具/git连续尝试工具 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna-Grace authored Jun 30, 2024
1 parent e43332f commit 93ff812
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Tools/开发工具/Script/ffdev.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ if ($program -eq "总调用") {
$pythonScript = Join-Path $parentDir "git\连续push尝试.py"
} elseif ($program -eq "连续pull") {
$pythonScript = Join-Path $parentDir "git\连续pull尝试.py"
} elseif ($program -eq "git连续尝试") {
$pythonScript = Join-Path $parentDir "git\git连续尝试.py"
} else {
Write-Warning "无效的程序调用"
Write-Output "可用程序: [目录复制] [参数查重] [非UTF-8编码] [尾随空格] [需求生成] [代码行数] [账号切换] [连续push] [连续pull]"
Write-Output "可用程序: [目录复制] [参数查重] [非UTF-8编码] [尾随空格] [需求生成] [代码行数] [账号切换] [连续push] [连续pull] [git连续尝试]"
$flag = 1
}

Expand Down
93 changes: 93 additions & 0 deletions Tools/开发工具/git/git连续尝试.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import time
import subprocess
import tkinter as tk
from plyer import notification
from tkinter import filedialog
from colorama import init, Fore

# --- init ---
init(autoreset=True)
root = tk.Tk()
root.withdraw()
# ------------

def run_commits(working_dir, command):
result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=working_dir)
if result.returncode == 0:
return "successful"
else:
return result.stderr

def is_network_error(stderr): # 判断错误类型
network_error_keywords = [
"unable to access",
"Could not resolve host",
"Failed to connect",
"Operation timed out",
"early EOF",
"RPC failed"
]
for keyword in network_error_keywords:
if keyword in stderr:
return True
return False

def main():
print(f"{Fore.BLUE}?{Fore.RESET} 请选择仓库目录", end="")
working_dir = filedialog.askdirectory()
print(f"\r{Fore.GREEN}{Fore.RESET} 选择的仓库目录: {working_dir}")

command = input("请输入需要执行的命令: ")

while True:
time_counter = input("请输入每次尝试的间隔(秒):")
# 检测适用性
try:
time_counter = int(time_counter)
if time_counter <= 1:
print(f"{Fore.RED}{Fore.RESET} 间隔过短!请指定一个大于1的值!")
else:
print(f"{Fore.GREEN}{Fore.RESET} 已设置间隔时间: {time_counter}")
break
except ValueError as e:
print(f"{Fore.RED}{Fore.RESET} 输入的值不合法,必须为一个正整数!")

counter = 0

while True:
counter += 1
output = run_commits(working_dir, command)
if "successful" in output:
print(f"{Fore.GREEN}{Fore.RESET} 运行成功!!")
notification.notify(
title='芙芙工具箱 | git连续尝试',
message=f'运行成功',
timeout=10
)
break
elif is_network_error(output):
print(f"{Fore.YELLOW}{Fore.RESET}{Fore.BLUE}{counter}{Fore.RESET} 次运行尝试失败")
print(f"原因: {Fore.RED}{output}{Fore.RESET}")
temp = time_counter
for i in range(time_counter, 0, -1):
print(f"\r{i}秒后重试...", end="")
time.sleep(1)
print("\r重试中...")
time_counter = temp # 还原秒数设置
else:
print(f"{Fore.RED}{Fore.RESET}{Fore.BLUE}{counter}{Fore.RESET} 次运行尝试失败,出现了非已知网路问题\n{Fore.BLUE}[提示]{Fore.RESET} 如果你确定这是网络问题,请提交issue或者PR,感谢!")
print(f"原因: {Fore.RED}{output}{Fore.RESET}")
notification.notify(
title='芙芙工具箱 | git连续尝试',
message=f'检测到非网络错误,请注意!',
timeout=10
)
t = input("请确认是否继续尝试: ")
if t.lower() not in ["y", "yes", "是", "继续", "确认"]:
print(f"{Fore.RED}{Fore.RESET} 由于检测到非网络错误,已终止程序")
break
print(f"{Fore.BLUE}[info]{Fore.RESET} 一共执行了 {Fore.BLUE}{counter}{Fore.RESET} 次命令")

if __name__ == "__main__":
main()
input ("按Enter键退出...")

0 comments on commit 93ff812

Please sign in to comment.