在buildbot构建属性上执行字符串转换?

时间:2015-02-20 17:36:39

标签: python buildbot

Interpolate中使用它之前,是否有一种在属性或源标记属性上执行字符串转换的好方法?我们在分支名称中使用斜杠,我需要将斜杠转换为破折号,以便我可以在文件名中使用它们。

也就是说,我有分支"功能/修复所有东西",可以Interpolate("%(prop:branch)s")Interpolate("%(src::branch)s")访问。我希望能够将它转换为"功能修复所有的东西"对于一些插值。显然,它需要保持其原始形式,以便从源代码控制中选择适当的分支。

2 个答案:

答案 0 :(得分:3)

事实证明,我只需要继承Interpolate

import re
from buildbot.process.properties import Interpolate


class InterpolateReplace(Interpolate):
    """Interpolate with regex replacements.

    This takes an additional argument, `patterns`, which is a list of
    dictionaries containing the keys "search" and "replace", corresponding to
    `pattern` and `repl` arguments to `re.sub()`.
    """
    def __init__(self, fmtstring, patterns, *args, **kwargs):
        Interpolate.__init__(self, fmtstring, *args, **kwargs)
        self._patterns = patterns

    def _sub(self, s):
        for pattern in self._patterns:
            search = pattern['search']
            replace = pattern['replace']
            s = re.sub(search, replace, s)
        return s

    def getRenderingFor(self, props):
        props = props.getProperties()
        if self.args:
            d = props.render(self.args)
            d.addCallback(lambda args:
                          self._sub(self.fmtstring % tuple(args)))
            return d
        else:
            d = props.render(self.interpolations)
            d.addCallback(lambda res:
                          self._sub(self.fmtstring % res))
            return d

答案 1 :(得分:0)

自从 buildbot v0.9.0Transform 一起使用后,似乎有一种更新、更简单的方法来执行此操作:

filename = util.Transform(
    lambda p: p.replace('/', '-'),
    util.Property('branch')
)
相关问题