Skip to content

Commit 5457fd1

Browse files
authored
CM-51935 - Fix work on read-only file systems (#338)
1 parent b35b6a0 commit 5457fd1

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

cycode/cli/user_settings/base_file_manager.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from typing import Any
55

66
from cycode.cli.utils.yaml_utils import read_yaml_file, update_yaml_file
7+
from cycode.logger import get_logger
8+
9+
logger = get_logger('Base File Manager')
710

811

912
class BaseFileManager(ABC):
@@ -15,5 +18,11 @@ def read_file(self) -> dict[Hashable, Any]:
1518

1619
def write_content_to_file(self, content: dict[Hashable, Any]) -> None:
1720
filename = self.get_filename()
18-
os.makedirs(os.path.dirname(filename), exist_ok=True)
21+
22+
try:
23+
os.makedirs(os.path.dirname(filename), exist_ok=True)
24+
except Exception as e:
25+
logger.warning('Failed to create directory for file, %s', {'filename': filename}, exc_info=e)
26+
return
27+
1928
update_yaml_file(filename, content)

cycode/cli/utils/version_checker.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from cycode.cli.user_settings.configuration_manager import ConfigurationManager
99
from cycode.cli.utils.path_utils import get_file_content
1010
from cycode.cyclient.cycode_client_base import CycodeClientBase
11+
from cycode.logger import get_logger
12+
13+
logger = get_logger('Version Checker')
1114

1215

1316
def _compare_versions(
@@ -154,8 +157,8 @@ def _update_last_check(self) -> None:
154157
os.makedirs(os.path.dirname(self.cache_file), exist_ok=True)
155158
with open(self.cache_file, 'w', encoding='UTF-8') as f:
156159
f.write(str(time.time()))
157-
except OSError:
158-
pass
160+
except Exception as e:
161+
logger.debug('Failed to update version check cache file: %s', {'file': self.cache_file}, exc_info=e)
159162

160163
def check_for_update(self, current_version: str, use_cache: bool = True) -> Optional[str]:
161164
"""Check if an update is available for the current version.

cycode/cli/utils/yaml_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ def _yaml_object_safe_load(file: TextIO) -> dict[Hashable, Any]:
3535

3636

3737
def read_yaml_file(filename: str) -> dict[Hashable, Any]:
38-
if not os.path.exists(filename):
38+
if not os.access(filename, os.R_OK) or not os.path.exists(filename):
39+
logger.debug('Config file is not accessible or does not exist: %s', {'filename': filename})
3940
return {}
4041

4142
with open(filename, encoding='UTF-8') as file:
4243
return _yaml_object_safe_load(file)
4344

4445

4546
def write_yaml_file(filename: str, content: dict[Hashable, Any]) -> None:
47+
if not os.access(filename, os.W_OK) and os.path.exists(filename):
48+
logger.warning('No write permission for file. Cannot save config, %s', {'filename': filename})
49+
return
50+
4651
with open(filename, 'w', encoding='UTF-8') as file:
4752
yaml.safe_dump(content, file)
4853

0 commit comments

Comments
 (0)