cover_params.sh default为空问题排查

This commit is contained in:
zhangkun9038@dingtalk.com 2025-11-19 09:39:48 +08:00
parent 662033d350
commit b20724ccdc

View File

@ -247,12 +247,31 @@ override_hyperopt_params() {
# 获取参数值
local value=$(jq -r ".params.$category.$param" "$json_file")
log "debug" " 原始值: $value"
# 检查value是否为空或null
if [ -z "$value" ] || [ "$value" = "null" ]; then
log "error" "参数 $param 的默认值为空或未定义,无法进行参数覆盖,请检查 $json_file 中的配置"
return 1
fi
# 特殊处理字符串值,添加引号
if [[ "$value" =~ ^[a-zA-Z]+$ ]] && [[ ! "$value" == "true" ]] && [[ ! "$value" == "false" ]]; then
value="\"$value\""
fi
log "debug" " 处理参数: $param = $value"
log "debug" " 处理后的值: $value"
log "debug" " 参数: $param = $value"
# 检查参数是否存在
if grep -q "$param.*default=" "$py_file"; then
log "debug" " 找到参数: $param"
# 显示原始行
local original_line=$(grep "$param.*default=" "$py_file" | head -1)
log "debug" " 原始行: $original_line"
else
log "warn" " 参数 $param$py_file 中未找到"
fi
# 构建sed命令来替换Python文件中的默认值
if [ "$dry_run" = true ]; then
@ -270,7 +289,16 @@ override_hyperopt_params() {
# 使用sed替换默认值保留其他属性完全兼容macOS
# 创建临时文件保存替换结果
local temp_file=$(mktemp)
sed "s/\($param.*default=\)[^,}]*/\\1$value/" "$py_file" > "$temp_file"
log "debug" " sed替换模式: s/\\($param.*default=\\)[^,}]*/\\1$value/"
log "debug" " 执行 sed 替换..."
sed "s/\\($param.*default=\\)[^,}]*/\\1$value/" "$py_file" > "$temp_file"
log "debug" " 替换结果保存到临时文件: $temp_file"
# 检查替换是否成功
if grep -q "$param.*default=$value" "$temp_file"; then
log "debug" " 替换成功,更新文件..."
else
log "warn" " 替换未成功或值未与预旧符合"
fi
# 将临时文件内容复制回原文件
mv "$temp_file" "$py_file"
log "info" " 已更新参数 $param 的默认值为 $value"