Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/recipe_engine/util.py

Issue 1347263002: Revert of Cross-repo recipe package system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/recipe_engine/unittests/properties_test.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2013-2015 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 import functools
6 import os
7
8 from cStringIO import StringIO
9
10 class RecipeAbort(Exception):
11 pass
12
13
14 class ModuleInjectionError(AttributeError):
15 pass
16
17
18 class ModuleInjectionSite(object):
19 def __init__(self, owner_module=None):
20 self.owner_module = owner_module
21
22 def __getattr__(self, key):
23 if self.owner_module is None:
24 raise ModuleInjectionError(
25 "RecipeApi has no dependency %r. (Add it to DEPS?)" % (key,))
26 else:
27 raise ModuleInjectionError(
28 "Recipe Module %r has no dependency %r. (Add it to __init__.py:DEPS?)"
29 % (self.owner_module.name, key))
30
31
32 class Placeholder(object):
33 """Base class for json placeholders. Do not use directly."""
34 def __init__(self):
35 self.name_pieces = None
36
37 @property
38 def backing_file(self): # pragma: no cover
39 """Return path to a temp file that holds or receives the data.
40
41 Valid only after 'render' has been called.
42 """
43 raise NotImplementedError
44
45 def render(self, test): # pragma: no cover
46 """Return [cmd items]*"""
47 raise NotImplementedError
48
49 def result(self, presentation, test):
50 """Called after step completion.
51
52 Args:
53 presentation (StepPresentation) - for the current step.
54 test (PlaceholderTestData) - test data for this placeholder.
55
56 Returns value to add to step result.
57
58 May optionally modify presentation as a side-effect.
59 """
60 pass
61
62 @property
63 def name(self):
64 assert self.name_pieces
65 return "%s.%s" % self.name_pieces
66
67
68 def static_wraps(func):
69 wrapped_fn = func
70 if isinstance(func, staticmethod):
71 # __get__(obj) is the way to get the function contained in the staticmethod.
72 # python 2.7+ has a __func__ member, but previous to this the attribute
73 # doesn't exist. It doesn't matter what the obj is, as long as it's not
74 # None.
75 wrapped_fn = func.__get__(object)
76 return functools.wraps(wrapped_fn)
77
78
79 def static_call(obj, func, *args, **kwargs):
80 if isinstance(func, staticmethod):
81 return func.__get__(obj)(*args, **kwargs)
82 else:
83 return func(obj, *args, **kwargs)
84
85
86 def static_name(obj, func):
87 if isinstance(func, staticmethod):
88 return func.__get__(obj).__name__
89 else:
90 return func.__name__
91
92
93 def returns_placeholder(func):
94 @static_wraps(func)
95 def inner(self, *args, **kwargs):
96 ret = static_call(self, func, *args, **kwargs)
97 assert isinstance(ret, Placeholder)
98 ret.name_pieces = (self.name, static_name(self, func))
99 return ret
100 # prevent this placeholder-returning function from becoming a composite_step.
101 inner._non_step = True # pylint: disable=protected-access
102 return inner
103
104
105 def cached_unary(f):
106 """Decorator that caches/memoizes an unary function result.
107
108 If the function throws an exception, the cache table will not be updated.
109 """
110 cache = {}
111 empty = object()
112
113 @functools.wraps(f)
114 def cached_f(inp):
115 cache_entry = cache.get(inp, empty)
116 if cache_entry is empty:
117 cache_entry = f(inp)
118 cache[inp] = cache_entry
119 return cache_entry
120 return cached_f
121
122
123 def scan_directory(path, predicate):
124 """Recursively scans a directory and yields paths that match predicate."""
125 for root, _dirs, files in os.walk(path):
126 for file_name in (f for f in files if predicate(f)):
127 file_path = os.path.join(root, file_name)
128 yield file_path
129
130
131 class StringListIO(object):
132 def __init__(self):
133 self.lines = [StringIO()]
134
135 def write(self, s):
136 while s:
137 i = s.find('\n')
138 if i == -1:
139 self.lines[-1].write(s)
140 break
141 self.lines[-1].write(s[:i])
142 self.lines[-1] = self.lines[-1].getvalue()
143 self.lines.append(StringIO())
144 s = s[i+1:]
145
146 def close(self):
147 if not isinstance(self.lines[-1], basestring):
148 self.lines[-1] = self.lines[-1].getvalue()
OLDNEW
« no previous file with comments | « third_party/recipe_engine/unittests/properties_test.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698