1# handle substitution of variables in .in files
2
3import sys
4import re
5import os
6from waflib import Build, Logs
7from samba_utils import SUBST_VARS_RECURSIVE
8
9def subst_at_vars(task):
10    '''substiture @VAR@ style variables in a file'''
11
12    env = task.env
13    s = task.inputs[0].read()
14
15    # split on the vars
16    a = re.split('(@\w+@)', s)
17    out = []
18    for v in a:
19        if re.match('@\w+@', v):
20            vname = v[1:-1]
21            if not vname in task.env and vname.upper() in task.env:
22                vname = vname.upper()
23            if not vname in task.env:
24                Logs.error("Unknown substitution %s in %s" % (v, task.name))
25                sys.exit(1)
26            v = SUBST_VARS_RECURSIVE(task.env[vname], task.env)
27        out.append(v)
28    contents = ''.join(out)
29    task.outputs[0].write(contents)
30    return 0
31
32def CONFIGURE_FILE(bld, in_file, **kwargs):
33    '''configure file'''
34
35    base=os.path.basename(in_file)
36    t = bld.SAMBA_GENERATOR('INFILE_%s' % base,
37                            rule = subst_at_vars,
38                            source = in_file + '.in',
39                            target = in_file,
40                            vars = kwargs)
41Build.BuildContext.CONFIGURE_FILE = CONFIGURE_FILE
42