incubus
incubus

Reputation: 1009

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. I really don't have much experience with buildbot other than what I have read in the docs and someone elses code. 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.

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. I also tried using util.Interpolate but again, it has the same issue. Finally, I tried str(util.Property("artifact.output")) but it just outputs Property("artifact.output").

Question:

Is it possible to retrieve a buildbot Property as a string value?

note: I was only able to find one other post from someone back on 2014 asking the same thing but no answer.

Upvotes: 5

Views: 1697

Answers (2)

stativ
stativ

Reputation: 1490

A Property is not a string by itself, but it's a class that implements an IRenderable interface. 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.

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.

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. Note that the arg does not have to be property, it can be a tring as well. 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")

Upvotes: 6

Rasmus Johansson
Rasmus Johansson

Reputation: 9

You can use Interpolate for that purpose:

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

Upvotes: -1

Related Questions