| 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 """Command output builder for SCons.""" |
| 32 |
| 33 |
| 34 import os |
| 35 import SCons.Script |
| 36 import subprocess |
| 37 |
| 38 |
| 39 def RunCommand(cmdargs, cwdir=None, env=None, echo_output=True): |
| 40 """Runs an external command. |
| 41 |
| 42 Args: |
| 43 cmdargs: A command string, or a tuple containing the command and its |
| 44 arguments. |
| 45 cwdir: Working directory for the command, if not None. |
| 46 env: Environment variables dict, if not None. |
| 47 echo_output: If True, output will be echoed to stdout. |
| 48 |
| 49 Returns: |
| 50 The integer errorlevel from the command. |
| 51 The combined stdout and stderr as a string. |
| 52 """ |
| 53 |
| 54 # Force unicode string in the environment to strings. |
| 55 if env: |
| 56 env = dict([(k, str(v)) for k, v in env.items()]) |
| 57 child = subprocess.Popen(cmdargs, cwd=cwdir, env=env, shell=True, |
| 58 stdin=subprocess.PIPE, |
| 59 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 60 child_out = [] |
| 61 child_retcode = None |
| 62 |
| 63 # Need to poll the child process, since the stdout pipe can fill. |
| 64 while child_retcode is None: |
| 65 child_retcode = child.poll() |
| 66 new_out = child.stdout.read() |
| 67 if echo_output: |
| 68 print new_out, |
| 69 child_out.append(new_out) |
| 70 |
| 71 if echo_output: |
| 72 print # end last line of output |
| 73 return child_retcode, ''.join(child_out) |
| 74 |
| 75 |
| 76 def CommandOutputBuilder(target, source, env): |
| 77 """Command output builder. |
| 78 |
| 79 Args: |
| 80 self: Environment in which to build |
| 81 target: List of target nodes |
| 82 source: List of source nodes |
| 83 |
| 84 Returns: |
| 85 None or 0 if successful; nonzero to indicate failure. |
| 86 |
| 87 Runs the command specified in the COMMAND_OUTPUT_CMDLINE environment variable |
| 88 and stores its output in the first target file. Additional target files |
| 89 should be specified if the command creates additional output files. |
| 90 |
| 91 Runs the command in the COMMAND_OUTPUT_RUN_DIR subdirectory. |
| 92 """ |
| 93 env = env.Clone() |
| 94 |
| 95 cmdline = env.subst('$COMMAND_OUTPUT_CMDLINE', target=target, source=source) |
| 96 cwdir = env.subst('$COMMAND_OUTPUT_RUN_DIR', target=target, source=source) |
| 97 if cwdir: |
| 98 cwdir = os.path.normpath(cwdir) |
| 99 env.AppendENVPath('PATH', cwdir) |
| 100 env.AppendENVPath('LD_LIBRARY_PATH', cwdir) |
| 101 else: |
| 102 cwdir = None |
| 103 |
| 104 retcode, output = RunCommand(cmdline, cwdir=cwdir, env=env['ENV']) |
| 105 |
| 106 # Save command line output |
| 107 output_file = open(str(target[0]), 'w') |
| 108 output_file.write(output) |
| 109 output_file.close() |
| 110 |
| 111 return retcode |
| 112 |
| 113 |
| 114 def generate(env): |
| 115 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 116 """SCons entry point for this tool.""" |
| 117 |
| 118 # Add the builder and tell it which build environment variables we use. |
| 119 action = SCons.Script.Action(CommandOutputBuilder, varlist=[ |
| 120 'COMMAND_OUTPUT_CMDLINE', |
| 121 'COMMAND_OUTPUT_RUN_DIR', |
| 122 ]) |
| 123 builder = SCons.Script.Builder(action = action) |
| 124 env.Append(BUILDERS={'CommandOutput': builder}) |
| 125 |
| 126 # Default command line is to run the first input |
| 127 env['COMMAND_OUTPUT_CMDLINE'] = '$SOURCE' |
| 128 |
| 129 # TODO(rspangler): add a pseudo-builder which takes an additional command |
| 130 # line as an argument. |
| OLD | NEW |