Fixed issue with bz2 files

 Fixed issue with bz2 files

 Fixed issue with bz2 files
parent 230818ec
Pipeline #20899 passed with stage
in 9 minutes and 50 seconds
......@@ -14,18 +14,25 @@ def _unzip(zip_file, directory):
myzip.extractall(directory)
def _untar(tar_file, directory, mode):
if ".tar" in tar_file:
import tarfile
with tarfile.open(name=tar_file, mode='r:'+mode) as t:
t.extractall(directory)
def _untar(tar_file, directory, ext):
if ext in [".bz2" or ".tbz2"]:
mode = "r:bz2"
elif ext in [".gz" or ".tgz"]:
mode = "r:gz"
else:
if mode=="bz2":
import bz2
with bz2.BZ2File(tar_file) as t:
open(os.path.splitext(tar_file)[0:-1][0], 'wb').write(t.read())
else:
raise ValueError("It was not possible to extract {0}".format(tar_file))
mode = "r"
import tarfile
with tarfile.open(name=tar_file, mode=mode) as t:
t.extractall(directory)
def _unbz2(bz2_file):
import bz2
with bz2.BZ2File(bz2_file) as t:
open(os.path.splitext(bz2_file)[0], 'wb').write(t.read())
def download_file(url, out_file):
"""Downloads a file from a given url
......@@ -96,12 +103,16 @@ def download_and_unzip(urls, filename):
# Uncompressing if it is the case
ext = os.path.splitext(filename)[-1].lower()
header = os.path.splitext(filename)[0].lower()
if ext == ".zip":
logger.info("Unziping in {0}".format(filename))
_unzip(filename, os.path.dirname(filename))
elif ext in [".gz", ".bz2"]:
elif header[-4:] == ".tar" or ext in [".tgz", ".tbz2"]:
logger.info("Untar/gzip in {0}".format(filename))
_untar(filename, os.path.dirname(filename), mode=ext[1:])
_untar(filename, os.path.dirname(filename), ext)
elif ext == ".bz2":
logger.info("Unbz2 in {0}".format(filename))
_unbz2(filename)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment