| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python2.4 | |
| 2 # Copyright 2008, Google Inc. | |
| 3 # All rights reserved. | |
| 4 # | |
| 5 # Redistribution and use in source and binary forms, with or without | |
| 6 # modification, are permitted provided that the following conditions are | |
| 7 # met: | |
| 8 # | |
| 9 # * Redistributions of source code must retain the above copyright | |
| 10 # notice, this list of conditions and the following disclaimer. | |
| 11 # * Redistributions in binary form must reproduce the above | |
| 12 # copyright notice, this list of conditions and the following disclaimer | |
| 13 # in the documentation and/or other materials provided with the | |
| 14 # distribution. | |
| 15 # * Neither the name of Google Inc. nor the names of its | |
| 16 # contributors may be used to endorse or promote products derived from | |
| 17 # this software without specific prior written permission. | |
| 18 # | |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 | |
| 31 """Search and replace builder for SCons.""" | |
| 32 | |
| 33 | |
| 34 import re | |
| 35 import SCons.Script | |
| 36 | |
| 37 | |
| 38 def ReplaceStrings(target, source, env): | |
| 39 """Replace Strings builder, does regex substitution on files. | |
| 40 | |
| 41 Args: | |
| 42 target: A single target file node. | |
| 43 source: A single input file node. | |
| 44 env: Environment in which to build. | |
| 45 | |
| 46 From env: | |
| 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 | |
| 49 pair (search_regex, replacement) in order. SCons variables in the | |
| 50 replacement strings will be evaluated. | |
| 51 | |
| 52 Returns: | |
| 53 The target node, a file with contents from source, with the substitutions | |
| 54 from REPLACE_STRINGS performed on it. | |
| 55 | |
| 56 For example: | |
| 57 env.ReplaceStrings('out', 'in', | |
| 58 REPLACE_STRINGS = [('a*', 'b'), ('b', 'CCC')]) | |
| 59 With 'in' having contents: Haaapy. | |
| 60 Outputs: HCCCpy. | |
| 61 """ | |
| 62 # Load text. | |
| 63 fh = open(source[0].abspath, 'rb') | |
| 64 text = fh.read() | |
| 65 fh.close() | |
| 66 # Do replacements. | |
| 67 for r in env['REPLACE_STRINGS']: | |
| 68 text = re.sub(r[0], env.subst(r[1]), text) | |
| 69 # Write it out. | |
| 70 fh = open(target[0].abspath, 'wb') | |
| 71 fh.write(text) | |
| 72 fh.close() | |
| 73 | |
| 74 | |
| 75 def generate(env): | |
| 76 # NOTE: SCons requires the use of this name, which fails gpylint. | |
| 77 """SCons entry point for this tool.""" | |
| 78 | |
| 79 # Add the builder | |
| 80 act = SCons.Script.Action(ReplaceStrings, varlist=['REPLACE_STRINGS']) | |
| 81 bld = SCons.Script.Builder(action=act, single_source=True) | |
| 82 env.Append(BUILDERS={'ReplaceStrings': bld}) | |
| OLD | NEW |