Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 # for details. All rights reserved. Use of this source code is governed by a | |
| 3 # BSD-style license that can be found in the LICENSE file. | |
| 4 # | |
| 5 | |
| 6 # Some utilities to invoke commands from python. | |
| 7 | |
| 8 import logging | |
| 9 import os | |
| 10 | |
| 11 class Arg(object): | |
| 12 """Holds one flag to be passed on the command line to an executable""" | |
| 13 | |
| 14 def __init__(self, name, value, bare = False): | |
| 15 self.name = name | |
| 16 self.value = value | |
| 17 | |
| 18 # if True, then do not include a leading '--' for this argument | |
| 19 self.bare = bare | |
| 20 | |
| 21 def format(self): | |
| 22 if not self.value is None: | |
| 23 arg ='%s=%s' % (self.name, self.value) | |
| 24 else: | |
| 25 arg = self.name | |
| 26 if not self.bare: | |
| 27 arg = '--' + arg | |
| 28 return arg | |
| 29 | |
| 30 def _FormatCommand(command, args): | |
| 31 """build up a command line string that can be used to start a process""" | |
|
Emily Fortuna
2012/02/21 19:05:57
Capital letter, period.
mattsh
2012/02/21 20:13:28
Done.
| |
| 32 return command + ' ' + ' '.join([arg.format() for arg in args ]) | |
| 33 | |
| 34 class CommandFailedException(Exception): | |
| 35 def __init__(self, message): | |
| 36 self._message = message | |
| 37 | |
| 38 def GetMessage(self): | |
| 39 return self._message | |
| 40 | |
| 41 | |
| 42 def RunCommand(command, args, output_file_path=None): | |
|
Emily Fortuna
2012/02/21 19:05:57
RunCommand ==> run_command ? (Python style -- here
mattsh
2012/02/21 20:13:28
Done.
| |
| 43 """launch the command with args passed as flags | |
| 44 | |
| 45 Args: | |
| 46 command: the executable (or shell command) to launch | |
| 47 args: A sequence of Arg object (each Arg is one flag to be | |
| 48 passed on the command line) | |
| 49 output_file_path: if supplied, then stdout is written to this | |
| 50 file, rather than our log file | |
| 51 """ | |
| 52 if args is None: | |
| 53 cmd = command | |
| 54 else: | |
| 55 cmd = _FormatCommand(command, args) | |
| 56 | |
| 57 output_desc = "" | |
| 58 if output_file_path != None: | |
| 59 output_desc = " > " + output_file_path | |
| 60 output_file = None; | |
| 61 if (output_file_path != None): | |
|
Emily Fortuna
2012/02/21 19:05:57
parens not necessary here and below
mattsh
2012/02/21 20:13:28
Done.
| |
| 62 output_file = open(output_file_path, 'wb') | |
| 63 jobDesc = "%s%s" % (cmd, output_desc) | |
| 64 logging.info("RUNNING " + jobDesc) | |
| 65 output_pipe = os.popen(cmd, 'r', 0) | |
| 66 output = output_pipe.read() | |
| 67 if (output_file != None): | |
| 68 output_file.write(output) | |
| 69 output_file.close() | |
| 70 else: | |
| 71 logging.info(output) | |
| 72 packed_status = output_pipe.close() | |
| 73 if packed_status is None: | |
| 74 logging.info('SUCCEEDED ' + jobDesc) | |
| 75 return | |
| 76 exit_code = packed_status >> 8 | |
| 77 msg = "FAILURE (exit_code=%d): '%s'" % (exit_code, jobDesc) | |
| 78 logging.error(msg) | |
| 79 raise CommandFailedException(msg) | |
| 80 | |
| OLD | NEW |