简体   繁体   English

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

[英]How to convert buildbot Property to string value

Problem:问题:

I am trying to figure out how to convert a buildbot Property into a string value.我想弄清楚如何将 buildbot 属性转换为字符串值。 I really don't have much experience with buildbot other than what I have read in the docs and someone elses code.除了我在文档和其他人的代码中阅读的内容外,我对 buildbot 的经验真的不多。 The issue is I have a Property that contains a path.问题是我有一个包含路径的属性。 I need to get the path as a string so that I can use some python functions such as 'split' and 'basename' to retrieve specific elements of the path.我需要将路径作为字符串获取,以便我可以使用一些 python 函数(例如“split”和“basename”)来检索路径的特定元素。

What I Have Tried:我试过的:

There is a property mapped like so有一个像这样映射的属性

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

When I call path.os.basename(util.Property("artifact.output")) it complains that Property has no 'rfind' method.当我调用path.os.basename(util.Property("artifact.output"))它抱怨 Property 没有 'rfind' 方法。 I also tried using util.Interpolate but again, it has the same issue.我也尝试使用util.Interpolate但同样,它有同样的问题。 Finally, I tried str(util.Property("artifact.output")) but it just outputs Property("artifact.output") .最后,我尝试str(util.Property("artifact.output"))但它只是输出Property("artifact.output")

Question:问题:

Is it possible to retrieve a buildbot Property as a string value?是否可以将 buildbot 属性检索为字符串值?

note: I was only able to find one other post from someone back on 2014 asking the same thing but no answer.注意:我只能从 2014 年的某人那里找到另一篇帖子,问同样的事情但没有答案。

A Property is not a string by itself, but it's a class that implements an IRenderable interface. Property本身不是字符串,而是实现IRenderable接口的类。 This interface defines something, that can be "rendered" into a string when needed.这个接口定义了一些东西,可以在需要时“渲染”成一个字符串。 To render a Property or any renderable (eg. the util.Interpolate object), you need a an IProperties provider.要呈现Property或任何可呈现对象(例如util.Interpolate对象),您需要一个IProperties提供程序。

The question is where to get such provider and how to render it.问题是从哪里获得这样的提供者以及如何呈现它。 When implementing your own step, you can use the Build instance that you can access from self.build as such provider and use it to render the property.在实现您自己的步骤时,您可以使用可以从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)

In the example above, the ExampleBuildStep takes an argument arg that will be rendered inside the run() function.在上面的示例中, ExampleBuildStep接受一个参数 arg,该参数将在run()函数内呈现。 Note that the arg does not have to be property, it can be a tring as well.请注意,arg 不一定是属性,它也可以是一个字符串。 You can now create use the build step with renderables:您现在可以使用可渲染对象创建使用构建步骤:

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

为此,您可以使用Interpolate

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

If you have access to the BuildStep object, you can grab properties already formatted as a string via the getProperty() method.如果您有权访问BuildStep object,则可以通过getProperty()方法获取已格式化为字符串的属性。

If you wanted to grab the "workername" as a string and print it, you could call:如果您想将“workername”作为字符串获取并打印出来,您可以调用:

workerName = step.getProperties().getProperty('workername','wname')
print("workerName: %s" % workerName)

Note: workername is one of the Common Build Properties you should always expect to find, alongside the user-defined ones.注意: workername是您应该始终期望找到的常见构建属性之一,以及用户定义的属性。

getProperty() is defined in properties.py : getProperty()properties.py中定义:

def getProperty(self, name, default=None):
    return self.properties.get(name, (default,))[0]

Remember to switch branches from 'master' to whatever your version of buildbot is.请记住将分支从“master”切换到您的 buildbot 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM