PHP 8.2.30
Preview: file_lock.py Size: 2.30 KB
/opt/hc_python/lib64/python3.12/site-packages/pre_commit/file_lock.py

from __future__ import annotations

import contextlib
import errno
import sys
from collections.abc import Callable
from collections.abc import Generator


if sys.platform == 'win32':  # pragma: no cover (windows)
    import msvcrt

    # https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/locking

    # on windows we lock "regions" of files, we don't care about the actual
    # byte region so we'll just pick *some* number here.
    _region = 0xffff

    @contextlib.contextmanager
    def _locked(
            fileno: int,
            blocked_cb: Callable[[], None],
    ) -> Generator[None]:
        try:
            msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region)
        except OSError:
            blocked_cb()
            while True:
                try:
                    msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)
                except OSError as e:
                    # Locking violation. Returned when the _LK_LOCK or _LK_RLCK
                    # flag is specified and the file cannot be locked after 10
                    # attempts.
                    if e.errno != errno.EDEADLOCK:
                        raise
                else:
                    break

        try:
            yield
        finally:
            # From cursory testing, it seems to get unlocked when the file is
            # closed so this may not be necessary.
            # The documentation however states:
            # "Regions should be locked only briefly and should be unlocked
            # before closing a file or exiting the program."
            msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region)
else:  # pragma: win32 no cover
    import fcntl

    @contextlib.contextmanager
    def _locked(
            fileno: int,
            blocked_cb: Callable[[], None],
    ) -> Generator[None]:
        try:
            fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except OSError:  # pragma: no cover (tests are single-threaded)
            blocked_cb()
            fcntl.flock(fileno, fcntl.LOCK_EX)
        try:
            yield
        finally:
            fcntl.flock(fileno, fcntl.LOCK_UN)


@contextlib.contextmanager
def lock(
        path: str,
        blocked_cb: Callable[[], None],
) -> Generator[None]:
    with open(path, 'a+') as f:
        with _locked(f.fileno(), blocked_cb):
            yield

Directory Contents

Dirs: 5 × Files: 25

Name Size Perms Modified Actions
commands DIR
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
languages DIR
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
resources DIR
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
1.43 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
16.42 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
3.14 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
282 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.56 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
78 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.56 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.30 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
8.33 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.48 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
5.26 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1019 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
15.54 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
911 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.42 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
495 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
7.43 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
4.06 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
8.27 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
6.88 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
5.42 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
561 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.31 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
0 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
127 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).