| 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 16 matching lines...) Expand all Loading... |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 """Defer tool for SCons.""" | 31 """Defer tool for SCons.""" |
| 32 | 32 |
| 33 | 33 |
| 34 import os | 34 import os |
| 35 import sys | 35 import sys |
| 36 import types | 36 import types |
| 37 | 37 import SCons.Errors |
| 38 | 38 |
| 39 __defer_groups = {} | 39 __defer_groups = {} |
| 40 | 40 |
| 41 | 41 |
| 42 def _InitializeDefer(self): | 42 def _InitializeDefer(self): |
| 43 """Re-initializes deferred function handling. | 43 """Re-initializes deferred function handling. |
| 44 | 44 |
| 45 Args: | 45 Args: |
| 46 self: Parent environment | 46 self: Parent environment |
| 47 """ | 47 """ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 66 continue # Still have dependencies | 66 continue # Still have dependencies |
| 67 if group.func_env_cwd: | 67 if group.func_env_cwd: |
| 68 # Run all the functions in our named group | 68 # Run all the functions in our named group |
| 69 for func, env, cwd in group.func_env_cwd: | 69 for func, env, cwd in group.func_env_cwd: |
| 70 os.chdir(cwd) | 70 os.chdir(cwd) |
| 71 func(env) | 71 func(env) |
| 72 did_work = True | 72 did_work = True |
| 73 del __defer_groups[name] | 73 del __defer_groups[name] |
| 74 break | 74 break |
| 75 if not did_work: | 75 if not did_work: |
| 76 print 'Error in _ExecuteDefer: dependency cycle detected.' | 76 errmsg = 'Error in _ExecuteDefer: dependency cycle detected.\n' |
| 77 for name, group in __defer_groups.items(): | 77 for name, group in __defer_groups.items(): |
| 78 print ' %s after: %s' % (name, group.after) | 78 errmsg += ' %s after: %s\n' % (name, group.after) |
| 79 # TODO(rspangler): should throw exception? | 79 raise SCons.Errors.UserError(errmsg) |
| 80 sys.exit(1) | |
| 81 | 80 |
| 82 # Restore directory | 81 # Restore directory |
| 83 os.chdir(oldcwd) | 82 os.chdir(oldcwd) |
| 84 | 83 |
| 85 | 84 |
| 86 class DeferFunc(object): | 85 class DeferFunc(object): |
| 87 """Named list of functions to be deferred.""" | 86 """Named list of functions to be deferred.""" |
| 88 | 87 |
| 89 def __init__(self): | 88 def __init__(self): |
| 90 """Initialize deferred function object.""" | 89 """Initialize deferred function object.""" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 144 |
| 146 # Get list of names and/or functions this function should defer until after | 145 # Get list of names and/or functions this function should defer until after |
| 147 after = [] | 146 after = [] |
| 148 for a in self.Flatten(kwargs.get('after')): | 147 for a in self.Flatten(kwargs.get('after')): |
| 149 if isinstance(a, str): | 148 if isinstance(a, str): |
| 150 after.append(a) | 149 after.append(a) |
| 151 elif isinstance(a, types.FunctionType): | 150 elif isinstance(a, types.FunctionType): |
| 152 after.append(a.__name__) | 151 after.append(a.__name__) |
| 153 elif a is not None: | 152 elif a is not None: |
| 154 # Deferring | 153 # Deferring |
| 155 # TODO(rspangler): should throw an exception | 154 raise ValueError('Defer after=%r is not a function or name' % a) |
| 156 print ('Warning: Defer can only wait for functions or names; ignoring' | |
| 157 'after = ', a) | |
| 158 | 155 |
| 159 # Find the deferred function | 156 # Find the deferred function |
| 160 if name not in __defer_groups: | 157 if name not in __defer_groups: |
| 161 __defer_groups[name] = DeferFunc() | 158 __defer_groups[name] = DeferFunc() |
| 162 group = __defer_groups[name] | 159 group = __defer_groups[name] |
| 163 | 160 |
| 164 # If we were given a function, also save environment and current directory | 161 # If we were given a function, also save environment and current directory |
| 165 if func: | 162 if func: |
| 166 group.func_env_cwd.append((func, self, os.getcwd())) | 163 group.func_env_cwd.append((func, self, os.getcwd())) |
| 167 | 164 |
| 168 # Add dependencies for the function | 165 # Add dependencies for the function |
| 169 group.after.update(after) | 166 group.after.update(after) |
| 170 | 167 |
| 171 | 168 |
| 172 def generate(env): | 169 def generate(env): |
| 173 # NOTE: SCons requires the use of this name, which fails gpylint. | 170 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 174 """SCons entry point for this tool.""" | 171 """SCons entry point for this tool.""" |
| 175 | 172 |
| 176 env.AddMethod(_InitializeDefer) | 173 env.AddMethod(_InitializeDefer) |
| 177 env.AddMethod(_ExecuteDefer) | 174 env.AddMethod(_ExecuteDefer) |
| 178 env.AddMethod(Defer) | 175 env.AddMethod(Defer) |
| OLD | NEW |