From da0f1818c38e1f4a8f2c03d735e99249934d03b3 Mon Sep 17 00:00:00 2001 From: Yusuke Sasaki Date: Sat, 17 Oct 2015 17:31:42 +0900 Subject: [PATCH] Avoid creating a temporary file when archiving/extracting --- generate_maf.py | 14 ++++++-------- maf_template.py | 26 +++----------------------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/generate_maf.py b/generate_maf.py index 1e8ca59..c322ba8 100755 --- a/generate_maf.py +++ b/generate_maf.py @@ -26,12 +26,12 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +import io import os import tarfile TEMPLATE_FILE_NAME = 'maf_template.py' TARGET_FILE_NAME = 'maf.py' -ARCHIVE_FILE_NAME = 'maflib.tar.bz2' MAFLIB_PATH = 'maflib' NEW_LINE = '#XXX'.encode() @@ -42,24 +42,22 @@ if __name__ == '__main__': try: - archive = tarfile.open(ARCHIVE_FILE_NAME, 'w:bz2') + f = io.BytesIO() + archive = tarfile.open(fileobj=f, mode='w:bz2') archive.add(MAFLIB_PATH, exclude=lambda fn: fn.endswith('.pyc')) except tarfile.TarError: raise Exception('can not use tar.bz2 file') finally: archive.close() - + + archive = f.getvalue() + with open(TEMPLATE_FILE_NAME) as f: code = f.read() - with open(ARCHIVE_FILE_NAME, 'rb') as f: - archive = f.read() - code += '#==>\n#'.encode() code += archive.replace('\n'.encode(), NEW_LINE).replace('\r'.encode(), CARRIAGE_RETURN) code += '\n#<==\n'.encode() with open(TARGET_FILE_NAME, 'wb') as f: f.write(code) - - os.unlink(ARCHIVE_FILE_NAME) diff --git a/maf_template.py b/maf_template.py index e734c73..339620e 100644 --- a/maf_template.py +++ b/maf_template.py @@ -33,6 +33,7 @@ # NOTE: coding ISO8859-1 is necessary for attaching maflib at the end of this # file. +import io import os import os.path import shutil @@ -42,7 +43,6 @@ import waflib.Context import waflib.Logs -TAR_NAME = 'maflib.tar' NEW_LINE = '#XXX'.encode() CARRIAGE_RETURN = '#YYY'.encode() ARCHIVE_BEGIN = '#==>\n'.encode() @@ -94,29 +94,9 @@ def unpack_maflib(directory): os.makedirs(os.path.join(directory, 'maflib')) os.chdir(directory) - bz2_name = TAR_NAME + '.bz2' - with open(bz2_name, 'wb') as f: - f.write(content) - - try: - t = tarfile.open(bz2_name) - except: - try: - os.system('bunzip2 ' + bz2_name) - t = tarfile.open(TAR_NAME) - except: - raise Exception('Cannot extract maflib. Check that python bz2 module or bunzip2 command is available.') - - try: + f = io.BytesIO(content) + with tarfile.open(fileobj=f) as t: t.extractall() - finally: - t.close() - - try: - os.remove(bz2_name) - os.remove(TAR_NAME) - except: - pass maflib_path = os.path.abspath(os.getcwd()) return maflib_path