NEP框架重构00阶段
This commit is contained in:
40
src/utils.py
40
src/utils.py
@@ -44,4 +44,42 @@ class Notifier:
|
||||
|
||||
def send(self, title, msg, priority=5):
|
||||
# 暂时只打印日志,不实际发送
|
||||
logging.info(f"[[Notification]] {title}: {msg}")
|
||||
logging.info(f"[[Notification]] {title}: {msg}")
|
||||
|
||||
|
||||
# src/utils.py 添加在最后
|
||||
|
||||
def run_cmd_with_log(cmd, cwd, log_file="exec.log", input_str=None):
|
||||
"""
|
||||
执行命令并将 stdout/stderr 重定向到日志文件
|
||||
"""
|
||||
import subprocess
|
||||
|
||||
log_path = os.path.join(cwd, log_file)
|
||||
mode = 'a' if os.path.exists(log_path) else 'w'
|
||||
|
||||
with open(log_path, mode) as f:
|
||||
f.write(f"\n\n>>> Executing: {cmd}\n")
|
||||
f.write(f">>> Input: {repr(input_str)}\n")
|
||||
f.write("-" * 40 + "\n")
|
||||
f.flush()
|
||||
|
||||
try:
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
shell=True,
|
||||
cwd=cwd,
|
||||
stdin=subprocess.PIPE if input_str else None,
|
||||
stdout=f, # 直接指向文件
|
||||
stderr=subprocess.STDOUT, # 把错误也合并到同一个日志
|
||||
text=True
|
||||
)
|
||||
|
||||
# 发送输入并等待
|
||||
process.communicate(input=input_str)
|
||||
|
||||
f.write(f"\n>>> Finished with Return Code: {process.returncode}\n")
|
||||
return process.returncode == 0
|
||||
except Exception as e:
|
||||
f.write(f"\n>>> Exception: {str(e)}\n")
|
||||
return False
|
||||
Reference in New Issue
Block a user