【问题标题】:Performing string transformations on buildbot build properties?对 buildbot 构建属性执行字符串转换?
【发布时间】:2015-02-20 17:36:38
【问题描述】:

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

也就是说,假设我有分支“feature/fix-all-the-things”,可以通过Interpolate("%(prop:branch)s")Interpolate("%(src::branch)s") 访问。我希望能够将其转换为“功能修复所有事物”以进行一些插值。显然,它需要保持其原始形式才能从源代码管理中选择适当的分支。

【问题讨论】:

    标签: python buildbot


    【解决方案1】:

    原来,我只需要继承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
    

    【讨论】:

      【解决方案2】:

      buildbot v0.9.0Transform 以来,似乎有一种更新、更简单的方法:

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

      【讨论】:

        猜你喜欢
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多