Skip to content

Commit

Permalink
FIX: Better management of temporary directories (name, cleaning, etc)
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Jan 17, 2025
1 parent 8db3208 commit 5599f55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History

## 0.21.9 (2024-mm-dd)

- FIX: Better management of temporary directories (name, cleaning, etc)

## 0.21.8 (2024-01-13)

- ENH: Add a new type (`BandsType`) for list of BandType
Expand Down
31 changes: 24 additions & 7 deletions eoreader/products/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def __init__(
self._output = AnyPath(self._tmp_output.name)

# Temporary file path (private)
self._tmp_process = self._output.joinpath(f"tmp_{self.condensed_name}")
self._tmp_process = self._output.joinpath("tmp")
os.makedirs(self._tmp_process, exist_ok=True)

# Pre initialization
Expand Down Expand Up @@ -301,6 +301,10 @@ def __init__(
# Condensed name
self.condensed_name = self._get_condensed_name()

# Once we get the condensed name, move the temporary folder in order to have its correct name
# This is to avoid a meaningless tmp folder (tmp_None) if the output is given directly in the init of the product
self._move_tmp_process(f"tmp_{self.condensed_name}")

def __del__(self):
"""Cleaning up _tmp directory"""
self.clear()
Expand All @@ -309,7 +313,11 @@ def __del__(self):
if self._tmp_output:
self._tmp_output.cleanup()

elif self._remove_tmp_process:
elif (
self._remove_tmp_process
and self._tmp_process is not None
and self._tmp_process.exists()
):
files.remove(self._tmp_process)

@abstractmethod
Expand Down Expand Up @@ -1436,20 +1444,29 @@ def output(self, value: str):
if not path.is_cloud_path(self._output):
self._output = self._output.resolve()

# Move temporary process folder
self._move_tmp_process(f"tmp_{self.condensed_name}")

# Remove old output if existing into the new output
if self._tmp_output:
self._tmp_output.cleanup()
self._tmp_output = None

def _move_tmp_process(self, new_tmp_name: str):
"""Move temporary process folder"""
# Create temporary process folder
old_tmp_process = self._tmp_process
self._tmp_process = self._output.joinpath(f"tmp_{self.condensed_name}")
self._tmp_process = self._output.joinpath(new_tmp_name)
os.makedirs(self._tmp_process, exist_ok=True)

# Move all files from old process folder into the new one
for file in path.listdir_abspath(old_tmp_process):
with contextlib.suppress(shutil.Error):
shutil.move(str(file), self._tmp_process)

# Remove old output if existing into the new output
if self._tmp_output:
self._tmp_output.cleanup()
self._tmp_output = None
# Remove opld temp process
if old_tmp_process is not None:
files.remove(old_tmp_process)

@property
def stac(self) -> StacItem:
Expand Down

0 comments on commit 5599f55

Please sign in to comment.