OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 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 import functools |
| 5 import os |
| 6 |
| 7 |
| 8 SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 9 BUILD_ROOT = os.path.dirname(os.path.dirname(SCRIPT_PATH)) |
| 10 ROOT_PATH = os.path.abspath(os.path.join( |
| 11 SCRIPT_PATH, os.pardir, os.pardir, os.pardir)) |
| 12 BASE_DIRS = [ |
| 13 SCRIPT_PATH, |
| 14 os.path.join(ROOT_PATH, 'build_internal', 'scripts', 'slave'), |
| 15 os.path.join(ROOT_PATH, 'build_internal', 'scripts', 'slave-internal') |
| 16 ] |
| 17 MODULE_DIRS = lambda: [os.path.join(x, 'recipe_modules') for x in BASE_DIRS] |
| 18 RECIPE_DIRS = lambda: [os.path.join(x, 'recipes') for x in BASE_DIRS] |
| 19 |
| 20 class RecipeAbort(Exception): |
| 21 pass |
| 22 |
| 23 |
| 24 class ModuleInjectionSite(object): |
| 25 pass |
| 26 |
| 27 |
| 28 class Placeholder(object): |
| 29 """Base class for json placeholders. Do not use directly.""" |
| 30 def __init__(self): |
| 31 self.name_pieces = None |
| 32 |
| 33 def render(self, test): # pragma: no cover |
| 34 """Return [cmd items]*""" |
| 35 raise NotImplementedError |
| 36 |
| 37 def result(self, presentation, test): |
| 38 """Called after step completion. |
| 39 |
| 40 Args: |
| 41 presentation (StepPresentation) - for the current step. |
| 42 test (PlaceholderTestData) - test data for this placeholder. |
| 43 |
| 44 Returns value to add to step result. |
| 45 |
| 46 May optionally modify presentation as a side-effect. |
| 47 """ |
| 48 pass |
| 49 |
| 50 @property |
| 51 def name(self): |
| 52 assert self.name_pieces |
| 53 return "%s.%s" % self.name_pieces |
| 54 |
| 55 |
| 56 def static_wraps(func): |
| 57 wrapped_fn = func |
| 58 if isinstance(func, staticmethod): |
| 59 wrapped_fn = func.__func__ |
| 60 return functools.wraps(wrapped_fn) |
| 61 |
| 62 |
| 63 def static_call(obj, func, *args, **kwargs): |
| 64 if isinstance(func, staticmethod): |
| 65 return func.__get__(obj)(*args, **kwargs) |
| 66 else: |
| 67 return func(obj, *args, **kwargs) |
| 68 |
| 69 |
| 70 def static_name(obj, func): |
| 71 if isinstance(func, staticmethod): |
| 72 return func.__get__(obj).__name__ |
| 73 else: |
| 74 return func.__name__ |
| 75 |
| 76 |
| 77 def returns_placeholder(func): |
| 78 @static_wraps(func) |
| 79 def inner(self, *args, **kwargs): |
| 80 ret = static_call(self, func, *args, **kwargs) |
| 81 assert isinstance(ret, Placeholder) |
| 82 ret.name_pieces = (self.name, static_name(self, func)) |
| 83 return ret |
| 84 return inner |
| 85 |
| 86 |
| 87 def wrap_followup(kwargs, pre=False): |
| 88 """ |
| 89 Decorator for a new followup_fn. |
| 90 |
| 91 Will pop the existing fn out of kwargs (if any), and return a decorator for |
| 92 the new folloup_fn. |
| 93 |
| 94 Args: |
| 95 kwargs - dictionary possibly containing folloup_fn |
| 96 pre - If true, the old folloup_fn is called before the wrapped function. |
| 97 Otherwise, the old followup_fn is called after the wrapped function. |
| 98 """ |
| 99 null_fn = lambda _: None |
| 100 old_followup = kwargs.pop('followup_fn', null_fn) |
| 101 def decorator(f): |
| 102 @functools.wraps(f) |
| 103 def _inner(step_result): |
| 104 if pre: |
| 105 old_followup(step_result) |
| 106 f(step_result) |
| 107 else: |
| 108 f(step_result) |
| 109 old_followup(step_result) |
| 110 if old_followup is not null_fn: |
| 111 _inner.__name__ += '[%s]' % old_followup.__name__ |
| 112 return _inner |
| 113 return decorator |
| 114 |
| 115 |
OLD | NEW |