def test_timeout(self): script_file = self._create_file('test.py', content=""" import time time.sleep(2.0) print 'Done' """) cmdline = CommandLine('python', [script_file]) iterable = iter(cmdline.execute(timeout=.5)) self.assertRaises(TimeoutError, iterable.next)
def execute(executable=None, file_=None, input_=None, output=None, args=None): """Generic external program execution. This function is not itself bound to a recipe command, but rather used from other commands. :param executable: name of the executable to run :param file\_: name of the script file, relative to the project directory, that should be run :param input\_: name of the file containing the data that should be passed to the shell script on its standard input stream :param output: name of the file to which the output of the script should be written :param args: command-line arguments to pass to the script """ if args: if isinstance(args, basestring): args = shlex.split(args) else: args = [] if executable is None: executable = file_ elif file_: args[:0] = [file_] if input_: input_file = file(input_, 'r') else: input_file = None output_file = None if output: output_file = file(output, 'w') try: log.debug('%s excuting args is %s', executable, args) cmdline = CommandLine(executable, args, input=input_file) for out, err in cmdline.execute(): if out is not None: log.info(out) if output: output_file.write(out + os.linesep) if err is not None: log.error(err) if output: output_file.write(err + os.linesep) finally: if input_: input_file.close() if output: output_file.close() return cmdline.returncode
def test_single_argument(self): cmdline = CommandLine('python', ['-V']) stdout = [] stderr = [] for out, err in cmdline.execute(timeout=5.0): if out is not None: stdout.append(out) if err is not None: stderr.append(err) py_version = '.'.join([str(v) for (v) in sys.version_info[:3]]) self.assertEqual(['Python %s' % py_version], stderr) self.assertEqual([], stdout) self.assertEqual(0, cmdline.returncode)
def test_multiple_arguments(self): script_file = self._create_file('test.py', content=""" import sys for arg in sys.argv[1:]: print arg """) cmdline = CommandLine('python', [script_file, 'foo', 'bar', 'baz']) stdout = [] stderr = [] for out, err in cmdline.execute(timeout=5.0): stdout.append(out) stderr.append(err) py_version = '.'.join([str(v) for (v) in sys.version_info[:3]]) self.assertEqual(['foo', 'bar', 'baz'], stdout) self.assertEqual([None, None, None], stderr) self.assertEqual(0, cmdline.returncode)
def test_input_stream_as_string(self): script_file = self._create_file('test.py', content=""" import sys data = sys.stdin.read() if data == 'abcd': print>>sys.stdout, 'Thanks' """) cmdline = CommandLine('python', [script_file], input='abcd') stdout = [] stderr = [] for out, err in cmdline.execute(timeout=5.0): stdout.append(out) stderr.append(err) py_version = '.'.join([str(v) for (v) in sys.version_info[:3]]) self.assertEqual(['Thanks'], stdout) self.assertEqual([None], stderr) self.assertEqual(0, cmdline.returncode)
def test_output_error_streams(self): script_file = self._create_file('test.py', content=""" import sys, time print>>sys.stdout, 'Hello' print>>sys.stdout, 'world!' sys.stdout.flush() time.sleep(.1) print>>sys.stderr, 'Oops' sys.stderr.flush() """) cmdline = CommandLine('python', [script_file]) stdout = [] stderr = [] for out, err in cmdline.execute(timeout=5.0): stdout.append(out) stderr.append(err) py_version = '.'.join([str(v) for (v) in sys.version_info[:3]]) self.assertEqual(['Hello', 'world!', None], stdout) self.assertEqual([None, None, 'Oops'], stderr) self.assertEqual(0, cmdline.returncode)