Please note:The SCons wiki is now restored from the attack in March 2013. All old passwords have been invalidated. Please reset your password if you have an account. If you note missing pages, please report them to webmaster@scons.org. Also, new account creation is currently disabled due to an ongoing spam flood (2013/08/27).

This is a builder to help when you need to take a file and replace certain variables in it. (See also SubstInFileBuilder for another approach which may do better handling changes in the variable values.)

For example, when creating pkg-config files, autoconf will take a package.pc.in file and create package.pc from it, replacing things such as @libdir@ with the actual value.

We can do much better :-)

scanreplace.py:

   1 from string import Template
   2 
   3 def replace_action(target, source, env):
   4     open(str(target[0]), 'w').write(Template(open(str(source[0]), 'r').read()).safe_substitute(env))
   5     return 0
   6 
   7 def replace_string(target, source, env):
   8     return "building '%s' from '%s'" % (str(target[0]), str(source[0]))
   9 
  10 def generate(env, **kw):
  11     action = env.Action(replace_action, replace_string)
  12     env['BUILDERS']['ScanReplace'] = env.Builder(action=action, src_suffix='.in', single_source=True)
  13 
  14 def exists(env):
  15     return 1

To use this, place scanreplace.py into your tools directory (see the manpage for more details) and then do this:

   1 env = Environment(tools=['default', 'scanreplace'], toolpath=['tools'])
   2 env['prefix'] = '/usr/local'
   3 env.ScanReplace('myprogram.pc.in')

This will create myprogram.pc from myprogram.pc.in, replacing variables along the way.

Variables will be found in the environment, so in the previous example, any occurence of '$prefix' or '${prefix}' in myprogram.pc.in would be replaced with '/usr/local'.

Have fun!

ReplacementBuilder (last edited 2009-04-15 19:51:27 by GaryOberbrunner)