def Run(args, errexit=True, redirect_stdout=None, redirect_stderr=None): """ Run: Run a command. Returns: return_code, stdout, stderr Run() is used to invoke "other" tools, e.g. those NOT prefixed with "pnacl-" stdout and stderr only contain meaningful data if redirect_{stdout,stderr} == subprocess.PIPE Run will terminate the program upon failure unless errexit == False TODO(robertm): errexit == True has not been tested and needs more work redirect_stdout and redirect_stderr are passed straight to subprocess.Popen """ result_stdout = None result_stderr = None if isinstance(args, str): args = shell.split(env.eval(args)) args = [pathtools.tosys(args[0])] + args[1:] Log.Info('Running: ' + StringifyCommand(args)) if env.getbool('DRY_RUN'): if redirect_stderr or redirect_stdout: # TODO(pdox): Prevent this from happening, so that # dry-run is more useful. Log.Fatal("Unhandled dry-run case.") return 0, None, None try: # If we have too long of a cmdline on windows, running it would fail. # Attempt to use a file with the command line options instead in that case. if ArgsTooLongForWindows(args): actual_args = ConvertArgsToFile(args) Log.Info('Wrote long commandline to file for Windows: ' + StringifyCommand(actual_args)) else: actual_args = args p = subprocess.Popen(actual_args, stdout=redirect_stdout, stderr=redirect_stderr) result_stdout, result_stderr = p.communicate() except Exception, e: msg = '%s\nCommand was: %s' % (str(e), StringifyCommand(args)) print(msg) DriverExit(1)
def ParseError(s, leftpos, rightpos, msg): Log.Error("Parse Error: %s", msg) Log.Error(' ' + s) Log.Error(' ' + (' ' * leftpos) + ('^' * (rightpos - leftpos + 1))) DriverExit(1)
print msg DriverExit(1) Log.Info('Return Code: ' + str(p.returncode)) if errexit and p.returncode != 0: if redirect_stdout == subprocess.PIPE: Log.Error('--------------stdout: begin') Log.Error(result_stdout) Log.Error('--------------stdout: end') if redirect_stderr == subprocess.PIPE: Log.Error('--------------stderr: begin') Log.Error(result_stderr) Log.Error('--------------stderr: end') DriverExit(p.returncode) return p.returncode, result_stdout, result_stderr def IsWindowsPython(): return 'windows' in platform.system().lower() def SetupCygwinLibs(): bindir = env.getone('DRIVER_BIN') # Prepend the directory containing cygwin1.dll etc. to the PATH to ensure we # get the right one. os.environ['PATH'] = os.pathsep.join( [pathtools.tosys(bindir)] + os.environ['PATH'].split(os.pathsep))