From 3ce09d0d725add6210edbb7cf864e4883631d76f Mon Sep 17 00:00:00 2001 From: Mateusz Masiarz Date: Wed, 27 Sep 2023 16:15:56 +0200 Subject: [PATCH] Function to check if there are correct permissions (cherry picked from commit 92d097fa39a752f3bd6b1b5c31bcbf098ac645b0) --- src/sinol_make/helpers/cache.py | 14 ++++++++++++++ src/sinol_make/util.py | 32 ++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/sinol_make/helpers/cache.py b/src/sinol_make/helpers/cache.py index 24128b7f..d062fb5e 100644 --- a/src/sinol_make/helpers/cache.py +++ b/src/sinol_make/helpers/cache.py @@ -117,3 +117,17 @@ def remove_results_if_contest_type_changed(contest_type): if package_util.check_if_contest_type_changed(contest_type): remove_results_cache() package_util.save_contest_type_to_cache(contest_type) + + +def check_can_access_cache(): + """ + Checks if user can access cache. + """ + try: + os.makedirs(paths.get_cache_path(), exist_ok=True) + with open(paths.get_cache_path("test"), "w") as f: + f.write("test") + os.unlink(paths.get_cache_path("test")) + except PermissionError: + util.exit_with_error("You don't have permission to access the `.cache/` directory. " + "`sinol-make` needs to be able to write to this directory.") diff --git a/src/sinol_make/util.py b/src/sinol_make/util.py index a0fb7173..6aabf743 100644 --- a/src/sinol_make/util.py +++ b/src/sinol_make/util.py @@ -10,6 +10,7 @@ import sinol_make from sinol_make.contest_types import get_contest_type +from sinol_make.helpers import paths, cache from sinol_make.structs.status_structs import Status @@ -54,6 +55,7 @@ def exit_if_not_package(): """ if not find_and_chdir_package(): exit_with_error('You are not in a package directory (couldn\'t find config.yml in current directory).') + cache.check_can_access_cache() def save_config(config): @@ -141,16 +143,21 @@ def check_for_updates(current_version) -> Union[str, None]: thread.start() version_file = data_dir.joinpath("version") - if version_file.is_file(): + try: version = version_file.read_text() + except PermissionError: try: - if compare_versions(current_version, version) == -1: - return version - else: - return None - except ValueError: # If the version file is corrupted, we just ignore it. + with open(paths.get_cache_path("sinol_make_version"), "r") as f: + version = f.read() + except (FileNotFoundError, PermissionError): return None - else: + + try: + if compare_versions(current_version, version) == -1: + return version + else: + return None + except ValueError: # If the version file is corrupted, we just ignore it. return None @@ -173,7 +180,16 @@ def check_version(): latest_version = data["info"]["version"] version_file = importlib.files("sinol_make").joinpath("data/version") - version_file.write_text(latest_version) + try: + version_file.write_text(latest_version) + except PermissionError: + if find_and_chdir_package(): + try: + os.makedirs(paths.get_cache_path(), exist_ok=True) + with open(paths.get_cache_path("sinol_make_version"), "w") as f: + f.write(latest_version) + except PermissionError: + pass def compare_versions(version_a, version_b):