Skip to content

Commit

Permalink
feat(system_source): 增加自动测速选择镜像源
Browse files Browse the repository at this point in the history
  • Loading branch information
fishros committed Sep 4, 2024
1 parent 2c2614b commit d25178c
Showing 1 changed file with 174 additions and 120 deletions.
294 changes: 174 additions & 120 deletions tools/tool_config_system_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,51 @@



ros_mirror_dic = {
"tsinghua":{"ROS1":"http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/","ROS2":"http://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu/"},
"huawei":{"ROS1":"https://repo.huaweicloud.com/ros/ubuntu/","ROS2":"https://repo.huaweicloud.com/ros2/ubuntu/"},
"packages.ros":{"ROS1":"http://packages.ros.org/ros/ubuntu/","ROS2":"http://packages.ros.org/ros2/ubuntu/"},
"repo-ros2":{"ROS2":"http://repo.ros2.org/ubuntu/"}
}


ros_dist_dic = {
'artful':{"packages.ros"},
'bionic':{"tsinghua","huawei","packages.ros"},
'buster':{"packages.ros"},
'cosmic':{"packages.ros"},
'disco':{"packages.ros"},
'eoan':{"packages.ros"},
'focal':{"tsinghua","huawei","packages.ros"},
'jessie':{"tsinghua","huawei","packages.ros"},
'lucid':{"packages.ros"},
'maverick':{"packages.ros"},
'natty':{"packages.ros"},
'oneiric':{"packages.ros"},
'precise':{"packages.ros"},
'quantal':{"packages.ros"},
'raring':{"packages.ros"},
'saucy':{"packages.ros"},
'stretch':{"tsinghua","huawei","packages.ros"},
'trusty':{"tsinghua","huawei","packages.ros"},
'utopic':{"packages.ros"},
'vivid':{"packages.ros"},
'wheezy':{"packages.ros"},
'wily':{"packages.ros"},
'xenial':{"tsinghua","huawei","packages.ros"},
'yakkety':{"packages.ros"},
'zesty':{"packages.ros"},
}


ros2_dist_dic = {
'bionic':{"tsinghua","huawei","packages.ros"},
'bullseye':{"tsinghua","huawei","packages.ros"},
'buster':{"packages.ros"},
'cosmic':{"tsinghua","huawei","packages.ros"},
'disco':{"tsinghua","huawei","packages.ros"},
'eoan':{"tsinghua","huawei","packages.ros"},
'focal':{"tsinghua","huawei","packages.ros"},
'jessie':{"tsinghua","huawei"},
'jammy':{"packages.ros"},
'noble':{"packages.ros"},
'stretch':{"tsinghua","huawei","packages.ros"},
'trusty':{"tsinghua","huawei"},
'xenial':{"tsinghua","huawei","packages.ros"},
}
ubuntu_ports_sources_template = """
deb <sources>-ports/ <code-name> main restricted universe multiverse
deb <sources>-ports/ <code-name>-updates main restricted universe multiverse
deb <sources>-ports/ <code-name>-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ <code-name>-security main restricted universe multiverse
"""
ubuntu_amd64_sources_template = """
deb <sources>/ <code-name> main restricted universe multiverse
deb <sources>/ <code-name>-updates main restricted universe multiverse
deb <sources>/ <code-name>-backports main restricted universe multiverse
deb <sources>/ <code-name>-security main restricted universe multiverse
"""
debian_amd64_sources_template = """
deb <sources>/ <code-name> main contrib non-free
deb <sources>/ <code-name>-updates main contrib non-free
deb <sources>/ <code-name>-backports main contrib non-free
deb <sources>-security <code-name>/updates main contrib non-free
"""
ubuntu_ports_deb822_template = """
Types: deb
URIs: <sources>/
Suites: <code-name> <code-name>-updates <code-name>-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
Types: deb
URIs: <sources>/
Suites: <code-name>-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
"""
ubuntu_amd64_deb822_template = """
Types: deb
URIs: <sources>/
Suites: <code-name> <code-name>-updates <code-name>-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
Types: deb
URIs: <sources>/
Suites: <code-name>-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
"""


class Tool(BaseTool):
Expand All @@ -74,91 +67,152 @@ def add_ros_source(self):
if code==2: return
tool = run_tool_file('tools.tool_install_ros',autorun=False)
if not tool.support_install(): return False
# tool.check_sys_source()
tool.add_key()
tool.add_source()


