Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cycode/cli/user_settings/base_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
7 changes: 5 additions & 2 deletions cycode/cli/utils/version_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion cycode/cli/utils/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ 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:
return _yaml_object_safe_load(file)


def write_yaml_file(filename: str, content: dict[Hashable, Any]) -> None:
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

with open(filename, 'w', encoding='UTF-8') as file:
yaml.safe_dump(content, file)

Expand Down
Loading