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

时间:2018-06-08 16:55:18

标签: python properties buildbot

问题:

我试图弄清楚如何将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年找到另一个人发帖,问同样的事情,但没有答案。

2 个答案:

答案 0 :(得分:1)

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

问题是从哪里获得这样的提供程序以及如何呈现它。实施自己的步骤时,可以使用Build实例,您可以从self.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不必一定是属性,它也可以是一个特技。现在,您可以使用带有可渲染对象的构建步骤进行创建:

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

答案 1 :(得分:-1)

您可以将Interpolate用于此目的:

util.Interpolate('string before ' + '%(prop:artifact.output)s' + ' string after')
相关问题