| 1 |
import re, os |
| 2 |
from SCons.Script import Builder |
| 3 |
from SCons.Action import Action |
| 4 |
from SCons.Node.FS import File |
| 5 |
def addTemplateBuilder(env): |
| 6 |
def buildEmitter(target, source, env): |
| 7 |
data = open(source[0].srcnode().get_path()).read() |
| 8 |
dict = env.Dictionary() |
| 9 |
keys = dict.keys() |
| 10 |
for key in keys: |
| 11 |
if isinstance(dict[key], str): |
| 12 |
env.Depends(target, env.Value(dict[key])) |
| 13 |
return target, source |
| 14 |
def buildFile(target, source, env): |
| 15 |
source_dest = str(target[0]) |
| 16 |
wfile = open(source_dest, "w") |
| 17 |
data = open(source[0].srcnode().get_path()).read() |
| 18 |
dict = env.Dictionary() |
| 19 |
keys = dict.keys() |
| 20 |
for key in keys: |
| 21 |
if isinstance(dict[key], str): |
| 22 |
data = re.sub("%" + key + "%", dict[key], data) |
| 23 |
wfile.write(data) |
| 24 |
wfile.close() |
| 25 |
return 0 |
| 26 |
def buildString(target, source, env): |
| 27 |
return "Preparing file %s" % os.path.basename(str(target[0])) |
| 28 |
env.Append(BUILDERS = { |
| 29 |
'Template': Builder( |
| 30 |
action = Action(buildFile, buildString), |
| 31 |
emitter = buildEmitter, |
| 32 |
), |
| 33 |
}) |