Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 1 | import re |
| 2 | from SCons.Script import * # the usual scons stuff you get in a SConscript |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 3 | import collections |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 4 | |
| 5 | def generate(env): |
| 6 | """ |
| 7 | Add builders and construction variables for the |
| 8 | SubstInFile tool. |
| 9 | |
| 10 | Adds SubstInFile builder, which substitutes the keys->values of SUBST_DICT |
| 11 | from the source to the target. |
| 12 | The values of SUBST_DICT first have any construction variables expanded |
| 13 | (its keys are not expanded). |
| 14 | If a value of SUBST_DICT is a python callable function, it is called and |
| 15 | the result is expanded as the value. |
| 16 | If there's more than one source and more than one target, each target gets |
| 17 | substituted from the corresponding source. |
| 18 | """ |
| 19 | def do_subst_in_file(targetfile, sourcefile, dict): |
| 20 | """Replace all instances of the keys of dict with their values. |
| 21 | For example, if dict is {'%VERSION%': '1.2345', '%BASE%': 'MyProg'}, |
| 22 | then all instances of %VERSION% in the file will be replaced with 1.2345 etc. |
| 23 | """ |
| 24 | try: |
| 25 | f = open(sourcefile, 'rb') |
| 26 | contents = f.read() |
| 27 | f.close() |
| 28 | except: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 29 | raise SCons.Errors.UserError("Can't read source file %s"%sourcefile) |
| 30 | for (k,v) in list(dict.items()): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 31 | contents = re.sub(k, v, contents) |
| 32 | try: |
| 33 | f = open(targetfile, 'wb') |
| 34 | f.write(contents) |
| 35 | f.close() |
| 36 | except: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 37 | raise SCons.Errors.UserError("Can't write target file %s"%targetfile) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 38 | return 0 # success |
| 39 | |
| 40 | def subst_in_file(target, source, env): |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 41 | if 'SUBST_DICT' not in env: |
| 42 | raise SCons.Errors.UserError("SubstInFile requires SUBST_DICT to be set.") |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 43 | d = dict(env['SUBST_DICT']) # copy it |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 44 | for (k,v) in list(d.items()): |
| 45 | if isinstance(v, collections.Callable): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 46 | d[k] = env.subst(v()).replace('\\','\\\\') |
| 47 | elif SCons.Util.is_String(v): |
| 48 | d[k] = env.subst(v).replace('\\','\\\\') |
| 49 | else: |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 50 | raise SCons.Errors.UserError("SubstInFile: key %s: %s must be a string or callable"%(k, repr(v))) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 51 | for (t,s) in zip(target, source): |
| 52 | return do_subst_in_file(str(t), str(s), d) |
| 53 | |
| 54 | def subst_in_file_string(target, source, env): |
| 55 | """This is what gets printed on the console.""" |
| 56 | return '\n'.join(['Substituting vars from %s into %s'%(str(s), str(t)) |
| 57 | for (t,s) in zip(target, source)]) |
| 58 | |
| 59 | def subst_emitter(target, source, env): |
| 60 | """Add dependency from substituted SUBST_DICT to target. |
| 61 | Returns original target, source tuple unchanged. |
| 62 | """ |
| 63 | d = env['SUBST_DICT'].copy() # copy it |
Christopher Dunn | bd1e895 | 2014-11-19 23:30:47 -0600 | [diff] [blame] | 64 | for (k,v) in list(d.items()): |
| 65 | if isinstance(v, collections.Callable): |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 66 | d[k] = env.subst(v()) |
| 67 | elif SCons.Util.is_String(v): |
| 68 | d[k]=env.subst(v) |
| 69 | Depends(target, SCons.Node.Python.Value(d)) |
| 70 | return target, source |
| 71 | |
| 72 | ## env.Append(TOOLS = 'substinfile') # this should be automaticaly done by Scons ?!? |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 73 | subst_action = SCons.Action.Action(subst_in_file, subst_in_file_string) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 74 | env['BUILDERS']['SubstInFile'] = Builder(action=subst_action, emitter=subst_emitter) |
| 75 | |
| 76 | def exists(env): |
| 77 | """ |
| 78 | Make sure tool exists. |
| 79 | """ |
| 80 | return True |