summaryrefslogtreecommitdiff
path: root/src/buildstream/utils.py
diff options
context:
space:
mode:
authorRebecca Grayson <becky.grayson1@hotmail.co.uk>2019-06-27 11:55:18 +0100
committerRebecca Grayson <becky.grayson1@hotmail.co.uk>2019-07-16 15:35:42 +0000
commitd14ce3cacdb5c1dfce396b64061bb4b8b554a1dc (patch)
tree3a622c45e16bbe077d31ae9a133655a3ec1bd9a1 /src/buildstream/utils.py
parentcf3a1f6248fa6c2784020ab8d91b42fe5ed29779 (diff)
downloadbuildstream-d14ce3cacdb5c1dfce396b64061bb4b8b554a1dc.tar.gz
Allowing tar files to be compressedbecky/tar_compression
Changes made to cli.py and _stream.py in order to support tar compression. Compression flag has been added, which overrides any file extensions given. Where no compression or file extension provided, default to uncompressed .tar.
Diffstat (limited to 'src/buildstream/utils.py')
-rw-r--r--src/buildstream/utils.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index 03ff67bf2..82cd4134b 100644
--- a/src/buildstream/utils.py
+++ b/src/buildstream/utils.py
@@ -1351,3 +1351,38 @@ def _deterministic_umask():
yield
finally:
os.umask(old_umask)
+
+
+# _get_compression:
+#
+# Given a file name infer the compression
+#
+# Args:
+# tar (str): The file name from which to determine compression
+#
+# Returns:
+# (str): One from '', 'gz', 'xz', 'bz2'
+#
+# Raises:
+# UtilError: In the case where an unsupported file extension has been provided,
+# expecting compression.
+#
+#
+def _get_compression(tar):
+ mapped_extensions = {'.tar': '', '.gz': 'gz', '.xz': 'xz', '.bz2': 'bz2'}
+
+ name, ext = os.path.splitext(tar)
+
+ try:
+ return mapped_extensions[ext]
+ except KeyError:
+ # If ext not in mapped_extensions, find out if inner ext is .tar
+ # If so, we assume we have been given an unsupported extension,
+ # which expects compression. Raise an error
+ _, suffix = os.path.splitext(name)
+ if suffix == '.tar':
+ raise UtilError("Expected compression with unknown file extension ('{}'), "
+ "supported extensions are ('.tar'), ('.gz'), ('.xz'), ('.bz2')".format(ext))
+ else:
+ # Assume just an unconventional name was provided, default to uncompressed
+ return ''