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

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

Issue 1785543004: Split Placeholder into InputPlaceholder and OutputPlaceholder. (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
« recipe_engine/util.py ('K') | « recipe_modules/json/api.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
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
11 11
12 12
13 class InputDataPlaceholder(recipe_util.Placeholder): 13 class InputDataPlaceholder(recipe_util.InputPlaceholder):
14 def __init__(self, data, suffix): 14 def __init__(self, data, suffix):
15 assert isinstance(data, basestring) 15 assert isinstance(data, basestring)
16 self.data = data 16 self.data = data
17 self.suffix = suffix 17 self.suffix = suffix
18 self._backing_file = None 18 self._backing_file = None
19 super(InputDataPlaceholder, self).__init__() 19 super(InputDataPlaceholder, self).__init__()
20 20
21 @property 21 @property
22 def backing_file(self): 22 def backing_file(self):
23 return self._backing_file 23 return self._backing_file
(...skipping 11 matching lines...) Expand all
35 return [self._backing_file] 35 return [self._backing_file]
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.Placeholder): 45 class OutputDataPlaceholder(recipe_util.OutputPlaceholder):
46 def __init__(self, suffix, leak_to): 46 def __init__(self, suffix, leak_to):
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__()
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
(...skipping 17 matching lines...) Expand all
73 else: # pragma: no cover 73 else: # pragma: no cover
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.Placeholder): 83 class OutputDataDirPlaceholder(recipe_util.OutputPlaceholder):
84 def __init__(self, suffix, leak_to): 84 def __init__(self, suffix, leak_to):
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__()
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')
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
OLDNEW
« recipe_engine/util.py ('K') | « recipe_modules/json/api.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698