From 9067a1844bad68399b85d847ecf4874111d0a262 Mon Sep 17 00:00:00 2001 From: mabw-rte <41002227+mabw-rte@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:51:21 +0200 Subject: [PATCH] feat(upgrade): add a function identifying `upgrade` temporary directories (#23) --- .../study/version/upgrade_app/__init__.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/antares/study/version/upgrade_app/__init__.py b/src/antares/study/version/upgrade_app/__init__.py index 5353429..0f36087 100644 --- a/src/antares/study/version/upgrade_app/__init__.py +++ b/src/antares/study/version/upgrade_app/__init__.py @@ -15,6 +15,18 @@ logger = logging.getLogger(__name__) +UPGRADE_TEMPORARY_DIR_SUFFIX = ".upgrade.tmp" +UPGRADE_TEMPORARY_DIR_PREFIX = "~" + + +def is_temporary_upgrade_dir(path: Path) -> bool: + """Check if a directory is a temporary upgrade directory.""" + return ( + path.name.startswith(UPGRADE_TEMPORARY_DIR_PREFIX) + and "".join(path.suffixes[-2:]) == UPGRADE_TEMPORARY_DIR_SUFFIX + and path.is_dir() + ) + def filter_out_child_files(files: t.Collection[str]) -> t.List[str]: """ @@ -91,7 +103,9 @@ def should_denormalize(self) -> bool: return any(meth.should_denormalize for meth in self.upgrade_methods) def __call__(self) -> None: - with tempfile.TemporaryDirectory(suffix=".upgrade.tmp", prefix="~", dir=self.study_dir.parent) as path: + with tempfile.TemporaryDirectory( + suffix=UPGRADE_TEMPORARY_DIR_SUFFIX, prefix=UPGRADE_TEMPORARY_DIR_PREFIX, dir=self.study_dir.parent + ) as path: tmp_path = Path(path) # Prepare the upgrade