Skip to content

Commit

Permalink
Function to check if there are correct permissions
Browse files Browse the repository at this point in the history
(cherry picked from commit 92d097f)
  • Loading branch information
MasloMaslane committed Sep 27, 2023
1 parent 48fef67 commit 3ce09d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/sinol_make/helpers/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
32 changes: 24 additions & 8 deletions src/sinol_make/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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


Expand All @@ -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):
Expand Down

0 comments on commit 3ce09d0

Please sign in to comment.