SCons需要变量()

时间:2015-04-17 12:00:13

标签: scons

我正在尝试设置build config file using the Variables class in SCons

我想知道,有没有办法要求设置变量,要么是通过命令行,要么是我要导入的“配置文件”? / p>

cfg = Variables('build.cfg', ARGUMENTS)                 # include cmdline args
cfg.Add('SOMEVAR', 'Some build config variable (required)')
cfg.Add('OTHERVAR', 'Another build config variable (required)')

base_env = Environment(variables = cfg)

当然我可以手动检查是否设置了所需的构建变量,但似乎这是一个常见的要求,在其他地方更好地处理:

try:
    base_env['SOMEVAR']
    base_env['OTHERVAR']
except KeyError as e:
    raise StopError('Required variable not set: {0}'.format(e))

1 个答案:

答案 0 :(得分:2)

我继续编码(并测试)了这个,取代了之前的答案。

from SCons.Variables import Variables

class VariablesRequired(Variables):
    def __init__(self, files=[], args={}, is_global=1):
        self.required = []
        super(self.__class__,self).__init__(files, args, is_global)

    def Add(self, key, help="", default=None, validator=None, converter=None, required=False):
        Variables.Add(self, key, help, default, validator, converter)
        if required:
            print "adding required option ", key[0]
            self.required.append(key[0])

    def Update(self, env):
        print "required options are: ", self.required
        Variables.Update(self, env)
        for requirement in self.required:
            if not env.has_key(requirement):
                print 'violation: ', requirement

cfg = VariablesRequired('dummy.py', ARGUMENTS)
cfg.Add('x', 'first dummy is x', required = True)
cfg.Add('y', 'second dummy is y', required = True)

结果:

$ cat dummy.py
x=1

$ scons {project}
scons: Reading SConscript files ...
adding required option  x
adding required option  y
required options are:  ['x', 'y']
violation:  y