[users at bb.net] WithProperties() and subclassed ShellCommand

Edmund Wong ewongbb at pw-wspx.org
Mon Jun 25 02:53:37 UTC 2018


Hi,

I'm looking at a thread from 2014, namely the one
on "withproperties or interpolate in ShellCommand):

https://lists.buildbot.net/pipermail/devel/2014-September/010878.html

I'm attempting to do something similar in that I was
hoping to put the setup command in the constructor.

The custom buildbot code was based on 0.8.2 version and
I'm attempting to upgrade it to 1.2.0 capable.

The code is as follows:

    def set_mock_command(self):
        # This variable is used to decide whether to wrap the

        # We need to have all commands as a string.  We'll
        # convert argv commands into string commands
        if isinstance(self.command, list):
            string_list = []
            for arg in self.command:
                if issubclass(arg.__class__, WithProperties):
                    string_list.append(quote(str(arg.fmtstring)))
                else:
                    string_list.append(quote(str(arg)))
            string_command = ' '.join(string_list)
        elif issubclass(self.command.__class__, WithProperties):
            string_command = self.command.fmtstring
        else:
            string_command = self.command
        mock_workdir =
self.mock_workdir_mutator(self.remote_kwargs['workdir'])

        # If the workdir is a WithProperties instance, we need to get
the format
        # string and wrap it in another WithProperties
        if issubclass(mock_workdir.__class__, WithProperties):
            mock_workdir = mock_workdir.fmtstring
        if self.mock_workdir_prefix is not None:
            mock_workdir = self.mock_workdir_prefix + mock_workdir

        if 'env' in self.remote_kwargs:
            pre_render_env = self.remote_kwargs['env']
            properties = self.build.getProperties()
            rendered_env = properties.render(pre_render_env)
            environment = ' '.join('%s="%s"' % (k, rendered_env[k])
                                   for k in rendered_env.keys())
        else:
            environment = ''

        self.command = [self.mock_login, '-v', '-r', self.target,
                        '--cwd', WithProperties(mock_workdir)] + \
            self.mock_args + ['--shell'] + \
            [WithProperties('/usr/bin/env %s %s' % (environment,
                                                    string_command))]

    def start(self):
        if self.mock:
            self.set_mock_command()
        self.super_class.start(self)

But with 1.2.0,  I need to, according to the thread, move the
set_mock_command() to the constructor.

I've modified the set_mock_command() code to:

    def set_mock_command(self):
        def replace_property(prop_item):
            m = prop_item.split(')s')
            retval = []
            for item in m:
                if '%(' in item:
                    v = item.replace('%(', '').replace("'", "")
                    retval.append(self.getProperty(v))
                else:
                    retval.append(item.replace("'", ""))
            return retval

        # This variable is used to decide whether to wrap the

        # We need to have all commands as a string.  We'll
        # convert argv commands into string commands
        if isinstance(self.command, list):
            string_list = []
            for arg in self.command:
                if issubclass(arg.__class__, WithProperties):
                    string_list.append(quote(str(arg.fmtstring)))
                else:
                    string_list.append(quote(str(arg)))
            string_command = ' '.join(string_list)
        elif issubclass(self.command.__class__, WithProperties):
            string_command = self.command.fmtstring
        else:
            string_command = self.command
        mock_workdir =
self.mock_workdir_mutator(self.remote_kwargs['workdir'])

        # If the workdir is a WithProperties instance, we need to get
the format
        # string and wrap it in another WithProperties
        if issubclass(mock_workdir.__class__, WithProperties):
            mock_workdir = mock_workdir.fmtstring
        if self.mock_workdir_prefix is not None:
            mock_workdir = self.mock_workdir_prefix + mock_workdir

        if 'env' in self.remote_kwargs:
            pre_render_env = self.remote_kwargs['env']
            for item in pre_render_env:
                env_item = pre_render_env[item]
                if issubclass(env_item.__class__, WithProperties):
                    tmp = quote(str(env_item.fmtstring))
                    replace_property(tmp)
                    r = tmp[tmp.index(')s') + 2:]
                    print(" --- > %s" % r)
                    tmp = tmp.replace('%(', '').replace(')s', '')
                    if ':' in tmp:
                        tmp = tmp[:tmp.index(':')]
                    pre_render_env[item] = self.getProperty(tmp)
            properties = self.build.getProperties()
            rendered_env = properties.render(pre_render_env)
            environment = ' '.join('%s="%s"' % (k, rendered_env[k])
                                   for k in rendered_env.keys())
        else:
            environment = ''

        self.command = [self.mock_login, '-v', '-r', self.target,
                        '--cwd', WithProperties(mock_workdir)] + \
            self.mock_args + ['--shell'] + \
            [WithProperties('/usr/bin/env %s %s' % (environment,
                                                    string_command))]


But this fails at the replace_property() function when trying
to do a checkconfig:

error while parsing config file:
Traceback (most recent call last):
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/twisted/internet/defer.py",
line 150, in maybeDeferred
    result = f(*args, **kw)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbot/scripts/checkconfig.py",
line 60, in checkconfig
    return _loadConfig(basedir=basedir, configFile=configFile, quiet=quiet)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbot/scripts/checkconfig.py",
line 30, in _loadConfig
    config.FileLoader(basedir, configFile).loadConfig()
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbot/config.py",
line 182, in loadConfig
    self.basedir, self.configFileName)
--- <exception caught here> ---
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbot/config.py",
line 140, in loadConfigDict
    execfile(filename, localDict)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/twisted/python/compat.py",
line 246, in execfile
    exec(code, globals, locals)
  File "/builds/buildbot/master01/master/master.cfg", line 42, in <module>
    branchObjects = generateCCBranchObjects(BRANCHES[branch], branch)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/misc.py",
line 2302, in generateCCBranchObjects
    mozilla2_dep_factory = factory_class(**factory_kwargs)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/process/factory.py",
line 2897, in __init__
    **kwargs)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/process/factory.py",
line 2382, in __init__
    **kwargs)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/process/factory.py",
line 1234, in __init__
    self.addBuildSteps()
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/process/factory.py",
line 1320, in addBuildSteps
    self.addConfigSteps()
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/process/factory.py",
line 1447, in addConfigSteps
    self.addTooltoolStep(workdir='build')
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/process/factory.py",
line 344, in addTooltoolStep
    **kwargs
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/steps/mock.py",
line 55, in __init__
    self.set_mock_command()
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/steps/mock.py",
line 110, in set_mock_command
    replace_property(tmp)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbotcustom/steps/mock.py",
line 73, in replace_property
    retval.append(self.getProperty(v))
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbot/process/properties.py",
line 244, in getProperty
    props = IProperties(self)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/twisted/python/components.py",
line 104, in _hook
    return factory(ob)
  File
"/builds/buildbot/master01/lib/python2.7/site-packages/buildbot/process/buildstep.py",
line 937, in <lambda>
    lambda step: interfaces.IProperties(step.build),
exceptions.TypeError: ('Could not adapt', None, <InterfaceClass
buildbot.interfaces.IProperties>)

Configuration Errors:
  error while parsing config file: ('Could not adapt', None,
<InterfaceClass buildbot.interfaces.IProperties>) (traceback in logfile)
make: *** [checkconfig] Error 1


Does this mean the properties aren't available at the constructor level?

Any help appreciated,

Edmund


More information about the users mailing list