Skip to content

Commit

Permalink
- ENH: Adding files.is_writable function to test if a directory is …
Browse files Browse the repository at this point in the history
…writable or not
  • Loading branch information
remi-braun committed Sep 22, 2021
1 parent 3c4d85b commit 84a78ac
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.Y.Z (YYYY-MM-DD)

## 1.8.0 (2021-09-22)

- ENH: Adding `files.is_writable` function to test if a directory is writable or not
- DOC: Using readthedocs instead of github docs

## 1.7.4 (2021-09-14)
Expand Down
19 changes: 11 additions & 8 deletions CI/SCRIPTS/script_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ class Polarization(ListEnum):
hv = "HV"


def get_s3_ci_path():
client = S3Client(
endpoint_url=f"https://{AWS_S3_ENDPOINT}",
aws_access_key_id=os.getenv(AWS_ACCESS_KEY_ID),
aws_secret_access_key=os.getenv(AWS_SECRET_ACCESS_KEY),
)
client.set_as_default_client()
return AnyPath("s3://sertit-sertit-utils-ci")


def get_proj_path():
"""Get project path"""
if int(os.getenv(CI_SERTIT_S3, 1)) and sys.platform != "win32":
# ON S3
client = S3Client(
endpoint_url=f"https://{AWS_S3_ENDPOINT}",
aws_access_key_id=os.getenv(AWS_ACCESS_KEY_ID),
aws_secret_access_key=os.getenv(AWS_SECRET_ACCESS_KEY),
)
client.set_as_default_client()
return AnyPath("s3://sertit-sertit-utils-ci")
return get_s3_ci_path()
else:
# ON DISK
return AnyPath(__file__).parent.parent.parent
Expand Down
9 changes: 8 additions & 1 deletion CI/SCRIPTS/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from cloudpathlib import AnyPath, CloudPath
from lxml import etree

from CI.SCRIPTS.script_utils import Polarization, files_path, s3_env
from CI.SCRIPTS.script_utils import Polarization, files_path, get_s3_ci_path, s3_env
from sertit import ci, files, misc, vectors


Expand Down Expand Up @@ -56,6 +56,13 @@ def test_paths():
# Root path
assert str(abs_file).startswith(str(files.get_root_path()))

# Writeable
with tempfile.TemporaryDirectory() as tmp_dir:
assert files.is_writable(tmp_dir) # Writeable

assert not files.is_writable(get_s3_ci_path()) # Not writable
assert not files.is_writable("cvfgbherth") # Non existing


def test_archive():
"""Test extracting functions"""
Expand Down
2 changes: 1 addition & 1 deletion sertit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.. include:: ../README.md
"""

__version__ = "1.7.4"
__version__ = "1.8.0"
__title__ = "sertit"
__description__ = ("SERTIT python library for generic tools",)
__author__ = "ICube-SERTIT"
Expand Down
26 changes: 26 additions & 0 deletions sertit/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
""" Tools for paths and files """

import errno
import hashlib
import json
import logging
Expand Down Expand Up @@ -1051,3 +1052,28 @@ def hash_file_content(file_content: str, len_param: int = 5) -> str:
hasher = hashlib.shake_256()
hasher.update(str.encode(file_content))
return hasher.hexdigest(len_param)


def is_writable(dir_path: Union[str, CloudPath, Path]):
"""
Determine whether the directory is writeable or not
Args:
dir_path (Union[str, CloudPath, Path]): Directory path
Returns:
bool: True if the directory is writable
"""
try:
testfile = tempfile.TemporaryFile(dir=dir_path)
testfile.close()
except (OSError, IOError, FileNotFoundError) as e:
if e.errno in [
errno.EACCES,
errno.EEXIST,
errno.EROFS,
errno.ENOENT,
]: # 2, 13, 17, 30
return False
e.filename = dir_path
raise
return True

0 comments on commit 84a78ac

Please sign in to comment.