补充
This commit is contained in:
41
lib/utils/helpers.py
Normal file
41
lib/utils/helpers.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# 工具函数
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from lib.constants.config import ERROR_MESSAGES
|
||||
|
||||
|
||||
def DetectFileType(FilePath):
|
||||
"""检测文件类型"""
|
||||
_, ext = os.path.splitext(FilePath)
|
||||
return ext.lower()
|
||||
|
||||
|
||||
def ExecuteCommand(command, shell=True, CaptureOutput=True, text=True):
|
||||
"""执行命令"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
command,
|
||||
shell=shell,
|
||||
check=True,
|
||||
capture_output=CaptureOutput,
|
||||
text=text
|
||||
)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f'{ERROR_MESSAGES["COMPILE_FAILED"]}: {e}')
|
||||
if e.stderr:
|
||||
print(f'Error output: {e.stderr}')
|
||||
return None
|
||||
|
||||
|
||||
def AppendFileContent(FilePath, content):
|
||||
"""追加文件内容"""
|
||||
try:
|
||||
with open(FilePath, 'a', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
f.write('\n')
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f'Failed to append to file {FilePath}: {e}')
|
||||
return False
|
||||
Reference in New Issue
Block a user