From bcf3826fa633c9a0864752582bce01b711ec772e Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 13 Feb 2016 13:54:24 +0100 Subject: Fix compression handling gzip/xz compressed the original file and removed the uncompressed version. This is really bad so compress to a temp file. Signed-off-by: Florian Pritz --- fb.py | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/fb.py b/fb.py index 6165365..3f83cfe 100755 --- a/fb.py +++ b/fb.py @@ -6,8 +6,10 @@ import contextlib import datetime import errno import getpass +import gzip import json import locale +import lzma import os import pycurl import re @@ -321,14 +323,18 @@ class ProgressBar: class Compressor: @staticmethod - def gzip(file): - subprocess.call(['gzip', '-n', file]) - return file + '.gz' + def gzip(src, dst): + dst += '.gz' + with open(src, 'rb') as f_in, gzip.open(dst, 'wb') as f_out: + f_out.writelines(f_in) + return dst @staticmethod - def xz(file): - subprocess.call(['xz', file]) - return file + '.xz' + def xz(src, dst): + dst += '.xz' + with open(src, 'rb') as f_in, lzma.open(dst, 'wb') as f_out: + f_out.writelines(f_in) + return dst class ConfigParser: def __init__(self, file, ignoreMissing=False): @@ -476,10 +482,31 @@ class FBClient: 1: Compressor.gzip, 2: Compressor.xz, } - return compressor[self.args.compress](file) + return compressor[self.args.compress](file, self.create_temp_copy_path(file)) else: return file + def handle_directory(self, path): + if os.path.isdir(path): + return self.create_tarball(path) + + return path + + def create_tarball(self, path): + compression = { + 0: "", + 1: "gz", + 2: "xz", + } + extension = "." + '.'.join(["tar", compression[self.args.compress]]) + tarball_path = os.path.normpath(self.tempdir + "/" + self.args.name + extension) + tar = tarfile.open(tarball_path, "w:" + compression[self.args.compress]) + tar.add(path) + tar.close() + + return tarball_path + + def create_temp_copy(self, file): dest = self.create_temp_copy_path(file) open(dest, "w").write(open(file).read()) @@ -497,7 +524,12 @@ class FBClient: if os.stat(file)[6] == 0: file = self.create_temp_copy(file) - upload_files.append(self.handle_compression(file)) + if os.path.isdir(file): + file = self.create_tarball(file) + else: + file = self.handle_compression(file) + + upload_files.append(file) resp = self.curlw.upload_files(upload_files) ids = resp["ids"] -- cgit v1.2.3-24-g4f1b