Chromium Code Reviews| Index: tools/testing/frogpad/command.py |
| diff --git a/tools/testing/frogpad/command.py b/tools/testing/frogpad/command.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e73fc954ca67ee049449a35ac34a26df98f89d3 |
| --- /dev/null |
| +++ b/tools/testing/frogpad/command.py |
| @@ -0,0 +1,80 @@ |
| +# Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +# for details. All rights reserved. Use of this source code is governed by a |
| +# BSD-style license that can be found in the LICENSE file. |
| +# |
| + |
| +# Some utilities to invoke commands from python. |
| + |
| +import logging |
| +import os |
| + |
| +class Arg(object): |
| + """Holds one flag to be passed on the command line to an executable""" |
| + |
| + def __init__(self, name, value, bare = False): |
| + self.name = name |
| + self.value = value |
| + |
| + # if True, then do not include a leading '--' for this argument |
| + self.bare = bare |
| + |
| + def format(self): |
| + if not self.value is None: |
| + arg ='%s=%s' % (self.name, self.value) |
| + else: |
| + arg = self.name |
| + if not self.bare: |
| + arg = '--' + arg |
| + return arg |
| + |
| +def _FormatCommand(command, args): |
| + """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.
|
| + return command + ' ' + ' '.join([arg.format() for arg in args ]) |
| + |
| +class CommandFailedException(Exception): |
| + def __init__(self, message): |
| + self._message = message |
| + |
| + def GetMessage(self): |
| + return self._message |
| + |
| + |
| +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.
|
| + """launch the command with args passed as flags |
| + |
| + Args: |
| + command: the executable (or shell command) to launch |
| + args: A sequence of Arg object (each Arg is one flag to be |
| + passed on the command line) |
| + output_file_path: if supplied, then stdout is written to this |
| + file, rather than our log file |
| + """ |
| + if args is None: |
| + cmd = command |
| + else: |
| + cmd = _FormatCommand(command, args) |
| + |
| + output_desc = "" |
| + if output_file_path != None: |
| + output_desc = " > " + output_file_path |
| + output_file = None; |
| + 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.
|
| + output_file = open(output_file_path, 'wb') |
| + jobDesc = "%s%s" % (cmd, output_desc) |
| + logging.info("RUNNING " + jobDesc) |
| + output_pipe = os.popen(cmd, 'r', 0) |
| + output = output_pipe.read() |
| + if (output_file != None): |
| + output_file.write(output) |
| + output_file.close() |
| + else: |
| + logging.info(output) |
| + packed_status = output_pipe.close() |
| + if packed_status is None: |
| + logging.info('SUCCEEDED ' + jobDesc) |
| + return |
| + exit_code = packed_status >> 8 |
| + msg = "FAILURE (exit_code=%d): '%s'" % (exit_code, jobDesc) |
| + logging.error(msg) |
| + raise CommandFailedException(msg) |
| + |