一键安装命令

rm -f apt-fast.py;wget http://down.liumou.site/upload/apt-fast.py;python3 apt-fast.py 

源码

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File    :   apt.py
@Time    :   2022-08-27 16:40
@Author  :   坐公交也用券
@Version :   1.0
@Contact :   faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc    :   当前文件作用
"""
from argparse import ArgumentParser
from os import system, chdir, getenv, path
from sys import exit


class InstallAptFast:
	def __init__(self, passwd):
		"""
		初始化
		:param passwd: 主机密码
		"""
		self.passwd = passwd
		self.url = 'https://gitcode.net/qq_36154886/apt-fast.git'
		self.home = getenv(key='HOME')
		print(f"当前主目录: {self.home}")
		self.apt_path = path.join(self.home, 'apt-fast')
	
	def sudo(self, cmd):
		"""
		执行sudo命令
		:param cmd: 需要执行的命令
		:return:
		"""
		cmd = str(f"echo {self.passwd} | sudo -S {cmd}")
		print(f"正在执行: {cmd}")
		res = int(system(cmd))
		if res == 0:
			return True
		return False
	
	def git(self):
		res = system("which git")
		if int(res) == 0:
			return False
		return True
	
	def install_git(self):
		"""
		安装git
		:return:
		"""
		if self.sudo("apt update"):
			print("源更新成功")
			if self.sudo("apt install -y git"):
				print("Git安装成功")
			else:
				print("Git安装失败")
				exit(2)
		else:
			print("源更新失败")
			exit(1)
	
	def clone(self):
		"""
		开始克隆
		:return:
		"""
		chdir(self.home)
		if path.exists(self.apt_path):
			self.sudo(cmd=f"rm -rf {self.apt_path}")
		cmd = f"git clone {self.url}"
		if int(system(cmd)) == 0:
			print("克隆成功")
		else:
			print("克隆失败")
			exit(3)
	
	def completion(self):
		cmd = "apt-get install bash-completion -y"
		self.sudo(cmd)
		cp = f"cp {path.join(self.apt_path, 'completions/bash/apt-fast')} /etc/bash_completion.d/"
		self.sudo(cmd=cp)
		chow = "chown root:root /etc/bash_completion.d/apt-fast"
		self.sudo(cmd=chow)
	
	def aria(self):
		"""
		安装aria2
		:return:
		"""
		cmd = "apt install -y aria2"
		self.sudo(cmd)
	
	def mv(self):
		"""
		移动可执行文件
		:return:
		"""
		file = path.join(self.apt_path, 'apt-fast')
		if path.isfile(file):
			cmd = f"cp -rf {file} /usr/bin/"
			self.sudo(cmd=cmd)
			conf = path.join(self.apt_path, 'apt-fast.conf')
			cmd = f"cp -rf {conf} /etc/"
			self.sudo(cmd=cmd)
	
	def start(self):
		if self.git():
			self.install_git()
		self.clone()
		self.mv()
		self.aria()
		self.completion()


if __name__ == "__main__":
	py_ver = "2022.0827.1736"
	arg = ArgumentParser(description='当前脚本版本: %s' % py_ver, prog="一键安装apt-fast")
	arg.add_argument('-p', '--passwd', type=str, help='设置sudo密码,默认:无', required=False)
	args = arg.parse_args()
	pd = args.passwd
	install = InstallAptFast(passwd=pd)
	install.start()