Skip to content

Commit f435903

Browse files
committed
2 parents 1fc3854 + 8f278e0 commit f435903

3 files changed

Lines changed: 72 additions & 36 deletions

File tree

client/node/toolloader/loadconfig.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ def __update_tool_names(self, tool_names, task_list):
161161
tool_names.remove(c_name)
162162
return tool_names
163163

164-
def read_tool_config_from_ini_file(self, tool_names=None, custom_tools=None, task_list=None, config_all_tools=False, include_common=True):
164+
def read_tool_config_from_ini_file(self, tool_names=None, custom_tools=None, task_list=None, config_all_tools=False,
165+
include_common=True):
165166
"""
166167
从ini文件中读取工具配置
167168
"""
@@ -194,6 +195,7 @@ def read_tool_config_from_ini_file(self, tool_names=None, custom_tools=None, tas
194195
"env_path": env_path_section, # env_path_section中现在已经没有key为"PATH"的元素,可以作为编译工具的env_path
195196
"env_value": env_value_section
196197
}
198+
self.__format_to_fullpath(compile_config)
197199
config_dict["compile_config"] = compile_config
198200
return config_dict
199201
else:
@@ -236,6 +238,25 @@ def __format_config(self, tool_cfg, env_path_section, env_value_section):
236238
tool_cfg["env_value"] = self.__str_to_dict(tool_cfg["env_value"], env_value_section)
237239
tool_cfg["tool_url"] = self.__str_to_list(tool_cfg["tool_url"])
238240
tool_cfg["path"] = self.__str_to_list(tool_cfg["path"], uniq=False)
241+
self.__format_to_fullpath(tool_cfg)
242+
243+
def __format_to_fullpath(self, tool_cfg):
244+
# 将路径类型的环境变量(env_path)中的相对路径转换成绝对路径
245+
for env_name, rel_path in tool_cfg["env_path"].items():
246+
if "PATH" == env_name: # PATH应该单独放在path字段中,如果放在env_path中,忽略,避免影响和覆盖原有PATH变量
247+
continue
248+
full_path = os.path.join(settings.TOOL_BASE_DIR, rel_path)
249+
tool_cfg["env_path"][env_name] = full_path
250+
251+
# 将PATH环境变量的相对路径转换成绝对路径
252+
path_env = []
253+
for rel_path in tool_cfg["path"]:
254+
if "$" in rel_path or "%" in rel_path: # 带变量的环境变量,已经是全路径
255+
full_path = rel_path
256+
else:
257+
full_path = os.path.join(settings.TOOL_BASE_DIR, rel_path)
258+
path_env.append(full_path)
259+
tool_cfg["path"] = path_env
239260

