修复 QLAPI 修复环境变量 remarks,esm 依赖查不到

This commit is contained in:
whyour
2025-04-19 01:38:43 +08:00
parent 2370923a75
commit 124e01e93a
6 changed files with 160 additions and 52 deletions
+42 -18
View File
@@ -43,35 +43,57 @@ def run():
split_str = "__sitecustomize__"
file_name = sys.argv[0].replace(f"{os.getenv('dir_scripts')}/", "")
command = f'bash -c "source {os.getenv("file_task_before")} {file_name}'
# 创建临时文件路径
temp_file = f"/tmp/env_{os.getpid()}.json"
# 构建命令数组
commands = [
f'source {os.getenv("file_task_before")} {file_name}'
]
task_before = os.getenv("task_before")
if task_before:
escape_task_before = task_before.replace('"', '\\"').replace("$", "\\$")
command += f" && eval '{escape_task_before}'"
escaped_task_before = task_before.replace("'", "'\\''")
commands.append(f"eval '{escaped_task_before}'")
print("执行前置命令\n")
prev_pythonpath = os.getenv("PREV_PYTHONPATH", "")
python_command = (
"python3 -c 'import os, json; print(json.dumps(dict(os.environ)))'"
)
command += f" && echo -e '{split_str}' && {python_command}\""
commands.append(f"echo -e '{split_str}'")
# 修改 Python 命令,使用单行并正确处理引号
python_cmd = f"python3 -c 'import os,json; f=open(\\\"{temp_file}\\\",\\\"w\\\"); json.dump(dict(os.environ),f); f.close()'"
commands.append(python_cmd)
command = " && ".join(cmd for cmd in commands if cmd)
command = f'bash -c "{command}"'
res = subprocess.check_output(command, shell=True, encoding="utf-8")
output, env_str = res.split(split_str)
output = res.split(split_str)[0]
env_json = json.loads(env_str.strip())
try:
with open(temp_file, 'r') as f:
env_json = json.loads(f.read())
for key, value in env_json.items():
os.environ[key] = value
for key, value in env_json.items():
os.environ[key] = value
os.unlink(temp_file)
except Exception as json_error:
print(f"\ue926 Failed to parse environment variables: {json_error}")
try:
os.unlink(temp_file)
except:
pass
if len(output) > 0:
print(output)
if task_before:
print("执行前置命令结束")
print("执行前置命令结束\n")
except subprocess.CalledProcessError as error:
print(f"run task before error: {error}")
print(f"\ue926 run task before error: {error}")
if task_before:
print("执行前置命令结束\n")
except OSError as error:
error_message = str(error)
if "Argument list too long" not in error_message:
@@ -81,9 +103,11 @@ def run():
"\ue926 The environment variable is too large. It is recommended to use task_before.py instead of task_before.sh\n"
)
if task_before:
print("执行前置命令结束")
print("执行前置命令结束\n")
except Exception as error:
print(f"run task before error: {error}")
print(f"\ue926 run task before error: {error}")
if task_before:
print("执行前置命令结束\n")
import task_before