Skip to content

Commit

Permalink
Revert "rollback lock file when installer fails"
Browse files Browse the repository at this point in the history
This reverts commit 132e1347a080018dda42507185fa81cc05b808a6.
  • Loading branch information
wagnerluis1982 committed Feb 14, 2023
1 parent 974b749 commit 7e8e8fb
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 61 deletions.
50 changes: 3 additions & 47 deletions src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
from __future__ import annotations

import shutil
import tempfile

from pathlib import Path
from typing import TYPE_CHECKING

from cleo.io.null_io import NullIO
Expand Down Expand Up @@ -64,7 +60,6 @@ def __init__(

self._execute_operations = True
self._lock = False
self._lock_prev_state: Path | None = None

self._whitelist: list[NormalizedName] = []

Expand Down Expand Up @@ -287,7 +282,7 @@ def _do_install(self) -> int:
self._populate_lockfile_repo(lockfile_repo, ops)

if self._update:
self._write_lock_file(lockfile_repo, backup=not self._lock)
self._write_lock_file(lockfile_repo)

if self._lock:
# If we are only in lock mode, no need to go any further
Expand Down Expand Up @@ -357,22 +352,10 @@ def _do_install(self) -> int:
self._filter_operations(ops, lockfile_repo)

# Execute operations
status = self._execute(ops)

# On errors, restore lock file state
if status != 0:
self._restore_lock_file()

self._cleanup()
return self._execute(ops)

return status

def _write_lock_file(
self, repo: LockfileRepository, force: bool = False, backup: bool = False
) -> None:
def _write_lock_file(self, repo: LockfileRepository, force: bool = False) -> None:
if self._write_lock and (force or self._update):
if backup:
self._backup_lock_file()
updated_lock = self._locker.set_lock_data(self._package, repo.packages)

if updated_lock:
Expand Down Expand Up @@ -584,30 +567,3 @@ def _get_installer(self) -> BaseInstaller:

def _get_installed(self) -> InstalledRepository:
return InstalledRepository.load(self._env)

def _backup_lock_file(self) -> None:
# Store current state of the lock file as a backup if the lock file exists
if not self._locker.lock.exists():
self._lock_prev_state = Path("NOT_EXIST")
else:
self._lock_prev_state = Path(tempfile.mkstemp("-poetry.lock", "backup-")[1])
shutil.copyfile(self._locker.lock, self._lock_prev_state)

def _restore_lock_file(self) -> None:
# Restore last state of the lock file, deleting it if there was none before.
if self._lock_prev_state:
if self._lock_prev_state == Path("NOT_EXIST"):
self._locker.lock.unlink(missing_ok=True)
else:
self._lock_prev_state.replace(self._locker.lock)

# Enforce to refresh lock data from the restored file
del self._locker.lock_data

def _cleanup(self) -> None:
if self._lock_prev_state:
self._lock_prev_state.unlink(missing_ok=True)
self._lock_prev_state = None

def __del__(self) -> None:
self._cleanup()
4 changes: 0 additions & 4 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ def lock_data(self) -> dict[str, Any]:

return self._lock_data

@lock_data.deleter
def lock_data(self) -> None:
self._lock_data = None

def is_locked(self) -> bool:
"""
Checks whether the locker has been locked (lockfile found).
Expand Down
6 changes: 0 additions & 6 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ def __init__(self, lock: str | Path, local_config: dict) -> None:
self._lock_data = None
self._write = False

@property
def lock(self) -> Path:
if isinstance(self._lock, TOMLFile):
return self._lock.path
return self._lock

def write(self, write: bool = True) -> None:
self._write = write

Expand Down
4 changes: 2 additions & 2 deletions tests/installation/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def load(

class Locker(BaseLocker):
def __init__(self, lock_path: str | Path) -> None:
self._lock = Path(lock_path).joinpath("poetry.lock")
self._lock = TOMLFile(Path(lock_path).joinpath("poetry.lock"))
self._written_data = None
self._locked = False
self._content_hash = self._get_content_hash()
Expand All @@ -112,7 +112,7 @@ def written_data(self) -> dict | None:
return self._written_data

def set_lock_path(self, lock: str | Path) -> Locker:
self._lock = Path(lock).joinpath("poetry.lock")
self._lock = TOMLFile(Path(lock).joinpath("poetry.lock"))

return self

Expand Down
4 changes: 2 additions & 2 deletions tests/installation/test_installer_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def load(

class Locker(BaseLocker):
def __init__(self, lock_path: str | Path) -> None:
self._lock = Path(lock_path).joinpath("poetry.lock")
self._lock = TOMLFile(Path(lock_path).joinpath("poetry.lock"))
self._written_data = None
self._locked = False
self._content_hash = self._get_content_hash()
Expand All @@ -64,7 +64,7 @@ def written_data(self) -> dict | None:
return self._written_data

def set_lock_path(self, lock: str | Path) -> Locker:
self._lock = Path(lock).joinpath("poetry.lock")
self._lock = TOMLFile(Path(lock).joinpath("poetry.lock"))

return self

Expand Down

0 comments on commit 7e8e8fb

Please sign in to comment.