| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2008, Google Inc. | 2 # Copyright 2008, Google Inc. |
| 3 # All rights reserved. | 3 # All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 """Replace Strings builder, does regex substitution on files. | 39 """Replace Strings builder, does regex substitution on files. |
| 40 | 40 |
| 41 Args: | 41 Args: |
| 42 target: A single target file node. | 42 target: A single target file node. |
| 43 source: A single input file node. | 43 source: A single input file node. |
| 44 env: Environment in which to build. | 44 env: Environment in which to build. |
| 45 | 45 |
| 46 From env: | 46 From env: |
| 47 REPLACE_STRINGS: A list of pairs of regex search and replacement strings. | 47 REPLACE_STRINGS: A list of pairs of regex search and replacement strings. |
| 48 The body of the source file has substitution performed on each | 48 The body of the source file has substitution performed on each |
| 49 pair (search_regex, replacement) in order. | 49 pair (search_regex, replacement) in order. SCons variables in the |
| 50 replacement strings will be evaluated. |
| 50 | 51 |
| 51 Returns: | 52 Returns: |
| 52 The target node, a file with contents from source, with the substitutions | 53 The target node, a file with contents from source, with the substitutions |
| 53 from REPLACE_STRINGS performed on it. | 54 from REPLACE_STRINGS performed on it. |
| 54 | 55 |
| 55 For example: | 56 For example: |
| 56 env.ReplaceStrings('out', 'in', | 57 env.ReplaceStrings('out', 'in', |
| 57 REPLACE_STRINGS = [('a*', 'b'), ('b', 'CCC')]) | 58 REPLACE_STRINGS = [('a*', 'b'), ('b', 'CCC')]) |
| 58 With 'in' having contents: Haaapy. | 59 With 'in' having contents: Haaapy. |
| 59 Outputs: HCCCpy. | 60 Outputs: HCCCpy. |
| 60 """ | 61 """ |
| 61 # Load text. | 62 # Load text. |
| 62 fh = open(source[0].abspath, 'rb') | 63 fh = open(source[0].abspath, 'rb') |
| 63 text = fh.read() | 64 text = fh.read() |
| 64 fh.close() | 65 fh.close() |
| 65 # Do replacements. | 66 # Do replacements. |
| 66 for r in env['REPLACE_STRINGS']: | 67 for r in env['REPLACE_STRINGS']: |
| 67 text = re.sub(r[0], r[1], text) | 68 text = re.sub(r[0], env.subst(r[1]), text) |
| 68 # Write it out. | 69 # Write it out. |
| 69 fh = open(target[0].abspath, 'wb') | 70 fh = open(target[0].abspath, 'wb') |
| 70 fh.write(text) | 71 fh.write(text) |
| 71 fh.close() | 72 fh.close() |
| 72 | 73 |
| 73 | 74 |
| 74 def generate(env): | 75 def generate(env): |
| 75 # NOTE: SCons requires the use of this name, which fails gpylint. | 76 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 76 """SCons entry point for this tool.""" | 77 """SCons entry point for this tool.""" |
| 77 | 78 |
| 78 # Add the builder | 79 # Add the builder |
| 79 act = SCons.Script.Action(ReplaceStrings, varlist=['REPLACE_STRINGS']) | 80 act = SCons.Script.Action(ReplaceStrings, varlist=['REPLACE_STRINGS']) |
| 80 bld = SCons.Script.Builder(action=act, single_source=True) | 81 bld = SCons.Script.Builder(action=act, single_source=True) |
| 81 env.Append(BUILDERS={'ReplaceStrings': bld}) | 82 env.Append(BUILDERS={'ReplaceStrings': bld}) |
| OLD | NEW |