在 Interpolate
中使用属性或源标记属性之前,是否有一种好的方法对其执行字符串转换?我们在分支名称中使用斜杠,我需要将斜杠转换为破折号,以便在文件名中使用它们。
也就是说,假设我有分支“feature/fix-all-the-things”,可通过 Interpolate("%(prop:branch)s")
或 Interpolate( “%(src::branch)s”)
。我希望能够将其转换为“功能修复所有事物”以进行一些插值。显然,它需要保持原来的形式,以便从源代码管理中选择合适的分支。
最佳答案
事实证明,我只需要继承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
关于python - 对 buildbot 构建属性执行字符串转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28634959/