OLD | NEW |
1 # Copyright 2013 The LUCI Authors. All rights reserved. | 1 # Copyright 2013 The LUCI Authors. All rights reserved. |
2 # Use of this source code is governed under the Apache License, Version 2.0 | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
3 # that can be found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
4 | 4 |
| 5 import textwrap |
| 6 |
| 7 from recipe_engine import config_types |
5 from recipe_engine import recipe_api | 8 from recipe_engine import recipe_api |
6 | 9 |
7 import textwrap | 10 class PythonApi(recipe_api.RecipeApi): |
| 11 def __call__(self, name, script, args=None, unbuffered=True, venv=None, |
| 12 **kwargs): |
| 13 """Return a step to run a python script with arguments. |
8 | 14 |
9 class PythonApi(recipe_api.RecipeApi): | 15 TODO: We should just use a single "args" list. Having "script" |
10 def __call__(self, name, script, args=None, unbuffered=True, **kwargs): | 16 separate but required/first leads to weird things like: |
11 """Return a step to run a python script with arguments.""" | 17 (... script='-m', args=['module']) |
| 18 |
| 19 Args: |
| 20 name (str): The name of the step. |
| 21 script (str or Path): The Path of the script to run, or the first |
| 22 command-line argument to pass to Python. |
| 23 args (list or None): If not None, additional arguments to pass to the |
| 24 Python command. |
| 25 unbuffered (bool): If True, run Python in unbuffered mode. |
| 26 venv (None or False or True or Path): If True, run the script through |
| 27 "vpython". This will, by default, probe the target script for a |
| 28 configured VirtualEnv and, failing that, use an empty VirtualEnv. If a |
| 29 Path, this is a path to an explicit "vpython" VirtualEnv spec file to |
| 30 use. If False or None (default), the script will be run through the |
| 31 standard Python interpreter. |
| 32 kwargs: Additional keyword arguments to forward to "step". |
| 33 """ |
12 env = self.m.context.env | 34 env = self.m.context.env |
13 cmd = ['python'] | 35 |
| 36 if venv: |
| 37 cmd = ['vpython'] |
| 38 if isinstance(venv, config_types.Path): |
| 39 cmd += ['-spec', venv] |
| 40 else: |
| 41 cmd = ['python'] |
| 42 |
14 if unbuffered: | 43 if unbuffered: |
15 cmd.append('-u') | 44 cmd.append('-u') |
16 else: | 45 else: |
17 env['PYTHONUNBUFFERED'] = None | 46 env['PYTHONUNBUFFERED'] = None |
18 | 47 |
19 context = {} | 48 context = {} |
20 if env: | 49 if env: |
21 context['env'] = env | 50 context['env'] = env |
22 | 51 |
23 cmd.append(script) | 52 cmd.append(script) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 else: | 85 else: |
57 self.m.step.active_result.presentation.step_text = text | 86 self.m.step.active_result.presentation.step_text = text |
58 | 87 |
59 def succeeding_step(self, name, text, as_log=None): | 88 def succeeding_step(self, name, text, as_log=None): |
60 """Return a succeeding step (correctly recognized in expectations).""" | 89 """Return a succeeding step (correctly recognized in expectations).""" |
61 return self.result_step(name, text, 0, as_log=as_log) | 90 return self.result_step(name, text, 0, as_log=as_log) |
62 | 91 |
63 def failing_step(self, name, text, as_log=None): | 92 def failing_step(self, name, text, as_log=None): |
64 """Return a failing step (correctly recognized in expectations).""" | 93 """Return a failing step (correctly recognized in expectations).""" |
65 return self.result_step(name, text, 1, as_log=as_log) | 94 return self.result_step(name, text, 1, as_log=as_log) |
OLD | NEW |