merge_remote_resources: 以远端为准,移除本地已不在远端的物料

远端(bioyond)不存在的物料不应保留在本地资源树中,
否则这些过期物料会在 PLR 反序列化时产生命名冲突。
同步时对两级子节点均执行移除:
- 三级物料(设备→仓库→物料)
- 三级子物料(设备→物料→子物料)

同时修复 else 分支缺少 remote_child_name 存在性检查的潜在 KeyError。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ZiWei
2026-04-28 11:59:29 +08:00
parent 44afc7733b
commit 96c4be17dc

View File

@@ -868,13 +868,34 @@ class ResourceTreeSet(object):
f"已存在,跳过" f"已存在,跳过"
) )
# 移除本地有但远端已不存在的物料(以远端为准)
remote_material_names = {m.res_content.name for m in remote_child.children}
removed_count = 0
for child in list(local_sub_device.children):
if child.res_content.name not in remote_material_names:
local_sub_device.children.remove(child)
removed_count += 1
logger.info(
f"移除远端已不存在的物料: '{remote_root_id}/{remote_child_name}/{child.res_content.name}'"
)
if added_count > 0: if added_count > 0:
logger.info( logger.info(
f"Device '{remote_root_id}/{remote_child_name}': " f"Device '{remote_root_id}/{remote_child_name}': "
f"从远端同步了 {added_count} 个物料子树" f"从远端同步了 {added_count} 个物料子树"
) )
if removed_count > 0:
logger.info(
f"Device '{remote_root_id}/{remote_child_name}': "
f"移除了 {removed_count} 个远端已删除的物料"
)
else: else:
# 二级物料已存在,比较三级子节点是否缺失 # 二级物料已存在,比较三级子节点是否缺失
if remote_child_name not in local_children_map:
logger.warning(
f"物料 '{remote_root_id}/{remote_child_name}' 在远端存在但本地不存在,跳过"
)
continue
local_material = local_children_map[remote_child_name] local_material = local_children_map[remote_child_name]
local_material_children_map = {child.res_content.name: child for child in local_material_children_map = {child.res_content.name: child for child in
local_material.children} local_material.children}
@@ -890,11 +911,28 @@ class ResourceTreeSet(object):
f"物料 '{remote_root_id}/{remote_child_name}/{remote_sub_name}' " f"物料 '{remote_root_id}/{remote_child_name}/{remote_sub_name}' "
f"已存在,跳过" f"已存在,跳过"
) )
# 移除本地有但远端已不存在的子物料(以远端为准)
remote_sub_names = {s.res_content.name for s in remote_child.children}
removed_count = 0
for child in list(local_material.children):
if child.res_content.name not in remote_sub_names:
local_material.children.remove(child)
removed_count += 1
logger.info(
f"移除远端已不存在的子物料: '{remote_root_id}/{remote_child_name}/{child.res_content.name}'"
)
if added_count > 0: if added_count > 0:
logger.info( logger.info(
f"物料 '{remote_root_id}/{remote_child_name}': " f"物料 '{remote_root_id}/{remote_child_name}': "
f"从远端同步了 {added_count} 个子物料" f"从远端同步了 {added_count} 个子物料"
) )
if removed_count > 0:
logger.info(
f"物料 '{remote_root_id}/{remote_child_name}': "
f"移除了 {removed_count} 个远端已删除的子物料"
)
else: else:
# 情况1: 一级节点是物料(不是 device # 情况1: 一级节点是物料(不是 device
# 检查是否已存在 # 检查是否已存在