def change_sys_source(self):
"""
一键换源
"""
ports = u"""
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ <code-name> main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ <code-name>-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ <code-name>-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu-ports/ <code-name>-security main restricted universe multiverse
"""
normal = """
deb https://mirrors.ustc.edu.cn/ubuntu/ <code-name> main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ <code-name>-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ <code-name>-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ <code-name>-security main restricted universe multiverse
"""
debian = """
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ <code-name> main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ <code-name>-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ <code-name>-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security <code-name>/updates main contrib non-free
"""


PrintUtils.print_delay('欢迎使用一键换源工具,本工具由[鱼香ROS]小鱼贡献..')
# delete file
def clean_old_source(self):
dic = {1:"仅更换系统源",2:"更换系统源并清理第三方源"}
code,result = ChooseTask(dic, "请选择换源方式,如果不知道选什么请选2").run()
# 尝试第一次更新索引文件
# result = CmdTask('sudo apt update',100).run()

FileUtils.delete('/etc/apt/sources.list')
if code==2:
print("删除一个资源文件")
FileUtils.delete('/etc/apt/sources.list.d')
# fix add source failed before config system source
CmdTask('sudo mkdir -p /etc/apt/sources.list.d').run()



def get_source_by_system(self,system,codename,arch,failed_sources=[]):
# 实际测试发现,阿里云虽然延时很低,但是带宽也低的离谱,一点都不用心,删掉了
ubuntu_amd64_sources = [
"https://mirrors.tuna.tsinghua.edu.cn/ubuntu",
# "https://mirrors.aliyun.com/ubuntu",
"https://mirrors.163.com/ubuntu",
"https://mirrors.ustc.edu.cn/ubuntu",
"https://archive.ubuntu.com/ubuntu",
"https://mirrors.kernel.org/ubuntu",
"http://mirrors.tuna.tsinghua.edu.cn/ubuntu",
# "http://mirrors.aliyun.com/ubuntu",
"http://mirrors.163.com/ubuntu",
"http://mirrors.ustc.edu.cn/ubuntu",
"http://archive.ubuntu.com/ubuntu",
"http://mirrors.kernel.org/ubuntu",
]
ubuntu_ports_sources = [
"https://ports.ubuntu.com/ubuntu-ports",
# "https://mirrors.aliyun.com/ubuntu-ports",
"https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports",
"https://ports.ubuntu.com/ubuntu-ports",
# "https://mirrors.aliyun.com/ubuntu-ports",
"https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports",
]
debian_amd64_sources = [
"https://mirrors.tuna.tsinghua.edu.cn/debian",
"https://mirrors.aliyun.com/debian",
"https://mirrors.163.com/debian",
"https://mirrors.ustc.edu.cn/debian",
"https://deb.debian.org/debian",
"https://mirrors.kernel.org/debian",
"http://mirrors.tuna.tsinghua.edu.cn/debian",
"http://mirrors.aliyun.com/debian",
"http://mirrors.163.com/debian",
"http://mirrors.ustc.edu.cn/debian",
"http://deb.debian.org/debian",
"http://mirrors.kernel.org/debian",
]
sources = []
template = ubuntu_amd64_sources_template
if system=='ubuntu':
if arch=='amd64':
sources = ubuntu_amd64_sources
template = ubuntu_amd64_sources_template
else:
sources = ubuntu_ports_sources
template = ubuntu_ports_sources_template
elif system=='debian':
if arch=='amd64':
template = debian_amd64_sources_template
sources = debian_amd64_sources
PrintUtils.print_delay('搜索到可用源:{}'.format(sources),0.002)
PrintUtils.print_delay('接下来将进行自动测速以为您选择最快的源:')
fast_source = AptUtils.get_fast_url(sources)

# 选择源
if len(failed_sources)>0:
PrintUtils.print_warn('接下来为您排除已经失败的源')
for source in fast_source:
if source in failed_sources:
PrintUtils.print_info(f'{source} 已经测试失败,跳过!')
else:
return source,template
else:
return fast_source[0],template
return None,None


def replace_source(self,failed_sources=[]):
arch = AptUtils.getArch()
PrintUtils.print_delay('检测到当前系统架构为[{}:{}],正在为你更换对应源..'.format(arch,osversion.get_codename()))
source = normal
if osversion.get_name().find("ubuntu")>=0:
if arch=='amd64': source = normal
else: source = ports
elif osversion.get_name().find("debian")>=0:
source = debian
FileUtils.new('/etc/apt/','sources.list',source.replace("<code-name>",osversion.get_codename()))

