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

WIP: wrap: Implemented wraplock #13784

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion mesonbuild/wrap/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from functools import lru_cache

from . import WrapMode
from .wraplock import WrapLock
from .. import coredata
from ..mesonlib import quiet_git, GIT, ProgressBar, MesonException, windows_proof_rmtree, Popen_safe
from ..interpreterbase import FeatureNew
Expand Down Expand Up @@ -304,6 +305,7 @@ class Resolver:
def __post_init__(self) -> None:
self.subdir_root = os.path.join(self.source_dir, self.subdir)
self.cachedir = os.environ.get('MESON_PACKAGE_CACHE_DIR') or os.path.join(self.subdir_root, 'packagecache')
self.wraplock = WrapLock(self.subdir_root)
self.wraps: T.Dict[str, PackageDefinition] = {}
self.netrc: T.Optional[netrc] = None
self.provided_deps: T.Dict[str, PackageDefinition] = {}
Expand Down Expand Up @@ -427,7 +429,7 @@ def find_program_provider(self, names: T.List[str]) -> T.Optional[str]:
return wrap_name
return None

def resolve(self, packagename: str, force_method: T.Optional[Method] = None) -> T.Tuple[str, Method]:
def _resolve(self, packagename: str, force_method: T.Optional[Method]) -> T.Tuple[str, Method]:
wrap = self.wraps.get(packagename)
if wrap is None:
wrap = self.get_from_wrapdb(packagename)
Expand Down Expand Up @@ -525,6 +527,10 @@ def has_buildfile() -> bool:
self.wrap.update_hash_cache(self.dirname)
return rel_path, method

def resolve(self, packagename: str, force_method: T.Optional[Method] = None) -> T.Tuple[str, Method]:
with self.wraplock:
return self._resolve(packagename, force_method)

def check_can_download(self) -> None:
# Don't download subproject data based on wrap file if requested.
# Git submodules are ok (see above)!
Expand Down
43 changes: 43 additions & 0 deletions mesonbuild/wrap/wraplock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

import os

try:
import fcntl
except ImportError:
class WrapLock():
def __init__(self, subdir_root: str):
pass
def __enter__(self, *args, **kwargs) -> None:
pass
def __exit__(self, *args, **kwargs) -> None:
pass
else:
class WrapLock():
def __init__(self, subdir_root: str):
self.lock_file = os.path.join(subdir_root, ".wraplock")

"""Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems."""
def __enter__(self, *args, **kwargs) -> None:
try:
self.fd = os.open(self.lock_file,
os.O_RDWR | os.O_TRUNC | os.O_CREAT,
0o644);
except FileNotFoundError:
self.fd = -1
return
fcntl.flock(self.fd, fcntl.LOCK_EX)

def __exit__(self, *args, **kwargs) -> None:
if self.fd == -1:
return;
# Do not remove the lockfile:
# https://github.com/tox-dev/py-filelock/issues/31
# https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to create a file? IIRC you can os.open(subdir_root, os.O_RDONLY) and lock it. Would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flock should work with directories, but for potability reasons (mostly orienting on what filelock did) files are better.

fcntl.flock(self.fd, fcntl.LOCK_UN)
os.close(self.fd)
self.fd = -1

__all__ = [
"WrapLock",
]
3 changes: 2 additions & 1 deletion test cases/unit/116 empty project/expected_mods.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@
"mesonbuild.utils.universal",
"mesonbuild.utils.vsenv",
"mesonbuild.wrap",
"mesonbuild.wrap.wrap"
"mesonbuild.wrap.wrap",
"mesonbuild.wrap.wraplock"
],
"count": 69
}
Expand Down
Loading