From 7045e6c7c158bee96c37285fdf997687c22254c2 Mon Sep 17 00:00:00 2001 From: Ilya Siamionau Date: Tue, 2 Sep 2025 12:51:39 +0200 Subject: [PATCH 1/2] CM-51935 - Add support for read-only file systems --- cycode/cli/user_settings/base_file_manager.py | 11 ++++++++++- cycode/cli/utils/version_checker.py | 7 +++++-- cycode/cli/utils/yaml_utils.py | 7 ++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cycode/cli/user_settings/base_file_manager.py b/cycode/cli/user_settings/base_file_manager.py index 4f07f11c..6a9d0fe2 100644 --- a/cycode/cli/user_settings/base_file_manager.py +++ b/cycode/cli/user_settings/base_file_manager.py @@ -4,6 +4,9 @@ from typing import Any from cycode.cli.utils.yaml_utils import read_yaml_file, update_yaml_file +from cycode.logger import get_logger + +logger = get_logger('Base File Manager') class BaseFileManager(ABC): @@ -15,5 +18,11 @@ def read_file(self) -> dict[Hashable, Any]: def write_content_to_file(self, content: dict[Hashable, Any]) -> None: filename = self.get_filename() - os.makedirs(os.path.dirname(filename), exist_ok=True) + + try: + os.makedirs(os.path.dirname(filename), exist_ok=True) + except Exception as e: + logger.warning('Failed to create directory for file, %s', {'filename': filename}, exc_info=e) + return + update_yaml_file(filename, content) diff --git a/cycode/cli/utils/version_checker.py b/cycode/cli/utils/version_checker.py index 8fd1d005..24146989 100644 --- a/cycode/cli/utils/version_checker.py +++ b/cycode/cli/utils/version_checker.py @@ -8,6 +8,9 @@ from cycode.cli.user_settings.configuration_manager import ConfigurationManager from cycode.cli.utils.path_utils import get_file_content from cycode.cyclient.cycode_client_base import CycodeClientBase +from cycode.logger import get_logger + +logger = get_logger('Version Checker') def _compare_versions( @@ -154,8 +157,8 @@ def _update_last_check(self) -> None: os.makedirs(os.path.dirname(self.cache_file), exist_ok=True) with open(self.cache_file, 'w', encoding='UTF-8') as f: f.write(str(time.time())) - except OSError: - pass + except Exception as e: + logger.debug('Failed to update version check cache file: %s', {'file': self.cache_file}, exc_info=e) def check_for_update(self, current_version: str, use_cache: bool = True) -> Optional[str]: """Check if an update is available for the current version. diff --git a/cycode/cli/utils/yaml_utils.py b/cycode/cli/utils/yaml_utils.py index c53d1ad1..64b1a02d 100644 --- a/cycode/cli/utils/yaml_utils.py +++ b/cycode/cli/utils/yaml_utils.py @@ -35,7 +35,8 @@ def _yaml_object_safe_load(file: TextIO) -> dict[Hashable, Any]: def read_yaml_file(filename: str) -> dict[Hashable, Any]: - if not os.path.exists(filename): + if not os.access(filename, os.R_OK) or not os.path.exists(filename): + logger.debug('Config file is not accessible or does not exist: %s', {'filename': filename}) return {} with open(filename, encoding='UTF-8') as file: @@ -43,6 +44,10 @@ def read_yaml_file(filename: str) -> dict[Hashable, Any]: def write_yaml_file(filename: str, content: dict[Hashable, Any]) -> None: + if not os.access(filename, os.W_OK): + logger.warning('No write permission for file. Cannot save config, %s', {'filename': filename}) + return + with open(filename, 'w', encoding='UTF-8') as file: yaml.safe_dump(content, file) From 931561eaf937c7e394fac11444694f8bfdcfb975 Mon Sep 17 00:00:00 2001 From: Ilya Siamionau Date: Tue, 2 Sep 2025 12:59:50 +0200 Subject: [PATCH 2/2] fix permission check --- cycode/cli/utils/yaml_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycode/cli/utils/yaml_utils.py b/cycode/cli/utils/yaml_utils.py index 64b1a02d..c92acdc8 100644 --- a/cycode/cli/utils/yaml_utils.py +++ b/cycode/cli/utils/yaml_utils.py @@ -44,7 +44,7 @@ def read_yaml_file(filename: str) -> dict[Hashable, Any]: def write_yaml_file(filename: str, content: dict[Hashable, Any]) -> None: - if not os.access(filename, os.W_OK): + if not os.access(filename, os.W_OK) and os.path.exists(filename): logger.warning('No write permission for file. Cannot save config, %s', {'filename': filename}) return