-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
142 lines (112 loc) · 5.54 KB
/
github.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
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
# Author: Acer Zhang
# Datetime: 2021/9/28
# Copyright belongs to the author.
# Please indicate the source for reprinting.
import os
import re
import requests
from qgui import CreateQGUI
from qgui.banner_tools import GitHub
from qgui.notebook_tools import *
from qwebsite.base import GitHubOptimizer
from qwebsite.flags import *
from qwebsite import __version__
ISO_LIST = ["https://hub.fastgit.org",
"https://github.aaakk.us.kg.cnpmjs.org",
"https://git.sdut.me",
"https://github.wuyanzheshui.workers.dev"]
def set_iso(url):
out = os.popen(f"git config --global url.{url}.insteadof https://github.com").read()
if out:
print("执行情况如下:\n", out)
def ping_iso(url):
print(f"开始测试{url}镜像源")
try:
ping_time = requests.get(url, timeout=4).elapsed.total_seconds()
if ping_time < 1:
print(f"镜像源\t{url}网络质量较好")
elif ping_time < 2:
print(f"镜像源\t{url}网络质量一般")
else:
print(f"镜像源\t{url}网络质量较差")
print(f"镜像源\t{url}的响应时间为{ping_time:.4f}s")
except requests.exceptions.ConnectTimeout:
print(f"镜像源\t{url}长时间无响应")
except Exception as e:
print(e, "发生了异常,可能无法使用该镜像源")
def clean_iso(url):
out = os.popen(f"git config --global --unset url.{url}.insteadof").read()
if out:
print("执行情况如下:\n", out)
def get_iso():
out = os.popen(f"git config --global -l").read()
url = re.findall(".*url\.(.*)\.insteadof.*", out)
if url:
url = url[0]
else:
url = "GitHub官方"
return url
def ping_callback(args):
url = args["下拉选择框"].get()
ping_iso(url=url)
print("指令已完成")
def clean_callback(args):
clean_iso(get_iso())
args["当前镜像源情况"].set(get_iso())
print("清空完毕")
def set_callback(args):
url = args["下拉选择框"].get()
clean_iso(get_iso())
set_iso(url)
args["当前镜像源情况"].set(get_iso())
print("设置完毕")
def set_hosts(args):
GitHubOptimizer()
print("指令已完成")
def reset_hosts(args):
GitHubOptimizer(RESET_FLAG)
print("指令已完成")
class QWebSiteOptGUI:
def __init__(self, title="QWebSiteOptimizer - GitHub"):
# 基础GUI
self.gui = CreateQGUI(title=title)
self.gui.add_banner_tool(GitHub("https://github.com/QPT-Family/QWebSiteOptimizer"))
self.gui.set_navigation_about(author="GT_ZhangAcer",
version=__version__,
github_url="https://github.com/QPT-Family/QWebSiteOptimizer")
# DNS解析选优功能区
set_dns_opt = BaseButton(bind_func=set_hosts, text="开始DNS选优", checked_text="正在选优ing", style="info")
clear_dns_opt = BaseButton(bind_func=reset_hosts, text="清除DNS选优", checked_text="正在清除ing", style="success")
self.gui.add_notebook_tool(HorizontalToolsCombine(tools=[set_dns_opt, clear_dns_opt],
title="[ROOT] DNS解析选优 - 需要管理员权限",
text="通过对多个DNS返回的结果进行筛选,匹配当前网络环境下最快的解析地址并设置Host文件,"
"从而达到加速目的。\n"
"该方式虽然不如VPN稳定可靠,但在短期内我们可通过该加速效果来达到优化GitHub浏览速度等问题。\n"
"建议在阅览GitHub出现卡顿时重新进行设置,若需要Clone大型项目,可切换至镜像源模式进行解决。"))
# 镜像源模式功能区
act = get_iso()
if not act:
act = "GitHub官方"
iso_act = Label(name="当前镜像源情况", text=act, title="当前镜像源:")
iso_comb = Combobox(name="下拉选择框", title="选择镜像源:", options=ISO_LIST)
iso_ping = BaseButton(bind_func=ping_callback, text="Ping镜像源", checked_text="正在测试ing", style="info")
iso_clear = BaseButton(bind_func=clean_callback, text="清除镜像源", checked_text="正在清除ing", style="success")
iso_set = BaseButton(bind_func=set_callback, text="设置镜像源", checked_text="正在设置ing", style="danger")
iso_btn = HorizontalToolsCombine(tools=[iso_comb, iso_ping, iso_set, iso_clear])
self.gui.add_notebook_tool(HorizontalFrameCombine(tools=[iso_act, iso_btn],
title="镜像源模式",
text="通过对GitHub默认源进行替换,从而快速从镜像源中加载我们需要的数据,提升Clone的效率。\n"
"但由于镜像源并非官方源,强烈、非常强烈建议我们仅在需要Clone公开项目时开启该模式,"
"而在Push以及\n私有项目的操作中保持关闭状态,避免出现异常情况!"))
def run(self):
self.gui.run()
if __name__ == '__main__':
# _u = "https://hub.fastgit.org"
# set_iso(_u)
# _a = get_iso()
# print(_a)
# clean_iso(_a[0])
# _b = get_iso()
# print("b:", _b)
_a = QWebSiteOptGUI()
_a.run()