# update
PrintUtils.print_delay("替换完成,尝试第一次更新....")
result = CmdTask('sudo apt update',100).run()
# https error update second
if result[0]!= 0 and FileUtils.check_result(result[1]+result[2],['Certificate verification','证书']):
PrintUtils.print_delay("发生证书错误,尝试第二次更新....")
FileUtils.delete('/etc/apt/sources.list')
FileUtils.new('/etc/apt/','sources.list',source.replace("https://","http://").replace("<code-name>",osversion.get_codename()))
result = CmdTask('sudo apt update',100).run()
if result[0]!=0:
PrintUtils.print_info("更新失败,开始更换导入方式并三次尝试...")
result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9",10).run()
result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517",10).run()
result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 54404762BBB6E853",10).run()
result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654",10).run()
# sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys
result = CmdTask("apt-get install debian-keyring debian-archive-keyring",10).run()
result = CmdTask("apt-key update",10).run()
result = CmdTask('sudo apt update',100).run()
if result[0]!=0:
PrintUtils.print_info("""如果出现问题NO_PUBKEY XXXXXXXX,请手动运行添加指令:apt-key adv --keyserver keyserver.ubuntu.com --recv-keys XXXXXXXX
如:error: NO_PUBKEY 0E98404D386FA1D9
运行指令:sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9
""")
name = osversion.get_name()
codename = osversion.get_codename()
if name.find("ubuntu")>=0:
system = 'ubuntu'
elif name.find("debian")>=0:
system = 'debian'
else:
return None
PrintUtils.print_delay('检测到当前系统:{} 架构:{} 代号:{},正在为你搜索适合的源...'.format(system,arch,codename))
source,template = self.get_source_by_system(system,codename,arch,failed_sources)
if not source: return None
PrintUtils.print_success('为您选择最快镜像源:{}'.format(source))
FileUtils.new('/etc/apt/','sources.list',template.replace("<code-name>",codename).replace('<sources>',source))
return source

# final check

def change_sys_source(self):
self.clean_old_source()

failed_sources = []
source = self.replace_source(failed_sources)
if source:
PrintUtils.print_delay("替换镜像源完成,尝试进行更新....")
result = CmdTask('sudo apt update',100).run()
while result[0]!= 0:
failed_sources.append(source)
PrintUtils.print_warn("更新失败,尝试更换其他源")
source = self.replace_source(failed_sources)
result = CmdTask('sudo apt update',100).run()
else:
PrintUtils.print_error("没有找到合适的镜像源,臣妾告退!")

# # update
# PrintUtils.print_delay("替换完成,尝试第一次更新....")
# result = CmdTask('sudo apt update',100).run()
# # https error update second
# if result[0]!= 0 and FileUtils.check_result(result[1]+result[2],['Certificate verification','证书']):
# PrintUtils.print_delay("发生证书错误,尝试第二次更新....")
# FileUtils.delete('/etc/apt/sources.list')
# FileUtils.new('/etc/apt/','sources.list',source.replace("https://","http://").replace("<code-name>",osversion.get_codename()))
# result = CmdTask('sudo apt update',100).run()
# if result[0]!=0:
# PrintUtils.print_info("更新失败,开始更换导入方式并三次尝试...")
# result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9",10).run()
# result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517",10).run()
# result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 54404762BBB6E853",10).run()
# result = CmdTask("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654",10).run()
# # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys
# # result = CmdTask("apt-get install debian-keyring debian-archive-keyring",10).run()
# result = CmdTask("apt-key update",10).run()
# result = CmdTask('sudo apt update',100).run()
# if result[0]!=0:
# PrintUtils.print_info("""如果出现问题NO_PUBKEY XXXXXXXX,请手动运行添加指令:apt-key adv --keyserver keyserver.ubuntu.com --recv-keys XXXXXXXX
# 如:error: NO_PUBKEY 0E98404D386FA1D9
# 运行指令:sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0E98404D386FA1D9
# """)
# # final check

if result[0]==0:
PrintUtils.print_success("搞定了,不信你看,累死宝宝了,还不快去给小鱼点个赞~")
PrintUtils.print_info(result[1])

PrintUtils.print_success("镜像修复完成.....")
PrintUtils.print_success("镜像更新完成.....")

def run(self):
# 正式的运行
Expand Down

0 comments on commit d25178c

Please sign in to comment.