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