snapshot before regression test
This commit is contained in:
1
lib/utils/__init__.py
Normal file
1
lib/utils/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""通用工具函数"""
|
||||
47
lib/utils/helpers.py
Normal file
47
lib/utils/helpers.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# 工具函数
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from lib.core.VLogger import get_logger as _vlog
|
||||
|
||||
|
||||
def DetectFileType(FilePath: str) -> str:
|
||||
"""检测文件类型"""
|
||||
_: str
|
||||
ext: str
|
||||
_, ext = os.path.splitext(FilePath)
|
||||
return ext.lower()
|
||||
|
||||
|
||||
def ExecuteCommand(command: list[str], shell: bool = False, CaptureOutput: bool = True, text: bool = True) -> subprocess.CompletedProcess[str] | None:
|
||||
"""执行命令
|
||||
command 应该是列表形式(如 ['gcc', 'file.c', '-o', 'file.exe'])
|
||||
避免字符串拼接以防止 shell 注入"""
|
||||
try:
|
||||
result: subprocess.CompletedProcess[str] = subprocess.run(
|
||||
command,
|
||||
shell=shell,
|
||||
check=True,
|
||||
capture_output=CaptureOutput,
|
||||
text=text
|
||||
)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
_vlog().error(f'编译失败: {e}')
|
||||
if e.stderr:
|
||||
_vlog().error(f'错误输出: {e.stderr}')
|
||||
return None
|
||||
|
||||
|
||||
def AppendFileContent(FilePath: str, content: str) -> bool:
|
||||
"""追加文件内容"""
|
||||
try:
|
||||
with open(FilePath, 'a', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
f.write('\n')
|
||||
return True
|
||||
except Exception as e:
|
||||
_vlog().error(f'写入文件失败 {FilePath}: {e}')
|
||||
return False
|
||||
Reference in New Issue
Block a user