Skip to content

Commit

Permalink
修改: src/d2py/tasks/utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinwei committed Apr 10, 2024
1 parent e178755 commit 24c1b7c
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/d2py/tasks/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""任务型事务辅助函数"""
import asyncio
import logging
import textwrap

async def run_cmd(cmd: str, cwd: str|None=None, indent: int=2, **kwds)->str:
async def communicate_cmd(cmd: str, cwd: str|None=None, indent: int=2, **kwds)->str:
"""运行命令行执行任务
Args:
Expand All @@ -13,6 +14,7 @@ async def run_cmd(cmd: str, cwd: str|None=None, indent: int=2, **kwds)->str:
Return:
返回命令任务执行的打印信息
"""
_ = lambda text: textwrap.indent(text, prefix=" "*indent) # 美化打印信息
proc = await asyncio.create_subprocess_shell(
cmd,
stdin=asyncio.subprocess.PIPE,
Expand All @@ -22,7 +24,44 @@ async def run_cmd(cmd: str, cwd: str|None=None, indent: int=2, **kwds)->str:
stdout, stderr = await proc.communicate(input=None)
std_data = f'[{cmd!r} 退出状态码 {proc.returncode}]\n'
if stdout:
std_data += f'[stdout]:\n{textwrap.indent(stdout.decode(), ' '*indent)}\n'
std_data += f'[stdout]:\n{_(stdout.decode())}\n'
if stderr:
std_data += f'[stdinfo]:\n{textwrap.indent(stderr.decode(), ' '*indent)}\n'
std_data += f'[stdinfo]:\n{_(stderr.decode())}\n'
return std_data

# async def log_cmd(cmd: str, cwd: str|None=None, indent: int=2, logger_name="run", **kwds)->str:
# """运行命令行执行任务,并记录打印信息

# Args:
# cmd: 命令行任务
# cwd: 当前工作目录
# indent: 命令行打印信息缩进位数

# Return:
# 返回命令任务执行的打印信息
# """
# logger = logger = logging.getLogger(logger_name)
# hd = logging.StreamHandler()
# hd.setFormatter(logging.Formatter("%(message)s"))
# logger.addHandler(hd)
# proc = await asyncio.create_subprocess_shell(
# cmd,
# stdin=asyncio.subprocess.PIPE,
# stdout=asyncio.subprocess.PIPE,
# stderr=asyncio.subprocess.PIPE,
# cwd=cwd, **kwds)
# # 获取实时输出/错误信息
# if not proc.stdout.at_eof():
# for line in iter(proc.stdout.readline, b''):
# line = await line
# logger.info(line.decode())
# if proc.stdout.at_eof():
# break
# if not proc.stderr.at_eof():
# for line in iter(proc.stderr.readline, b''):
# line = await line
# logger.info(line.decode())
# if proc.stderr.at_eof():
# break
# # 等待命令执行完成
# proc.wait()

0 comments on commit 24c1b7c

Please sign in to comment.