You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Buildbot的NestedParameter中获取单个属性?

解决Buildbot嵌套属性访问问题

问题根源

Buildbot的%(prop:...)插值语法仅支持访问顶层属性,无法直接解析嵌套字典的层级结构;而你之前错误地将自定义renderer函数放在插值字符串中调用,导致出现"invalid Interpolate selector"错误——renderer函数需要直接作为参数传入,不能用插值语法引用。

可行方案

方案1:使用util.renderer自定义渲染函数

通过装饰器定义一个获取嵌套属性的函数,直接在需要的步骤中引用该函数,无需插值语法。

from buildbot.plugins import util
import json

@util.renderer
def get_linux_artifact_flag(props):
    # 获取顶层artifacts属性,默认空字典避免KeyError
    artifacts = props.getProperty('artifacts', {})
    # 处理属性可能是字符串序列化的情况(如果NestedParameter存储的是JSON字符串)
    if isinstance(artifacts, str):
        artifacts = json.loads(artifacts)
    # 逐层获取嵌套属性,设置默认值
    return str(artifacts.get('tools', {}).get('linux', False))

# 在ShellCommand步骤中使用
build_factory = util.BuildFactory()
build_factory.addStep(util.ShellCommand(
    command=['your-downstream-command', '--enable-linux', get_linux_artifact_flag]
))

方案2:使用Interpolate结合Lambda表达式

无需单独定义函数,直接通过Lambda表达式在Interpolate中解析嵌套属性,适合简单场景。

from buildbot.plugins import util
import json

build_factory.addStep(util.ShellCommand(
    command=util.Interpolate(
        lambda props: [
            'your-downstream-command',
            '--enable-linux',
            str(json.loads(props.getProperty('artifacts', '{}')).get('tools', {}).get('linux', False))
        ]
    )
))

方案3:预处理嵌套属性(可选)

如果需要在多个步骤中复用嵌套属性,可以在构建初期添加一个步骤,将嵌套属性提取为顶层属性,后续直接用%(prop:...)访问。

from buildbot.plugins import util
import json

build_factory.addStep(util.MasterShellCommand(
    command=util.Interpolate(
        lambda props: [
            'buildbot', 'set-property', '--property', 'tools_linux',
            str(json.loads(props.getProperty('artifacts', '{}')).get('tools', {}).get('linux', False))
        ]
    )
))

# 后续步骤直接访问顶层属性
build_factory.addStep(util.ShellCommand(
    command=['your-downstream-command', '--enable-linux', '%(prop:tools_linux)s']
))

注意事项

  • 如果artifacts属性存储的是JSON字符串而非字典,必须先用json.loads()反序列化,否则会出现字符串索引错误。
  • 所有嵌套属性访问都要添加默认值,避免因属性缺失导致构建失败。

内容的提问来源于stack exchange,提问作者XonqNopp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 22:32:37