From 098e63b027e2cefe01b2a90a1ab7d4684f8ffe0f Mon Sep 17 00:00:00 2001 From: Ansgar Burchardt Date: Sun, 27 Oct 2013 20:20:06 +0100 Subject: [PATCH] daklib/filewriter.py: no longer invoke a shell when compressing files --- daklib/filewriter.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/daklib/filewriter.py b/daklib/filewriter.py index 2015f14f..5e11de53 100644 --- a/daklib/filewriter.py +++ b/daklib/filewriter.py @@ -68,21 +68,25 @@ class BaseFileWriter(object): os.chmod(tempfilename, 0o664) os.rename(tempfilename, filename) + # internal helper function to compress output + def compress(self, cmd, suffix, path): + in_filename = "{0}.new".format(path) + out_filename = "{0}.{1}.new".format(path, suffix) + with open(in_filename, 'r') as in_fh, open(out_filename, 'w') as out_fh: + check_call(cmd, stdin=in_fh, stdout=out_fh) + self.rename("{0}.{1}".format(path, suffix)) + def close(self): ''' Closes the file object and does the compression and rename work. ''' self.file.close() if self.gzip: - check_call('gzip -9cn --rsyncable <%s.new >%s.gz.new' % (self.path, self.path), - shell = True) - self.rename('%s.gz' % self.path) + self.compress(['gzip', '-9cn', '--rsyncable'], 'gz', self.path) if self.bzip2: - check_call('bzip2 -9 <%s.new >%s.bz2.new' % (self.path, self.path), shell = True) - self.rename('%s.bz2' % self.path) + self.compress(['bzip2', '-9'], 'bz2', self.path) if self.xz: - check_call('xz -c <{0}.new >{0}.xz.new'.format(self.path), shell=True) - self.rename('{0}.xz'.format(self.path)) + self.compress(['xz', '-c'], 'xz', self.path) if self.uncompressed: self.rename(self.path) else: -- 2.39.5