| 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 # Get key to use for tracking whether we've seen this node | |
| 68 target_abspath = None | |
| 69 if hasattr(tgt, 'abspath'): | |
| 70 # Use target's absolute path as the key | |
| 71 target_abspath = tgt.abspath | |
| 72 target_key = target_abspath | |
| 73 else: | |
| 74 # Hope node's representation is unique enough (the default repr | |
| 75 # contains a pointer to the target as a string). This works for | |
| 76 # Alias() nodes. | |
| 77 target_key = repr(tgt) | |
| 78 | |
| 79 # Skip if we have been here before | |
| 80 if target_key in all: return | |
| 81 # Note that we have been here | |
| 82 all[target_key] = True | |
| 83 # Skip ones that match an exclude pattern, if we have one. | |
| 84 if (exclude_pattern and target_abspath | |
| 85 and exclude_pattern.match(target_abspath)): | |
| 86 return | |
| 87 | |
| 88 # Handle non-leaf nodes recursively | |
| 89 lst = tgt.children(scan=1) | |
| 90 if lst: | |
| 91 _FindSources(ptrns, lst, all) | |
| 92 return | |
| 93 | |
| 94 # Get real file (backed by repositories). | |
| 95 rfile = tgt.rfile() | |
| 96 rfile_is_file = rfile.isfile() | |
| 97 # See who it matches | |
| 98 for pattern, lst in ptrns.items(): | |
| 99 # Add files to the list for the first pattern that matches (implicitly, | |
| 100 # don't add directories). | |
| 101 if rfile_is_file and pattern.match(rfile.path): | |
| 102 lst.append(rfile.abspath) | |
| 103 break | |
| 104 | |
| 105 # Prepare a group for each pattern. | |
| 106 patterns = {} | |
| 107 for g in groups: | |
| 108 patterns[re.compile(g, re.IGNORECASE)] = [] | |
| 109 | |
| 110 # Do the search. | |
| 111 _FindSources(patterns, target, {}) | |
| 112 | |
| 113 return patterns.values() | |
| 114 | |
| 115 | |
| 116 def generate(env): | |
| 117 # NOTE: SCons requires the use of this name, which fails gpylint. | |
| 118 """SCons entry point for this tool.""" | |
| 119 | |
| 120 # Add a method to gather all inputs needed by a target. | |
| 121 env.AddMethod(GatherInputs, 'GatherInputs') | |
| OLD | NEW |