【问题标题】:How to convert buildbot Property to string value如何将 buildbot 属性转换为字符串值
【发布时间】:2018-11-18 18:56:57
【问题描述】:

问题:

我想弄清楚如何将 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 年找到某人的另一篇帖子,问同样的问题但没有答案。

【问题讨论】:

    标签: python properties buildbot


    【解决方案1】:

    Property 本身并不是一个字符串,而是一个实现了IRenderable 接口的类。这个接口定义了一些东西,可以在需要时“渲染”成一个字符串。要渲染 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 不必是属性,也可以是字符串。您现在可以使用可渲染的构建步骤来创建:

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

    【讨论】:

      【解决方案2】:

      您可以为此目的使用Interpolate

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

      【讨论】:

      • 这不会像作者指出的 2014 年的帖子那样工作 stackoverflow.com/questions/23864100/…
      • 我在 Interpolate 看到过类似的问题。将 Interpolate 作为输入传递给 ShellCommand 或其他 buildbot 函数时它工作正常,但是当我将其用作我自己的类的 arg 时,它抱怨它不是字符串类型。奇怪
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多