[英]How to convert buildbot Property to string value
问题:
我想弄清楚如何将 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
接口的类。 这个接口定义了一些东西,可以在需要时“渲染”成一个字符串。 要呈现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")
为此,您可以使用Interpolate
:
util.Interpolate('string before ' + '%(prop:artifact.output)s' + ' string after')
如果您有权访问BuildStep
object,则可以通过getProperty()
方法获取已格式化为字符串的属性。
如果您想将“workername”作为字符串获取并打印出来,您可以调用:
workerName = step.getProperties().getProperty('workername','wname')
print("workerName: %s" % workerName)
注意: workername
是您应该始终期望找到的常见构建属性之一,以及用户定义的属性。
getProperty()
在properties.py中定义:
def getProperty(self, name, default=None):
return self.properties.get(name, (default,))[0]
请记住将分支从“master”切换到您的 buildbot 版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.