240261
def read_config_from_tool_schemes(self, tool_names=None, task_list=None):
241262
"""
@@ -266,7 +287,7 @@ def read_config_from_tool_schemes(self, tool_names=None, task_list=None):
266287
config_dict = {}
267288
already_config_tools = [] # 记录已经读取到配置的工具
268289
for task_config in task_list:
269-
task_name = task_config["task_name"]
290+
task_name = task_config.get("task_name")
270291
if tool_names and task_name not in tool_names:
271292
continue
272293
task_params = task_config.get("task_params", {})
@@ -282,29 +303,40 @@ def read_config_from_tool_schemes(self, tool_names=None, task_list=None):
282303
for tool_lib in tool_libs:
283304
lib_support_os = tool_lib.get("os", [])
284305
if self._os_type in lib_support_os:
306+
# 使用新的结构存储环境变量,避免修改原有的任务参数
307+
new_lib_envs = {}
285308
lib_envs = tool_lib.get("envs", {})
286309
if not lib_envs: # 可能会传空list,这里判空,避免后面格式出错
287-
lib_envs = {}
310+
new_lib_envs = {}
311+
288312
scm_url = tool_lib.get("scm_url")
289313
lib_dir_name = scm_url.split('/')[-1].strip().replace(".git", "")
290-
# 环境变量中的$ROOT_DIR替换为目录名,后续加载环境变量时会拼接为全路径
314+
lib_dir_path = os.path.join(settings.TOOL_BASE_DIR, lib_dir_name)
315+
316+
# 将环境变量中的$ROOT_DIR替换为实际路径,重新保存到new_lib_envs
291317
for key, value in lib_envs.items():
292318
if "$ROOT_DIR" in value:
293-
lib_envs[key] = value.replace("$ROOT_DIR", lib_dir_name)
319+
new_lib_envs[key] = value.replace("$ROOT_DIR", lib_dir_path)
320+
else:
321+
new_lib_envs[key] = value
322+
294323
# PATH和其他环境变量分开处理
295324
path_envs = []
296325
other_envs = {}
297-
for env_name, env_value in lib_envs.items():
326+
for env_name, env_value in new_lib_envs.items():
298327
if env_name == "PATH":
299328
path_envs = self.__str_to_list(env_value)
300329
else:
301-
other_envs[env_name] = env_value
330+
# 根据英文分号拆分后,再环境变量分隔符拼接到一起
331+
value_list = self.__str_to_list(env_value)
332+
value_format = os.pathsep.join(value_list)
333+
other_envs[env_name] = value_format
302334
lib_config = {
303335
"tool_url": scm_url,
304336
"scm_type": tool_lib.get("scm_type"),
305337
"auth_info": tool_lib.get("auth_info"),
306338
"path": path_envs,
307-
"env_path": lib_envs,
339+
"env_path": other_envs,
308340
"env_value": {}
309341
}
310342
lib_name = tool_lib.get("name")

client/settings/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@
9595
# ========================
9696
# 默认从Git拉取工具;如果使用本地工具,可以在config.ini中配置该值为True,将不自动拉取内置工具和配置文件
9797
USE_LOCAL_TOOL = False
98-
# 默认的工具目录,即puppy根目录下的data/tools目录
99-
DEFAULT_TOOL_BASE_DIR = join(TCA_BASE_DIR, "tools")
10098
# 扫描工具目录
10199
TOOL_BASE_DIR = join(TCA_BASE_DIR, "tools")
102100
# 扫描工具配置文件地址,需要在config.ini中配置

client/util/envset.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import logging
1515
import platform
1616

17-
from node.app import settings
18-
from node.toolloader.loadconfig import ConfigLoader
1917
from util.textutil import StringMgr
2018

2119
logger = logging.getLogger(__name__)
@@ -67,6 +65,7 @@ def __update_os_environ(self, envs, task_mode = True):
6765
os.environ.update(new_envs)
6866
else:
6967
for env_name, env_value in envs.items():
68+
logger.debug(f">>> set env {env_name}={os.path.expandvars(env_value)}")
7069
os.environ.update({env_name: os.path.expandvars(env_value)})
7170

7271
def set_tool_env(self, tool_config):
@@ -88,17 +87,13 @@ def set_tool_env(self, tool_config):
8887
"""
8988
tool_envs = {}
9089

91-
path_evn = []
90+
path_env = []
9291
for tool_name, tool_params in tool_config.items():
9392
# 添加路径类型的环境变量
94-
for env_name, rel_path in tool_params["env_path"].items():
93+
for env_name, full_path in tool_params["env_path"].items():
9594
if "PATH" == env_name: # PATH应该单独放在path字段中,如果放在env_path中,忽略,避免影响和覆盖原有PATH变量
9695
continue
9796
if env_name not in tool_envs:
98-
full_path = os.path.join(settings.TOOL_BASE_DIR, rel_path)
99-
# 如果工具目录不存在,重定向到默认的工具目录下
100-
if not os.path.exists(full_path):
101-
full_path = os.path.join(settings.DEFAULT_TOOL_BASE_DIR, rel_path)
10297
tool_envs[env_name] = full_path
10398

10499
# 添加值类型的环境变量
@@ -108,21 +103,17 @@ def set_tool_env(self, tool_config):
108103

