Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if files have correct permissions #137

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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, FileNotFoundError):
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
35 changes: 35 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import yaml

from sinol_make import util, configure_parsers
from sinol_make.helpers import paths
from tests import util as test_util
from tests.fixtures import create_package
from tests.commands.run import util as run_util
Expand Down Expand Up @@ -79,6 +80,9 @@ def test_check_version(**kwargs):
version_file = data_dir.joinpath("version")
if not data_dir.is_dir():
data_dir.mkdir()
data_dir.chmod(0o777)
if version_file.is_file():
version_file.unlink()

# Test correct request
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "1.0.0"}})
Expand All @@ -97,6 +101,37 @@ def test_check_version(**kwargs):
util.check_version()
assert not version_file.is_file()

with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
with open("config.yml", "w") as config_file:
config_file.write("")

# No permission to write
version_file.write_text("0.0.0")
version_file.chmod(0o000)
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "1.0.0"}})
util.check_version()
assert version_file.is_file()
version_file.chmod(0o777)
assert version_file.read_text() == "0.0.0"
assert os.path.exists(paths.get_cache_path("sinol_make_version"))
with open(paths.get_cache_path("sinol_make_version"), "r") as f:
assert f.read() == "1.0.0"

# No permission to write to cache and data dir
with open(paths.get_cache_path("sinol_make_version"), "w") as f:
f.write("0.0.0")
os.chmod(paths.get_cache_path("sinol_make_version"), 0o000)
os.chmod(paths.get_cache_path(), 0o000)
os.chmod(data_dir, 0o000)
version_file.chmod(0o000)
mocker.get("https://pypi.python.org/pypi/sinol-make/json", json={"info": {"version": "1.0.0"}})
util.check_version()
os.chmod(data_dir, 0o777)
version_file.chmod(0o777)
assert version_file.is_file()
assert version_file.read_text() == "0.0.0"


@pytest.mark.parametrize("create_package", [test_util.get_simple_package_path()], indirect=True)
def test_version_change(create_package):
Expand Down
Loading