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

Drop lockfile as direct dependency #7310

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Expand Up @@ -59,7 +59,6 @@ html5lib = "^1.0"
importlib-metadata = { version = ">=4.4,<6", python = "<3.10" }
jsonschema = "^4.10.0"
keyring = "^23.9.0"
lockfile = "^0.12.2"
# packaging uses calver, so version is unclamped
packaging = ">=20.4"
pexpect = "^4.7.0"
Expand Down Expand Up @@ -172,7 +171,6 @@ warn_unused_ignores = false
[[tool.mypy.overrides]]
module = [
'cachecontrol.*',
'lockfile.*',
'pexpect.*',
'requests_toolbelt.*',
'shellingham.*',
Expand Down
35 changes: 32 additions & 3 deletions src/poetry/utils/authenticator.py
Expand Up @@ -4,6 +4,9 @@
import dataclasses
import functools
import logging
import os
import socket
import threading
import time
import urllib.parse

Expand All @@ -12,7 +15,6 @@
from typing import TYPE_CHECKING
from typing import Any

import lockfile
import requests
import requests.auth
import requests.exceptions
Expand All @@ -35,7 +37,7 @@
logger = logging.getLogger(__name__)


class FileLockLockFile(lockfile.LockBase): # type: ignore[misc]
class FileLockLockFile:
# The default LockFile from the lockfile package as used by cachecontrol can remain
# locked if a process exits ungracefully. See eg
# <https://github.com/python-poetry/poetry/issues/6030#issuecomment-1189383875>.
Expand All @@ -45,7 +47,24 @@ class FileLockLockFile(lockfile.LockBase): # type: ignore[misc]
def __init__(
self, path: str, threaded: bool = True, timeout: float | None = None
) -> None:
super().__init__(path, threaded, timeout)
self.path = path
self.lock_file = f"{Path(path).absolute()}.lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
thread_ident = threading.current_thread().ident
assert thread_ident is not None
self.tname = f"-{thread_ident & 0xffffffff}" if threaded else ""
# unique name is mostly about the current process, but must
# also contain the path -- otherwise, two adjacent locked
# files conflict (one file gets locked, creating lock-file and
# unique file, the other one gets locked, creating lock-file
# and overwriting the already existing lock-file, then one
# gets unlocked, deleting both lock-file and unique file,
# finally the last lock errors out upon releasing.
self.unique_name = Path(self.lock_file).parent.joinpath(
f"{self.hostname}{self.tname}.{self.pid}{hash(self.path)}"
)
self.timeout = timeout
self.file_lock = FileLock(self.lock_file)

def acquire(self, timeout: float | None = None) -> None:
Expand All @@ -54,6 +73,16 @@ def acquire(self, timeout: float | None = None) -> None:
def release(self) -> None:
self.file_lock.release()

def __enter__(self) -> FileLockLockFile:
self.acquire()
return self

def __exit__(self, *_exc: tuple[Any, ...]) -> None:
self.release()

def __repr__(self) -> str:
return f"<{self.__class__.__name__}: {self.unique_name!r} -- {self.path!r}>"


@dataclasses.dataclass(frozen=True)
class RepositoryCertificateConfig:
Expand Down