summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>2014-07-14 10:25:19 +0300
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>2014-11-14 14:22:01 +0200
commitd3a62f7a602f73a8f0884ad5edac80ed75ea9e4c (patch)
treea39226e13ea0931220bc2a5582356971f00a9946
parentc160594eb7423514dccbb2b3876dedfb1e80e58e (diff)
downloadgit-buildpackage-d3a62f7a602f73a8f0884ad5edac80ed75ea9e4c.tar.gz
git-buildpackage-d3a62f7a602f73a8f0884ad5edac80ed75ea9e4c.tar.bz2
git-buildpackage-d3a62f7a602f73a8f0884ad5edac80ed75ea9e4c.zip
buildpackage: refactor tarball compression
Introduce a new function for running the compressor commands. Unifies exception/error handling - GbpError will be returned if the compressor fails. Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rw-r--r--gbp/scripts/common/buildpackage.py49
1 files changed, 31 insertions, 18 deletions
diff --git a/gbp/scripts/common/buildpackage.py b/gbp/scripts/common/buildpackage.py
index 898f6752..74c33e39 100644
--- a/gbp/scripts/common/buildpackage.py
+++ b/gbp/scripts/common/buildpackage.py
@@ -52,6 +52,27 @@ def sanitize_prefix(prefix):
return '/'
+def compress(cmd, options, output, input_data=None):
+ """
+ Filter data through a compressor cmd.
+
+ For better performance input_data should feed data in bigger chunks.
+ """
+ stdin = subprocess.PIPE if input_data else None
+ try:
+ with open(output, 'w') as fobj:
+ popen = subprocess.Popen([cmd] + options, stdin=stdin, stdout=fobj)
+ if stdin:
+ for chunk in input_data:
+ popen.stdin.write(chunk)
+ popen.stdin.close()
+ if popen.wait():
+ raise GbpError("Error creating %s: running '%s' failed" %
+ (output, ' '.join([cmd] + options)))
+ except (OSError, IOError) as err:
+ raise GbpError("Error creating %s: %s" % (output, err))
+
+
def git_archive_submodules(repo, treeish, output, prefix, comp_type, comp_level,
comp_opts, format='tar'):
"""
@@ -86,11 +107,8 @@ def git_archive_submodules(repo, treeish, output, prefix, comp_type, comp_level,
# compress the output
if comp_type:
- ret = os.system("%s --stdout -%s %s %s > %s" % \
- (comp_type, comp_level, " ".join(comp_opts),
- main_archive, output))
- if ret:
- raise GbpError("Error creating %s: %d" % (output, ret))
+ compress(comp_type, ['--stdout', '-%s' % comp_level] + comp_opts +
+ [main_archive], output)
else:
shutil.move(main_archive, output)
finally:
@@ -105,19 +123,14 @@ def git_archive_single(repo, treeish, output, prefix, comp_type, comp_level,
Exception handling is left to the caller.
"""
prefix = sanitize_prefix(prefix)
- with open(output, 'w') as archive_fd:
- if comp_type:
- cmd = [comp_type, '--stdout', '-%s' % comp_level] + comp_opts
- else:
- cmd = ['cat']
-
- popen = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=archive_fd)
- for chunk in repo.archive(format, prefix, None, treeish):
- popen.stdin.write(chunk)
- popen.stdin.close()
- if popen.wait():
- raise GbpError("Error creating %s: compressor cmd failed" % output)
-
+ if comp_type:
+ cmd = comp_type
+ opts = ['--stdout', '-%s' % comp_level] + comp_opts
+ else:
+ cmd= 'cat'
+ opts = []
+ input_data = repo.archive(format, prefix, None, treeish)
+ compress(cmd, opts, output, input_data)
def untar_data(outdir, data):
"""Extract tar provided as an iterable"""