From 18981da85749bb0946af1d26d64b57df7a65cc73 Mon Sep 17 00:00:00 2001 From: root <1561515308@qq.com> Date: Tue, 2 Apr 2024 00:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0list=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmpt/__init__.py | 17 +++++++++++++++-- pmpt/util.py | 15 +++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pmpt/__init__.py b/pmpt/__init__.py index 29f934a..38df8b5 100644 --- a/pmpt/__init__.py +++ b/pmpt/__init__.py @@ -2,6 +2,7 @@ import click from . import update as updates from . import util from moyanlib import jsons +from rich.table import Table from . import source as sou from . import install as installs from . import search as searchs @@ -32,8 +33,20 @@ def install(*args,**kwargs): installs.main(*args,**kwargs) @cli.command(name='list',short_help='List all Python packages') -def listp(): - util.runpip('list') +def listp(): + table = Table(show_header=True) + table.add_column('Name') + table.add_column('Version') + listv = util.runpip('freeze',out=False) + for line in iter(listv.stdout.readline, b''): + + # 在这里你可以对每一行输出进行处理 + line = line.decode('utf-8').strip() # 将字节转换为字符串并去除换行符 + if '==' not in line or line[0]=='#': + continue + lineList = line.split('==') + table.add_row(lineList[0],lineList[1]) + util.console.print(table) @cli.command() @click.argument('name') diff --git a/pmpt/util.py b/pmpt/util.py index cd3b601..992943e 100644 --- a/pmpt/util.py +++ b/pmpt/util.py @@ -48,7 +48,7 @@ def loadIndex(): if len(IndexList) == 0: raise FileNotFoundError('No index. Run "pmpt update" first to update the index') -def runpip(command,other=None,dbg=False) -> Popen: +def runpip(command,other=None,dbg=False,out=True) -> Popen: ''' 运行pip ''' @@ -62,11 +62,14 @@ def runpip(command,other=None,dbg=False) -> Popen: console.print('Command to be run:',' '.join(Command)) runClass = Popen(Command,stdout=subprocess.PIPE,stderr=subprocess.PIPE) - for line in iter(runClass.stdout.readline, b''): - # 在这里你可以对每一行输出进行处理 - line = line.decode('utf-8').strip() # 将字节转换为字符串并去除换行符 - console.print(line) - runClass.communicate() + if out: + for line in iter(runClass.stdout.readline, b''): + # 在这里你可以对每一行输出进行处理 + line = line.decode('utf-8').strip() # 将字节转换为字符串并去除换行符 + console.print(line) + runClass.communicate() + else: + runClass.wait() return runClass