OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from recipe_engine import recipe_api |
| 6 |
| 7 import textwrap |
| 8 |
| 9 class PythonApi(recipe_api.RecipeApi): |
| 10 def __call__(self, name, script, args=None, unbuffered=True, **kwargs): |
| 11 """Return a step to run a python script with arguments.""" |
| 12 cmd = ['python'] |
| 13 if unbuffered: |
| 14 cmd.append('-u') |
| 15 cmd.append(script) |
| 16 return self.m.step(name, cmd + list(args or []), **kwargs) |
| 17 |
| 18 def inline(self, name, program, add_python_log=True, **kwargs): |
| 19 """Run an inline python program as a step. |
| 20 |
| 21 Program is output to a temp file and run when this step executes. |
| 22 """ |
| 23 program = textwrap.dedent(program) |
| 24 compile(program, '<string>', 'exec', dont_inherit=1) |
| 25 |
| 26 try: |
| 27 self(name, self.m.raw_io.input(program, '.py'), **kwargs) |
| 28 finally: |
| 29 result = self.m.step.active_result |
| 30 if result and add_python_log: |
| 31 result.presentation.logs['python.inline'] = program.splitlines() |
| 32 |
| 33 return result |
| 34 |
| 35 def result_step(self, name, text, retcode, as_log=None): |
| 36 """Return a no-op step that exits with a specified return code.""" |
| 37 try: |
| 38 self.inline(name, |
| 39 'import sys; sys.exit(%d)' % (retcode,), |
| 40 add_python_log=False, |
| 41 step_test_data=lambda: self.m.raw_io.test_api.output( |
| 42 text, retcode=retcode)) |
| 43 finally: |
| 44 if as_log: |
| 45 self.m.step.active_result.presentation.logs[as_log] = text |
| 46 else: |
| 47 self.m.step.active_result.presentation.step_text = text |
| 48 |
| 49 def succeeding_step(self, name, text, as_log=None): |
| 50 """Return a succeeding step (correctly recognized in expectations).""" |
| 51 return self.result_step(name, text, 0, as_log=as_log) |
| 52 |
| 53 def failing_step(self, name, text, as_log=None): |
| 54 """Return a failing step (correctly recognized in expectations).""" |
| 55 return self.result_step(name, text, 1, as_log=as_log) |
OLD | NEW |