Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wagnerluis1982 committed Feb 17, 2023
1 parent 47abc59 commit 7562620
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import os

from typing import TYPE_CHECKING
Expand Down Expand Up @@ -606,15 +607,8 @@ def set_lock_data(self, root: Package, packages: list[Package]) -> bool:
# to make sure we have permissions later.
self._lock_data = self._locker.compute_lock_data(root, packages)

# On verify if we can write to the lock file, an `open(lock, "a")` is enough.
# However, in case of no lock file exists and the installation fails, then an
# empty poetry.lock would be kept in the filesystem.
# So, a non-intrusive approach is done here to emulate what `open` would do.
if self._should_write():
path = self._locker.lock
path = path if self.is_locked() else path.parent
if not os.access(path, os.W_OK):
raise PermissionError(f"Permission denied on writing to file '{path}'")
self._check_writable()
return True

return False
Expand All @@ -627,12 +621,19 @@ def rollback(self) -> None:
self._lock_data = None

def _should_write(self) -> bool:
# if lock file exists: compare new lock data with old lock data
if self.is_locked():
try:
old_lock_data = self._locker.lock_data
except RuntimeError:
# incompatible, invalid or no lock file
pass
else:
return self._lock_data != old_lock_data
with contextlib.suppress(RuntimeError):
# error happens when incompatible, invalid or no lock file
return self._lock_data != self._locker.lock_data
return True

def _check_writable(self) -> None:
# On verify if lock file is writable, an `open(path, "a")` would be enough.
# However, in case of no lock file exists and the installation fails, then an
# empty poetry.lock would be kept in the filesystem.
# So, a non-intrusive approach is done here to emulate what `open` would do.
path = self._locker.lock
path = path if path.is_file() else path.parent
if not os.access(path, os.W_OK):
raise PermissionError(f"Permission denied on writing to file '{path}'")

0 comments on commit 7562620

Please sign in to comment.