From 2def3a153608e0866e9ac0f57e0bb3c64ad2a677 Mon Sep 17 00:00:00 2001 From: Chi Cheng Date: Tue, 7 Jan 2025 12:06:36 +0000 Subject: [PATCH] changed the output file configurators so that it also check if the filename can be written to. Fixed the writable check. --- MDANSE/Src/MDANSE/Core/Platform.py | 52 ++++++++++--------- .../Configurators/OutputFilesConfigurator.py | 6 +-- .../OutputStructureConfigurator.py | 6 +-- .../OutputTrajectoryConfigurator.py | 6 +-- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/MDANSE/Src/MDANSE/Core/Platform.py b/MDANSE/Src/MDANSE/Core/Platform.py index 9db1d00e9..13ecd49d8 100644 --- a/MDANSE/Src/MDANSE/Core/Platform.py +++ b/MDANSE/Src/MDANSE/Core/Platform.py @@ -150,47 +150,49 @@ def change_directory(self, directory): os.chdir(directory) @classmethod - def is_directory_writable(cls, path): + def is_file_writable(cls, filepath: str) -> bool: """Check if the directories can be created and a file can be written into it. Parameters ---------- - path : str - The path to test if a file can be written to it. + filepath : str + The filepath to test if the file can be written. Returns ------- bool - True if a file can be written to the input path. + True if a file can be written. """ - path = cls.get_path(path) + dirname = cls.get_path(os.path.dirname(filepath)) - if os.path.exists(path): - while True: - file = os.path.join(path, next(tempfile._get_candidate_names())) - if not os.path.isfile(file): + def recursive_check(head_0: str) -> bool: + """Builds the directories up and tests if the file can be + written and then removes everything so that no changes are + made to the filesystem. + """ + if os.path.exists(dirname): + if not os.path.isfile(filepath): try: - open(file, "w").close() - os.remove(file) + open(filepath, "w").close() + os.remove(filepath) except OSError: return False return True - head, tail = os.path.split(path) - if not tail: - head, tail = os.path.split(head) - - if os.path.exists(head): - try: - os.mkdir(path) - except OSError: - return False - writable = cls.is_directory_writable(path) - os.rmdir(path) - return writable - else: - return cls.is_directory_writable(head) + head, tail = os.path.split(head_0) + if os.path.exists(head): + try: + os.mkdir(head_0) + except OSError: + return False + writable = recursive_check(dirname) + os.rmdir(head_0) + return writable + else: + return recursive_check(head) + + return recursive_check(dirname) def create_directory(self, path): """ diff --git a/MDANSE/Src/MDANSE/Framework/Configurators/OutputFilesConfigurator.py b/MDANSE/Src/MDANSE/Framework/Configurators/OutputFilesConfigurator.py index fda0bc02a..194b4c555 100644 --- a/MDANSE/Src/MDANSE/Framework/Configurators/OutputFilesConfigurator.py +++ b/MDANSE/Src/MDANSE/Framework/Configurators/OutputFilesConfigurator.py @@ -75,10 +75,8 @@ def configure(self, value): self.error_status = "empty root name for the output file." return - dirname = os.path.dirname(root) - - if not PLATFORM.is_directory_writable(dirname): - self.error_status = f"the directory {dirname} is not writable" + if not PLATFORM.is_file_writable(root): + self.error_status = f"the file {root} is not writable" return if not formats: diff --git a/MDANSE/Src/MDANSE/Framework/Configurators/OutputStructureConfigurator.py b/MDANSE/Src/MDANSE/Framework/Configurators/OutputStructureConfigurator.py index b8b50f0f3..24c7b16a7 100644 --- a/MDANSE/Src/MDANSE/Framework/Configurators/OutputStructureConfigurator.py +++ b/MDANSE/Src/MDANSE/Framework/Configurators/OutputStructureConfigurator.py @@ -74,10 +74,8 @@ def configure(self, value): self.error_status = "empty root name for the output file." return - dirname = os.path.dirname(root) - - if not PLATFORM.is_directory_writable(dirname): - self.error_status = f"the directory {dirname} is not writable" + if not PLATFORM.is_file_writable(root): + self.error_status = f"the file {root} is not writable" return if not format in self.formats: diff --git a/MDANSE/Src/MDANSE/Framework/Configurators/OutputTrajectoryConfigurator.py b/MDANSE/Src/MDANSE/Framework/Configurators/OutputTrajectoryConfigurator.py index a525b9ce2..c2bec2e22 100644 --- a/MDANSE/Src/MDANSE/Framework/Configurators/OutputTrajectoryConfigurator.py +++ b/MDANSE/Src/MDANSE/Framework/Configurators/OutputTrajectoryConfigurator.py @@ -71,10 +71,8 @@ def configure(self, value: tuple): self.error_status = "empty root name for the output file." return - dirname = os.path.dirname(root) - - if not PLATFORM.is_directory_writable(dirname): - self.error_status = f"the directory {dirname} is not writable" + if not PLATFORM.is_file_writable(root): + self.error_status = f"the file {root} is not writable" return if dtype < 17: