Skip to content

Commit

Permalink
feat: handle download and extraction for tarfiles with '.tar' extension
Browse files Browse the repository at this point in the history
  • Loading branch information
anesson-cs committed Nov 20, 2023
1 parent 4cb0f70 commit 759a87e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion eodag/plugins/download/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def _finalize(self, fs_path, progress_callback=None, **kwargs):
os.makedirs(outputs_dir)
shutil.move(product_extraction_path, outputs_dir)

elif fs_path.endswith(".tar.gz"):
elif fs_path.endswith(".tar") or fs_path.endswith(".tar.gz"):
with tarfile.open(fs_path, "r") as zfile:
progress_callback.reset(total=1)
zfile.extractall(path=extraction_dir)
Expand Down
37 changes: 31 additions & 6 deletions eodag/plugins/download/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,18 @@ def download(
)
progress_callback = ProgressCallback(disable=True)

kwargs.pop("outputs_extension", "parameter not available in kwargs")
outputs_extension = getattr(self.config, "products", {}).get(
product.product_type, {}
).get("outputs_extension", None) or getattr(
self.config, "outputs_extension", ".zip"
)

fs_path, record_filename = self._prepare_download(
product, progress_callback=progress_callback, **kwargs
product,
progress_callback=progress_callback,
outputs_extension=outputs_extension,
**kwargs,
)
if not fs_path or not record_filename:
if fs_path:
Expand Down Expand Up @@ -417,14 +427,14 @@ def download_request(product, auth, progress_callback, wait, timeout, **kwargs):
logger.debug("Download recorded in %s", record_filename)

# Check that the downloaded file is really a zip file
outputs_extension = kwargs.get("outputs_extension", None) or getattr(
self.config, "outputs_extension", ".zip"
)
if not zipfile.is_zipfile(fs_path) and outputs_extension == ".zip":
logger.warning(
"Downloaded product is not a Zip File. Please check its file type before using it"
)
new_fs_path = fs_path[: fs_path.index(".zip")]
new_fs_path = os.path.join(
os.path.dirname(fs_path),
sanitize(product.properties["title"]),
)
if os.path.isfile(fs_path) and not tarfile.is_tarfile(fs_path):
if not os.path.isdir(new_fs_path):
os.makedirs(new_fs_path)
Expand All @@ -433,6 +443,18 @@ def download_request(product, auth, progress_callback, wait, timeout, **kwargs):
file_path = os.path.join(new_fs_path, os.listdir(new_fs_path)[0])
new_file_path = file_path[: file_path.index(".zip")]
shutil.move(file_path, new_file_path)
elif tarfile.is_tarfile(fs_path):
if not new_fs_path.endswith(".tar"):
new_fs_path += ".tar"
shutil.move(fs_path, new_fs_path)
product_path = self._finalize(
new_fs_path,
progress_callback=progress_callback,
outputs_extension=".tar",
**kwargs,
)
product.location = path_to_uri(product_path)
return product_path
else:
shutil.move(fs_path, new_fs_path)
product.location = path_to_uri(new_fs_path)
Expand All @@ -451,7 +473,10 @@ def download_request(product, auth, progress_callback, wait, timeout, **kwargs):
product.location = path_to_uri(new_fs_path)
return new_fs_path
product_path = self._finalize(
fs_path, progress_callback=progress_callback, **kwargs
fs_path,
progress_callback=progress_callback,
outputs_extension=outputs_extension,
**kwargs,
)
product.location = path_to_uri(product_path)
return product_path
Expand Down
3 changes: 3 additions & 0 deletions eodag/resources/providers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6278,3 +6278,6 @@
order_status_success:
status: completed
message: "Done!"
products:
COP_DEM_GLO30_DGED:
outputs_extension: .tar

0 comments on commit 759a87e

Please sign in to comment.