Skip to main content
 首页 » 编程设计

python之如何将 buildbot 属性转换为字符串值

2025年05月04日21findumars

问题:

我想弄清楚如何将 buildbot 属性转换为字符串值。除了我在文档和其他人的代码中阅读的内容外,我对 buildbot 的经验真的不多。 问题是我有一个包含路径的属性。我需要获取字符串形式的路径,以便我可以使用一些 python 函数(例如“split”和“basename”)来检索路径的特定元素。

我尝试过的:

有一个像这样映射的属性

"artifact.output":"S3://dev/artifacts/out/package1.tar.gz"

当我调用 path.os.basename(util.Property("artifact.output")) 时,它会提示 Property 没有“rfind”方法。我也尝试使用 util.Interpolate 但同样,它有同样的问题。最后,我尝试了 str(util.Property("artifact.output")) 但它只是输出 Property("artifact.output")。

问题:

是否可以将 buildbot 属性检索为字符串值?

注意:我只能从 2014 年的某人那里找到另一篇帖子,问同样的事情但没有答案。

请您参考如下方法:

Property 本身不是一个字符串,而是一个实现了 IRenderable 接口(interface)的类。该接口(interface)定义了一些东西,可以在需要时将其“呈现”为字符串。要呈现 Property 或任何可呈现对象(例如 util.Interpolate 对象),您需要一个 IProperties 提供程序。

问题是从哪里获得这样的提供者以及如何呈现它。实现您自己的步骤时,您可以使用可从 self.build 访问的 Build 实例作为此类提供程序,并使用它来呈现属性。

class ExampleBuildStep(BuildStep): 
    def __init__(self, arg, **kwargs): 
        """ 
        Args: 
            arg - any string, Property or any renderable that will be rendered in run 
        """ 
        self.arg = arg 
        super().__init__(**kwargs) 
 
    @defer.inlineCallbacks 
    def run(self): 
        # the renderedcommand will be the string representation of the self.arg 
        renderedcommand = yield self.build.render(self.arg) 

在上面的示例中,ExampleBuildStep 接受一个参数 arg,它将在 run() 函数中呈现。请注意,arg 不必是属性,它也可以是 tring。您现在可以使用带有可渲染对象的构建步骤来创建:

step = ExampleBuildStep(util.Property("artifact.output")) 
step = ExampleBuildStep(util.Interpolate('%(prop:artifact.output)s')) 
step = ExampleBuildStep("string argument")