Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用'动态生成选项;getChoices';Buildbot中的成员函数?_Python_Buildbot - Fatal编程技术网

Python 如何使用'动态生成选项;getChoices';Buildbot中的成员函数?

Python 如何使用'动态生成选项;getChoices';Buildbot中的成员函数?,python,buildbot,Python,Buildbot,我已经有了一个方法,可以返回特定回购协议的分支列表,我想使用它根据Buildbot的web UI中返回的列表动态生成选择 例如,与此静态列表不同: c['schedulers'].append( schedulers.ForceScheduler( ..., branch=util.ChoiceStringParameter( ..., choices=['master', 'branch1', 'branc

我已经有了一个方法,可以返回特定回购协议的分支列表,我想使用它根据Buildbot的web UI中返回的列表动态生成选择

例如,与此静态列表不同:

c['schedulers'].append(
    schedulers.ForceScheduler(
        ...,
        branch=util.ChoiceStringParameter(
            ...,
            choices=['master', 'branch1', 'branch2', ...],
            ...
我想要动态生成的东西,比如:

def get_branches():
    refs = subprocess.check_output(["git", "ls-remote", "--heads", "git@bitbucket.org:foo/bar.git"])
    branches = []
    for item in refs.split('\n'):
        m = re.match(r"^\w+\trefs/heads/(.*$)", item)
        if m:
            branch = m.group(1)
            branches.append(branch)

    return branches

c['schedulers'].append(
    schedulers.ForceScheduler(
        ...,
        branch=util.ChoiceStringParameter(
            ...,
            choices=get_branches,
            ...
Buildbot文档解释说,可以通过对“getChoices”成员函数进行子类化和覆盖来实现。它还提供了InheritBuildParameter类的源代码,但我不知道如何使用“getChoices”成员函数


关于这个主题的文档太少了,我不得不问你这个问题!提前感谢:-)

使用生成器类是动态选择的一种方法:

class Branches(object):
    def __init__(self):
        self.branches = []
        refs = subprocess.check_output(["git", "ls-remote", "--heads", "git@bitbucket.org:foo/bar.git"])
        for item in refs.split('\n'):
            m = re.match(r"^\w+\trefs/heads/(.*)$", item)
            if m:
                branch = m.group(1)
                self.branches.append(branch)
    def __iter__(self):
        self.__init__()
        for b in self.branches:
            yield b

c['schedulers'].append( ForceScheduler(
        ...
        branch=ChoiceStringParameter(name="branch",
                                 choices=Branches(),
                                 default="develop"),
       ...