109104
# 将PATH的环境变量放到一个list中
110105
for rel_path in tool_params["path"]:
111-
if rel_path not in path_evn:
112-
path_evn.append(rel_path)
106+
if rel_path not in path_env:
107+
path_env.append(rel_path)
113108

114109
# 添加PATH环境变量
115-
path_str = os.environ["PATH"] # 先读取系统PATH环境变量
116-
path_list = path_str.split(os.pathsep)
117-
for rel_path in path_evn:
118-
if "$" in rel_path or "%" in rel_path: # 带变量的环境变量,已经是全路径
119-
full_path = rel_path
120-
else:
121-
full_path = os.path.join(settings.TOOL_BASE_DIR, rel_path)
122-
# 如果工具目录不存在,重定向到默认的工具目录下
123-
if not os.path.exists(full_path):
124-
full_path = os.path.join(settings.DEFAULT_TOOL_BASE_DIR, rel_path)
125-
# if absolute_path not in path_list: # path不要去重,会影响加载顺序
110+
path_str = os.getenv("PATH") # 先读取系统PATH环境变量
111+
if path_str:
112+
path_list = path_str.split(os.pathsep)
113+
else: # 增加判空,防止读取不到的情况
114+
path_list = []
115+
for full_path in path_env:
116+
# path不要去重,会影响加载顺序
126117
path_list = [full_path] + path_list # 后添加的放在前面,优先搜索
127118
tool_envs["PATH"] = os.pathsep.join(path_list)
128119

@@ -156,6 +147,7 @@ def set_task_env(self, task_params):
156147
if task_envs:
157148
# 设置到进程环境变量中
158149
self.__update_os_environ(task_envs, True)
150+
# logger.debug('设置任务环境变量::\n%s' % '\n'.join(['%s=%s' % (key, value) for key, value in task_envs.items()]))
159151
EnvSetting.env_setting_init()
160152

161153
def get_origin_env(self, os_env=None):
@@ -175,17 +167,31 @@ def get_origin_env(self, os_env=None):
175167
env = os_env
176168
else:
177169
env = dict(os.environ)
170+
178171
# 判断是否是pyinstaller包状态, 若是源码状态,则不处理
179172
if len(sys.argv) > 0 and sys.argv[0].endswith(".py"):
180173
return env
174+
181175
# Linux
182176
if sys.platform == "linux" or sys.platform == "linux2":
183177
lp_key = 'LD_LIBRARY_PATH'
184-
lp_orig = env.get(lp_key + '_ORIG')
185-
if lp_orig is not None:
186-
env[lp_key] = lp_orig
187-
else:
188-
env.pop(lp_key, None)
178+
# 收集需要的lp环境变量
179+
wanted_env_list = []
180+
# 收集现有进程的lp环境变量,剔除掉pyinstaller添加的临时运行目录,则为需要传递给子进程的环境变量
181+
cur_lp_value = env.get(lp_key)
182+
if cur_lp_value:
183+
cur_lp_list = cur_lp_value.split(os.pathsep)
184+
for lp_value in cur_lp_list:
185+
# 当前进程的lp环境变量,剔除掉pyinstaller添加的临时运行目录,其他的为需要的
186+
if not lp_value.startswith("/tmp/_MEI"):
187+
wanted_env_list.append(lp_value)
188+
if wanted_env_list: # 现有的lp已经包含_ORIG
189+
env[lp_key] = os.pathsep.join(wanted_env_list)
190+
logger.debug(f"{lp_key}={cur_lp_value}, change to {env[lp_key]}")
191+
else:
192+
env.pop(lp_key, None)
193+
logger.debug(f"{lp_key}={cur_lp_value}, delete it.")
194+
189195
# Mac:
190196
# 由于Mac下二进制包无法检测环境中环境变量原来是否有DYLD_LIBRARY_PATH,加上目前没有发现有项目遇到类似问题
191197
# 这里暂不做处理,继续观察

0 commit comments

Comments
 (0)