Created
December 17, 2012 14:45
-
-
Save ThiefMaster/4318783 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CrazyBuffer(object): | |
def __init__(self): | |
self._buffer = StringIO() | |
def write(self, data): | |
self._buffer.write(data) | |
def close(self): | |
pass | |
def get_value(self): | |
return self._buffer.getvalue() | |
def reset(self): | |
self._buffer.close() | |
self._buffer = StringIO() | |
def streamed_tar_response(files): | |
crazy_buffer = CrazyBuffer() | |
tar = tarfile.open('/dev/null', 'w|', crazy_buffer) | |
for file, arcname in files: | |
arcname = arcname.decode('utf-8').encode('latin1', 'ignore') | |
if isinstance(file, basestring): | |
info = tar.gettarinfo(file, arcname) | |
info.mode = 0644 | |
with open(file, 'rb') as f: | |
tar.addfile(info, f) | |
else: # assume StringIO | |
info = tarfile.TarInfo(arcname) | |
file.seek(0, os.SEEK_END) | |
info.size = file.tell() | |
file.seek(0) | |
info.mode = 0444 | |
info.mtime = int(time.time()) | |
tar.addfile(info, file) | |
yield crazy_buffer.get_value() | |
crazy_buffer.reset() | |
# Making shit up at this point in case there | |
# is someting in the buffer after the loop. | |
# Not sure if actually needed! | |
tar.close() | |
yield crazy_buffer.get_value() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment