Skip to content

Commit

Permalink
changed the output file configurators so that it also check if the fi…
Browse files Browse the repository at this point in the history
…lename can be written to. Fixed the writable check.
ChiCheng45 committed Jan 7, 2025
1 parent 2da45ba commit 2def3a1
Showing 4 changed files with 33 additions and 37 deletions.
52 changes: 27 additions & 25 deletions MDANSE/Src/MDANSE/Core/Platform.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Original file line number Diff line number Diff line change
@@ -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:
Original file line number Diff line number Diff line change
@@ -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:
Original file line number Diff line number Diff line change
@@ -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:

0 comments on commit 2def3a1

Please sign in to comment.