| 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 """Input gathering tool for SCons.""" |
| 32 |
| 33 |
| 34 import re |
| 35 import SCons.Script |
| 36 |
| 37 |
| 38 def GatherInputs(env, target, groups=['.*'], exclude_pattern=None): |
| 39 """Find all (non-generated) input files used for a target. |
| 40 |
| 41 Args: |
| 42 target: a target node to find source files for |
| 43 For example: File('bob.exe') |
| 44 groups: a list of patterns to use as categories |
| 45 For example: ['.*\\.c$', '.*\\.h$'] |
| 46 exclude_pattern: a pattern to exclude from the search |
| 47 For example: '.*third_party.*' |
| 48 Returns: |
| 49 A list of lists of files for each category. |
| 50 Each file will be placed in the first category which matches, |
| 51 even if categories overlap. |
| 52 For example: |
| 53 [['bob.c', 'jim.c'], ['bob.h', 'jim.h']] |
| 54 """ |
| 55 |
| 56 # Compile exclude pattern if any |
| 57 if exclude_pattern: |
| 58 exclude_pattern = re.compile(exclude_pattern) |
| 59 |
| 60 def _FindSources(ptrns, tgt, all): |
| 61 """Internal Recursive function to find all pattern matches.""" |
| 62 # Recursively process lists |
| 63 if SCons.Util.is_List(tgt): |
| 64 for t in tgt: |
| 65 _FindSources(ptrns, t, all) |
| 66 else: |
| 67 # Skip if we have been here before |
| 68 if tgt.abspath in all: return |
| 69 # Note that we have been here |
| 70 all[tgt.abspath] = True |
| 71 # Skip ones that match an exclude pattern, if we have one. |
| 72 if exclude_pattern and exclude_pattern.match(tgt.abspath): return |
| 73 |
| 74 # Handle non-leaf nodes recursively |
| 75 lst = tgt.children(scan=1) |
| 76 if lst: |
| 77 _FindSources(ptrns, lst, all) |
| 78 return |
| 79 |
| 80 # See who it matches |
| 81 for pattern, lst in ptrns.items(): |
| 82 # Get real file (backed by repositories). |
| 83 rfile = tgt.rfile() |
| 84 # Add to the list for the first pattern that matches. |
| 85 if pattern.match(rfile.path): |
| 86 lst.append(rfile.abspath) |
| 87 break |
| 88 |
| 89 # Prepare a group for each pattern. |
| 90 patterns = {} |
| 91 for g in groups: |
| 92 patterns[re.compile(g, re.IGNORECASE)] = [] |
| 93 |
| 94 # Do the search. |
| 95 _FindSources(patterns, target, {}) |
| 96 |
| 97 return patterns.values() |
| 98 |
| 99 |
| 100 def generate(env): |
| 101 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 102 """SCons entry point for this tool.""" |
| 103 |
| 104 # Add a method to gather all inputs needed by a target. |
| 105 env.AddMethod(GatherInputs, 'GatherInputs') |
| OLD | NEW |