Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean directories in sync operation processor on close #1093

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/neptune/new/internal/operation_processors/operation_storage.py
Expand Up @@ -18,11 +18,13 @@
]

import os
import shutil
from pathlib import Path

from neptune.new.constants import NEPTUNE_DATA_DIRECTORY
from neptune.new.internal.container_type import ContainerType
from neptune.new.internal.id_formats import UniqueId
from neptune.new.internal.utils.logger import logger


class OperationStorage:
Expand All @@ -44,3 +46,15 @@ def upload_path(self) -> Path:
@staticmethod
def _get_container_dir(type_dir: str, container_id: UniqueId, container_type: ContainerType):
return f"{NEPTUNE_DATA_DIRECTORY}/{type_dir}/{container_type.create_dir_name(container_id)}"

def close(self):
shutil.rmtree(self.data_path, ignore_errors=True)

parent = self.data_path.parent

files = os.listdir(parent)
if len(files) == 0:
try:
os.rmdir(parent)
except OSError:
logger.debug(f"Cannot remove directory: {parent}")
Expand Up @@ -59,4 +59,4 @@ def start(self):
pass

def stop(self, seconds: Optional[float] = None):
pass
self._operation_storage.close()
10 changes: 10 additions & 0 deletions tests/neptune/new/attributes/atoms/test_file.py
Expand Up @@ -161,3 +161,13 @@ def test_fetch_extension(self):
var = File(exp, path)
var.assign(value, wait=wait)
self.assertEqual(expected_ext, var.fetch_extension())

def test_clean_files_on_close(self):
run = self._create_run()
data_path = run._op_processor._operation_storage.data_path

assert os.path.exists(data_path)

run.stop()

assert not os.path.exists(data_path)