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

Side by Side Diff: recipe_modules/raw_io/api.py

Issue 1773273003: Make output placeholders like json.output index-able by name. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/recipes-py@master
Patch Set: Address comments. Created 4 years, 9 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
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from recipe_engine import recipe_api 5 from recipe_engine import recipe_api
6 from recipe_engine import util as recipe_util 6 from recipe_engine import util as recipe_util
7 7
8 import os 8 import os
9 import shutil 9 import shutil
10 import tempfile 10 import tempfile
(...skipping 25 matching lines...) Expand all
36 36
37 def result(self, presentation, test): 37 def result(self, presentation, test):
38 assert self._backing_file 38 assert self._backing_file
39 exists = os.path.exists(self._backing_file) 39 exists = os.path.exists(self._backing_file)
40 if not test.enabled and exists: # pragma: no cover 40 if not test.enabled and exists: # pragma: no cover
41 os.unlink(self._backing_file) 41 os.unlink(self._backing_file)
42 self._backing_file = None 42 self._backing_file = None
43 43
44 44
45 class OutputDataPlaceholder(recipe_util.OutputPlaceholder): 45 class OutputDataPlaceholder(recipe_util.OutputPlaceholder):
46 def __init__(self, suffix, leak_to): 46 def __init__(self, suffix, leak_to, name=None):
47 self.suffix = suffix 47 self.suffix = suffix
48 self.leak_to = leak_to 48 self.leak_to = leak_to
49 self._backing_file = None 49 self._backing_file = None
50 super(OutputDataPlaceholder, self).__init__() 50 super(OutputDataPlaceholder, self).__init__(name=name)
51 51
52 @property 52 @property
53 def backing_file(self): 53 def backing_file(self):
54 return self._backing_file 54 return self._backing_file
55 55
56 def render(self, test): 56 def render(self, test):
57 assert not self._backing_file, 'Placeholder can be used only once' 57 assert not self._backing_file, 'Placeholder can be used only once'
58 if self.leak_to: 58 if self.leak_to:
59 self._backing_file = str(self.leak_to) 59 self._backing_file = str(self.leak_to)
60 return [self._backing_file] 60 return [self._backing_file]
(...skipping 13 matching lines...) Expand all
74 try: 74 try:
75 with open(self._backing_file, 'rb') as f: 75 with open(self._backing_file, 'rb') as f:
76 return f.read() 76 return f.read()
77 finally: 77 finally:
78 if not self.leak_to: 78 if not self.leak_to:
79 os.unlink(self._backing_file) 79 os.unlink(self._backing_file)
80 self._backing_file = None 80 self._backing_file = None
81 81
82 82
83 class OutputDataDirPlaceholder(recipe_util.OutputPlaceholder): 83 class OutputDataDirPlaceholder(recipe_util.OutputPlaceholder):
84 def __init__(self, suffix, leak_to): 84 def __init__(self, suffix, leak_to, name=None):
85 self.suffix = suffix 85 self.suffix = suffix
86 self.leak_to = leak_to 86 self.leak_to = leak_to
87 self._backing_dir = None 87 self._backing_dir = None
88 super(OutputDataDirPlaceholder, self).__init__() 88 super(OutputDataDirPlaceholder, self).__init__(name=name)
89 89
90 @property 90 @property
91 def backing_file(self): # pragma: no cover 91 def backing_file(self): # pragma: no cover
92 raise ValueError('Output dir placeholders can not be used for stdin, ' 92 raise ValueError('Output dir placeholders can not be used for stdin, '
93 'stdout or stderr') 93 'stdout or stderr')
94 94
95 def render(self, test): 95 def render(self, test):
96 assert not self._backing_dir, 'Placeholder can be used only once' 96 assert not self._backing_dir, 'Placeholder can be used only once'
97 if self.leak_to: 97 if self.leak_to:
98 self._backing_dir = str(self.leak_to) 98 self._backing_dir = str(self.leak_to)
(...skipping 30 matching lines...) Expand all
129 129
130 130
131 class RawIOApi(recipe_api.RecipeApi): 131 class RawIOApi(recipe_api.RecipeApi):
132 @recipe_util.returns_placeholder 132 @recipe_util.returns_placeholder
133 @staticmethod 133 @staticmethod
134 def input(data, suffix=''): 134 def input(data, suffix=''):
135 return InputDataPlaceholder(data, suffix) 135 return InputDataPlaceholder(data, suffix)
136 136
137 @recipe_util.returns_placeholder 137 @recipe_util.returns_placeholder
138 @staticmethod 138 @staticmethod
139 def output(suffix='', leak_to=None): 139 def output(suffix='', leak_to=None, name=None):
140 """Returns a Placeholder for use as a step argument, or for std{out,err}. 140 """Returns a Placeholder for use as a step argument, or for std{out,err}.
141 141
142 If 'leak_to' is None, the placeholder is backed by a temporary file with 142 If 'leak_to' is None, the placeholder is backed by a temporary file with
143 a suffix 'suffix'. The file is deleted when the step finishes. 143 a suffix 'suffix'. The file is deleted when the step finishes.
144 144
145 If 'leak_to' is not None, then it should be a Path and placeholder 145 If 'leak_to' is not None, then it should be a Path and placeholder
146 redirects IO to a file at that path. Once step finishes, the file is 146 redirects IO to a file at that path. Once step finishes, the file is
147 NOT deleted (i.e. it's 'leaking'). 'suffix' is ignored in that case. 147 NOT deleted (i.e. it's 'leaking'). 'suffix' is ignored in that case.
148 """ 148 """
149 return OutputDataPlaceholder(suffix, leak_to) 149 return OutputDataPlaceholder(suffix, leak_to, name=name)
150 150
151 @recipe_util.returns_placeholder 151 @recipe_util.returns_placeholder
152 @staticmethod 152 @staticmethod
153 def output_dir(suffix='', leak_to=None): 153 def output_dir(suffix='', leak_to=None, name=None):
154 """Returns a directory Placeholder for use as a step argument. 154 """Returns a directory Placeholder for use as a step argument.
155 155
156 If 'leak_to' is None, the placeholder is backed by a temporary dir with 156 If 'leak_to' is None, the placeholder is backed by a temporary dir with
157 a suffix 'suffix'. The dir is deleted when the step finishes. 157 a suffix 'suffix'. The dir is deleted when the step finishes.
158 158
159 If 'leak_to' is not None, then it should be a Path and placeholder 159 If 'leak_to' is not None, then it should be a Path and placeholder
160 redirects IO to a dir at that path. Once step finishes, the dir is 160 redirects IO to a dir at that path. Once step finishes, the dir is
161 NOT deleted (i.e. it's 'leaking'). 'suffix' is ignored in that case. 161 NOT deleted (i.e. it's 'leaking'). 'suffix' is ignored in that case.
162 """ 162 """
163 return OutputDataDirPlaceholder(suffix, leak_to) 163 return OutputDataDirPlaceholder(suffix, leak_to, name=name)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698