Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/jmrplens/phonometry. Standards-conformant acoustics library for Python: measurement and metrology, docs.jmrp.io/phonometry
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778# Copyright (c) 2026. Jose Manuel Requena Plens"""A disk that refuses what a video writer asks of it, for the writers' tests.
The suite may run as root, whom no permission refuses, so a refusal is put inthis process's way instead: a name whose status cannot be read, as inside adirectory without search permission, and a hidden partial file that cannotbe removed, as one a scanner holds open on Windows, or one in a directorythat takes files and lets none go."""
from __future__ import annotations
import errnoimport osimport pathlibfrom typing import TYPE_CHECKING, Any
if TYPE_CHECKING: import pytest
def refuse_status(monkeypatch: pytest.MonkeyPatch, path: pathlib.Path) -> None: """Make reading the status of *path* fail, as inside a directory this process cannot search.""" real_stat = os.stat refused = os.fspath(path)
def stat( target: int | str | bytes | os.PathLike[str] | os.PathLike[bytes], *args: Any, **kwargs: Any, ) -> os.stat_result: if not isinstance(target, int) and os.fspath(target) == refused: raise PermissionError(errno.EACCES, "Permission denied", refused) return real_stat(target, *args, **kwargs)
real_isdir = os.path.isdir
def isdir(target: str | bytes | os.PathLike[str] | os.PathLike[bytes]) -> bool: # Path.is_dir on Python 3.14 asks os.path.isdir, which on Windows # reads the status without os.stat; a real unsearchable directory # reads there as no directory at all. if os.fspath(target) == refused: return False return real_isdir(target)
monkeypatch.setattr(os, "stat", stat) monkeypatch.setattr(os.path, "isdir", isdir)
def hold_partials(monkeypatch: pytest.MonkeyPatch) -> None: """Make removing any hidden partial file fail, as while a scanner holds it open.""" real_unlink = pathlib.Path.unlink
def unlink(self: pathlib.Path, *, missing_ok: bool = False) -> None: if self.name.endswith(".partial"): msg = "the file is being used by another process" raise PermissionError(errno.EACCES, msg, str(self)) real_unlink(self, missing_ok=missing_ok)
monkeypatch.setattr(pathlib.Path, "unlink", unlink)
def refuse_partials(monkeypatch: pytest.MonkeyPatch) -> None: """Make creating any hidden partial file fail, as in a directory this process may not write.""" real_open = pathlib.Path.open
def opened(self: pathlib.Path, *args: Any, **kwargs: Any) -> object: if self.name.endswith(".partial"): raise PermissionError(errno.EACCES, "Permission denied", str(self)) return real_open(self, *args, **kwargs)
monkeypatch.setattr(pathlib.Path, "open", opened)
def partial_sizes(directory: pathlib.Path) -> list[int]: """The size of every hidden partial file in *directory*, smallest first.""" return sorted(p.stat().st_size for p in directory.glob("*.partial"))