OLD | NEW |
(Empty) | |
| 1 from slave import recipe_api |
| 2 from slave import recipe_util |
| 3 |
| 4 import os |
| 5 import tempfile |
| 6 |
| 7 class InputDataPlaceholder(recipe_util.Placeholder): |
| 8 def __init__(self, data, suffix): |
| 9 assert isinstance(data, basestring) |
| 10 self.data = data |
| 11 self.suffix = suffix |
| 12 self.input_file = None |
| 13 super(InputDataPlaceholder, self).__init__() |
| 14 |
| 15 def render(self, test): |
| 16 if test.enabled: |
| 17 # cheat and pretend like we're going to pass the data on the |
| 18 # cmdline for test expectation purposes. |
| 19 return [self.data] |
| 20 else: # pragma: no cover |
| 21 input_fd, self.input_file = tempfile.mkstemp(suffix=self.suffix) |
| 22 os.write(input_fd, self.data) |
| 23 os.close(input_fd) |
| 24 return [self.input_file] |
| 25 |
| 26 def result(self, presentation, test): |
| 27 if not test.enabled: # pragma: no cover |
| 28 os.unlink(self.input_file) |
| 29 |
| 30 |
| 31 class OutputDataPlaceholder(recipe_util.Placeholder): |
| 32 def __init__(self, suffix): |
| 33 self.suffix = suffix |
| 34 self.output_file = None |
| 35 super(OutputDataPlaceholder, self).__init__() |
| 36 |
| 37 def render(self, test): |
| 38 if test.enabled: |
| 39 return ['/path/to/tmp/' + self.suffix.lstrip('.')] |
| 40 else: # pragma: no cover |
| 41 output_fd, self.output_file = tempfile.mkstemp(self.suffix) |
| 42 os.close(output_fd) |
| 43 return [self.output_file] |
| 44 |
| 45 def result(self, presentation, test): |
| 46 if test.enabled: |
| 47 return test.data |
| 48 else: # pragma: no cover |
| 49 assert self.output_file is not None |
| 50 try: |
| 51 with open(self.output_file.read(), 'rb') as f: |
| 52 return f.read() |
| 53 finally: |
| 54 os.unlink(self.output_file) |
| 55 |
| 56 |
| 57 class RawIOApi(recipe_api.RecipeApi): |
| 58 @recipe_util.returns_placeholder |
| 59 @staticmethod |
| 60 def input(data, suffix): |
| 61 return InputDataPlaceholder(data, suffix) |
| 62 |
| 63 @recipe_util.returns_placeholder |
| 64 @staticmethod |
| 65 def output(suffix): |
| 66 return OutputDataPlaceholder(suffix) |
OLD | NEW |