美化输出
This commit is contained in:
parent
9e49101e5c
commit
81805e488e
41
pmpt/environment.py
Normal file
41
pmpt/environment.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
import subprocess
|
||||
import re
|
||||
|
||||
def get_version(command):
|
||||
try:
|
||||
# 使用 subprocess 调用系统命令获取版本信息
|
||||
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, universal_newlines=True)
|
||||
# 使用正则表达式提取版本号
|
||||
version_match = re.search(r'(\d+\.\d+(\.\d+){0,2})', output)
|
||||
if version_match:
|
||||
return version_match.group(1)
|
||||
else:
|
||||
return False
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
def getGCCver():
|
||||
# 检查是否能执行 gcc 命令
|
||||
return get_version('gcc --version')
|
||||
|
||||
def getClangVer():
|
||||
# 检查是否能执行 clang 命令
|
||||
return get_version('clang --version')
|
||||
|
||||
def getMSVCver():
|
||||
# 检查是否能执行 cl 命令(MSVC编译器)
|
||||
return get_version('cl')
|
||||
|
||||
def getRustVer():
|
||||
process = subprocess.Popen(['rustc --version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)
|
||||
stdout, stderr = process.communicate()
|
||||
if process.returncode == 0:
|
||||
rustVer = stdout.decode().split(' ')[1]
|
||||
return rustVer
|
||||
else:
|
||||
return False
|
||||
|
||||
print("GCC 版本:", getGCCver())
|
||||
print("Clang 版本:", getClangVer())
|
||||
print("MSVC 版本:", getMSVCver())
|
||||
print('Rust版本:',getRustVer())
|
|
@ -1,68 +1,78 @@
|
|||
from . import util
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
|
||||
def search(name):
|
||||
util.loadIndex() #加载索引
|
||||
|
||||
for Index in util.IndexList:
|
||||
dt = Index.packageList.get(name,None)
|
||||
if dt:
|
||||
return Index.IndexURL
|
||||
|
||||
def main(packlist,upgrade,reads,force_reinstall,ignore_requires_python,yes):
|
||||
|
||||
console = Console()
|
||||
if reads: # 从文件读取列表
|
||||
f = open(packlist[0])
|
||||
packlist = f.read().split('\n')
|
||||
|
||||
packsInfo = {}
|
||||
print('Start installing these packages')
|
||||
print('Start looking for package')
|
||||
with console.status('🚀🔍 Searching for package information...') as status:
|
||||
table = Table(show_header=True,header_style='bold')
|
||||
table.add_column("Package Name", width=20,style='bold')
|
||||
table.add_column("Package Source", width=20,style='green')
|
||||
|
||||
for rawpack in packlist: # 解析指定了版本的包名
|
||||
if '==' in rawpack:
|
||||
pack = rawpack.split('==')[0]
|
||||
elif '>=' in rawpack:
|
||||
pack = rawpack.split('>=')[0]
|
||||
elif '<=' in rawpack:
|
||||
pack = rawpack.split('<=')[0]
|
||||
elif '<' in rawpack:
|
||||
pack = rawpack.split('<')[0]
|
||||
elif '>' in rawpack:
|
||||
pack = rawpack.split('>')[0]
|
||||
else:
|
||||
pack = rawpack
|
||||
for rawpack in packlist: # 解析指定了版本的包名
|
||||
if '==' in rawpack:
|
||||
pack = rawpack.split('==')[0]
|
||||
elif '>=' in rawpack:
|
||||
pack = rawpack.split('>=')[0]
|
||||
elif '<=' in rawpack:
|
||||
pack = rawpack.split('<=')[0]
|
||||
elif '<' in rawpack:
|
||||
pack = rawpack.split('<')[0]
|
||||
elif '>' in rawpack:
|
||||
pack = rawpack.split('>')[0]
|
||||
else:
|
||||
pack = rawpack
|
||||
|
||||
|
||||
result = search(pack.lower()) # 转小写并获取源地址
|
||||
packsInfo[pack] = [result,rawpack]
|
||||
result = search(pack.lower()) # 转小写并获取源地址
|
||||
packsInfo[pack] = [result,rawpack]
|
||||
|
||||
canInstallPack = []
|
||||
for k,v in packsInfo.items():
|
||||
if not v:
|
||||
print('Unable to find', k)
|
||||
if not v[0]:
|
||||
table.add_row(k, "[red]Not found[red]")
|
||||
else:
|
||||
print(f'Find {k} from {v[0]}')
|
||||
table.add_row(k,v[0])
|
||||
canInstallPack.append(k)
|
||||
console.print(table)
|
||||
|
||||
print('Will install',', '.join(canInstallPack))
|
||||
if len(canInstallPack) < 1:
|
||||
console.print('❌ [red]There are no packages available for installation.[red]')
|
||||
exit()
|
||||
|
||||
console.print('📦 Packages to be installed:')
|
||||
console.print(' '.join(canInstallPack))
|
||||
|
||||
while True: # 是否允许安装
|
||||
if yes:
|
||||
break
|
||||
|
||||
ye =input('Are you sure you want to install? (y/n)')
|
||||
ye = console.input('Proceed with installation? [Y/n]: ')
|
||||
|
||||
if ye.lower() == 'y':
|
||||
break
|
||||
elif ye.lower() == 'n':
|
||||
print('User Cancels Installation')
|
||||
console.print('🛑 [red]User canceled the installation.[/red]')
|
||||
exit()
|
||||
else:
|
||||
continue
|
||||
|
||||
print('Start calling pip')
|
||||
console.print('🛠️ Initiating pip installation...')
|
||||
for pack in canInstallPack:
|
||||
# 构建命令
|
||||
|
||||
# 构建命
|
||||
args = [ '-i', packsInfo[pack][0]] # 指定源
|
||||
|
||||
if upgrade: # 升级
|
||||
|
@ -76,9 +86,9 @@ def main(packlist,upgrade,reads,force_reinstall,ignore_requires_python,yes):
|
|||
ret = util.runpip('install',args) # 运行pip
|
||||
|
||||
if ret.returncode != 0: #是否执行完毕
|
||||
print('Failed to install.')
|
||||
console.print('❌ [red]Installation failed.[/red]')
|
||||
exit()
|
||||
print('Installation completed')
|
||||
console.print(f'✅ [green]Installation successful for {pack}[/green]')
|
||||
|
||||
|
||||
|
|
@ -3,9 +3,12 @@ from . import util
|
|||
from moyanlib import jsons
|
||||
from .install import search
|
||||
import os
|
||||
from rich.console import Console
|
||||
|
||||
console = Console()
|
||||
def main(name,allinfo,api_url):
|
||||
if not search(name):
|
||||
print('The package does not exist')
|
||||
console.print('❌ [red]The package does not exist[/red]')
|
||||
exit()
|
||||
|
||||
if not api_url:
|
||||
|
@ -13,11 +16,11 @@ def main(name,allinfo,api_url):
|
|||
|
||||
req = requests.get(api_url.format(name))
|
||||
if req.status_code == 404:
|
||||
print('The package does not exist')
|
||||
console.print('❌ [red]The package does not exist[/red]')
|
||||
exit()
|
||||
elif req.status_code != 200:
|
||||
print('Server Error!')
|
||||
print('Error Code:',req.status_code)
|
||||
console.print('❌ [red]Server Error![/red]')
|
||||
console.print('[red]Error Code: '+str(req.status_code)+'[/red]')
|
||||
exit()
|
||||
|
||||
|
||||
|
|
|
@ -6,7 +6,9 @@ import os
|
|||
import urllib.parse as urlparse
|
||||
from hashlib import sha256
|
||||
from .util import dirs
|
||||
from rich.console import Console
|
||||
|
||||
console = Console()
|
||||
def getSourceID(url):
|
||||
'''
|
||||
获取源id
|
||||
|
@ -33,7 +35,7 @@ def getIndex(url):
|
|||
|
||||
ClassIndex = Index(url)
|
||||
|
||||
print('Start parsing the HTML index for this source.')
|
||||
console.print('🔍 Parsing HTML index...')
|
||||
for line in tqdm(HTMLIndex.split('\n')):
|
||||
# 提取并筛选标签
|
||||
line_list = line.split('>')
|
||||
|
@ -42,8 +44,8 @@ def getIndex(url):
|
|||
|
||||
ClassIndex.addPackage(package_name) # 添加包
|
||||
|
||||
print('This source has a total of', ClassIndex.number,'packages.')
|
||||
print('Start saving the index for this source.')
|
||||
console.print('Total number of packages:', str(ClassIndex.number))
|
||||
console.print('📚 Saving index..."')
|
||||
dill.dump(ClassIndex,open(f'{dirs.user_data_dir}/Index/{getSourceID(url)}.pidx','wb'))
|
||||
|
||||
def getAllIndex():
|
||||
|
@ -55,11 +57,11 @@ def getAllIndex():
|
|||
'''
|
||||
SourceList = jsons.load(open(os.path.join(dirs.user_config_dir,'Source.json'))) # 加载源列表
|
||||
if len(SourceList) < 1:
|
||||
print('You have not configured any sources.')
|
||||
console.print('❌ [red]You have not configured any sources.[/red]')
|
||||
exit(1)
|
||||
|
||||
for url in SourceList: # 遍历源列表
|
||||
print('Start downloading index from', url)
|
||||
console.print('📚 Downloading index from', url+'...')
|
||||
getIndex(url)
|
||||
print()
|
||||
console.print('✅ [green]Index downloaded successfully![/green]')
|
||||
|
||||
|
|
12
pmpt/util.py
12
pmpt/util.py
|
@ -1,8 +1,10 @@
|
|||
#pylint:disable=W0622
|
||||
import os
|
||||
from subprocess import Popen,PIPE
|
||||
import sys
|
||||
import pathlib
|
||||
import dill
|
||||
from rich import print
|
||||
import time
|
||||
from platformdirs import PlatformDirs
|
||||
|
||||
|
@ -24,6 +26,7 @@ def loadIndex():
|
|||
加载索引
|
||||
'''
|
||||
if len(IndexList) == 0: # 判断是否为空
|
||||
print('🔍 Loading index...')
|
||||
IndexDir = pathlib.Path(os.path.join(dirs.user_data_dir,'Index'))
|
||||
for i in IndexDir.iterdir(): # 遍历索引文件夹
|
||||
IndexFile = dill.load(open(i,'rb')) # 加载索引
|
||||
|
@ -32,17 +35,20 @@ def loadIndex():
|
|||
if len(IndexList) == 0:
|
||||
raise FileNotFoundError('No index. Run "pmpt update" first to update the index')
|
||||
|
||||
def runpip(command,other=[]) -> Popen:
|
||||
def runpip(command,other=None,dbg=False) -> Popen:
|
||||
'''
|
||||
运行pip
|
||||
'''
|
||||
if not other:
|
||||
other = []
|
||||
baseCommand = [sys.executable,'-m','pip']
|
||||
baseCommand.append(command)
|
||||
|
||||
Command = baseCommand + other
|
||||
|
||||
print(' '.join(Command))
|
||||
if dbg:
|
||||
print('Command to be run:',' '.join(Command))
|
||||
|
||||
runClass = Popen(Command)
|
||||
runClass.wait()
|
||||
return runClass
|
||||
|
|
@ -4,3 +4,4 @@ moyanlib
|
|||
click
|
||||
dill
|
||||
tqdm
|
||||
rich
|
Loading…
Reference in New Issue
Block a user