From 6d430524f1a0ee661dbd1c4af1aac6d721a0d064 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Mon, 19 Nov 2012 12:04:07 +0100 Subject: [PATCH] binary all the way, fixes issue #138 Signed-off-by: Steven Armstrong --- cdist/emulator.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cdist/emulator.py b/cdist/emulator.py index 42744fab..a9c760cb 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -29,7 +29,7 @@ import cdist from cdist import core class Emulator(object): - def __init__(self, argv, stdin=sys.stdin, env=os.environ): + def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ): self.argv = argv self.stdin = stdin self.env = env @@ -151,6 +151,10 @@ class Emulator(object): # Record / Append source self.cdist_object.source.append(self.object_source) + chunk_size = 65536 + def _read_stdin(self): + return self.stdin.read(self.chunk_size) + def save_stdin(self): """If something is written to stdin, save it in the object as $__object/stdin so it can be accessed in manifest and gencode-* @@ -160,10 +164,12 @@ class Emulator(object): try: # go directly to file instead of using CdistObject's api # as that does not support streaming - # FIXME: no streaming needed anymore - use a raw file (not yet there?) path = os.path.join(self.cdist_object.absolute_path, 'stdin') - with open(path, 'w') as fd: - fd.write(self.stdin.read()) + with open(path, 'wb') as fd: + chunk = self._read_stdin() + while chunk: + fd.write(chunk) + chunk = self._read_stdin() except EnvironmentError as e: raise cdist.Error('Failed to read from stdin: %s' % e)