| 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. |
| 50 |
| 51 Returns: |
| 52 The target node, a file with contents from source, with the substitutions |
| 53 from REPLACE_STRINGS performed on it. |
| 54 |
| 55 For example: |
| 56 env.ReplaceStrings('out', 'in', |
| 57 REPLACE_STRINGS = [('a*', 'b'), ('b', 'CCC')]) |
| 58 With 'in' having contents: Haaapy. |
| 59 Outputs: HCCCpy. |
| 60 """ |
| 61 # Load text. |
| 62 fh = open(source[0].abspath, 'rb') |
| 63 text = fh.read() |
| 64 fh.close() |
| 65 # Do replacements. |
| 66 for r in env['REPLACE_STRINGS']: |
| 67 text = re.sub(r[0], r[1], text) |
| 68 # Write it out. |
| 69 fh = open(target[0].abspath, 'wb') |
| 70 fh.write(text) |
| 71 fh.close() |
| 72 |
| 73 |
| 74 def generate(env): |
| 75 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 76 """SCons entry point for this tool.""" |
| 77 |
| 78 # Add the builder |
| 79 act = SCons.Script.Action(ReplaceStrings, varlist=['REPLACE_STRINGS']) |
| 80 bld = SCons.Script.Builder(action=act, single_source=True) |
| 81 env.Append(BUILDERS={'ReplaceStrings': bld}) |
| OLD | NEW |