def test_rwlock_reentrant(tmp_path): path = fspath(tmp_path) foo = PathInfo("foo") with rwlock(path, "cmd1", [], [foo]): with rwlock(path, "cmd1", [], [foo]): pass with _edit_rwlock(path) as lock: assert lock == { "read": {}, "write": { "foo": { "cmd": "cmd1", "pid": os.getpid() } }, } with rwlock(path, "cmd", [foo], []): with rwlock(path, "cmd", [foo], []): pass with _edit_rwlock(path) as lock: assert lock == { "read": { "foo": [{ "cmd": "cmd", "pid": os.getpid() }] }, "write": {}, }
def rwlocked(call, read=None, write=None): import sys from dvc.rwlock import rwlock from dvc.dependency.repo import DependencyREPO if read is None: read = [] if write is None: write = [] stage = call._args[0] assert stage.repo.lock.is_locked def _chain(names): return [ item.path_info for attr in names for item in getattr(stage, attr) # There is no need to lock DependencyREPO deps, as there is no # corresponding OutputREPO, so we can't even write it. if not isinstance(item, DependencyREPO) ] cmd = " ".join(sys.argv) with rwlock(stage.repo.tmp_dir, cmd, _chain(read), _chain(write)): return call()
def rwlocked(call, read=None, write=None): import sys from dvc.dependency.repo import RepoDependency from dvc.rwlock import rwlock if read is None: read = [] if write is None: write = [] stage = call._args[0] # pylint: disable=protected-access assert stage.repo.lock.is_locked def _chain(names): return [ item.fs_path for attr in names for item in getattr(stage, attr) # There is no need to lock RepoDependency deps, as there is no # corresponding OutputREPO, so we can't even write it. if not isinstance(item, RepoDependency) ] cmd = " ".join(sys.argv) with rwlock(stage.repo.tmp_dir, cmd, _chain(read), _chain(write)): return call()
def test_rwlock_subdirs(tmp_path): path = fspath(tmp_path) foo = PathInfo("foo") subfoo = PathInfo("foo/subfoo") with rwlock(path, "cmd1", [foo], []): with pytest.raises(LockError): with rwlock(path, "cmd2", [], [subfoo]): pass with rwlock(path, "cmd1", [], [subfoo]): with pytest.raises(LockError): with rwlock(path, "cmd2", [foo], []): pass with rwlock(path, "cmd1", [], [subfoo]): with pytest.raises(LockError): with rwlock(path, "cmd2", [], [foo]): pass with rwlock(path, "cmd1", [subfoo], []): with rwlock(path, "cmd2", [foo], []): pass
def test_rwlock_subdirs(tmp_path): path = os.fspath(tmp_path) foo = PathInfo("foo") subfoo = PathInfo("foo/subfoo") with rwlock(path, "cmd1", [foo], []): with pytest.raises(LockError, match=r"subfoo(.|\n)*cmd1"): with rwlock(path, "cmd2", [], [subfoo]): pass with rwlock(path, "cmd1", [], [subfoo]): with pytest.raises(LockError, match=r"'foo'(.|\n)*cmd1"): with rwlock(path, "cmd2", [foo], []): pass with rwlock(path, "cmd1", [], [subfoo]): with pytest.raises(LockError): with rwlock(path, "cmd2", [], [foo]): pass with rwlock(path, "cmd1", [subfoo], []): with rwlock(path, "cmd2", [foo], []): pass
def test_rwlock(tmp_path): path = os.fspath(tmp_path) foo = PathInfo("foo") with rwlock(path, "cmd1", [foo], []): with pytest.raises(LockError): with rwlock(path, "cmd2", [], [foo]): pass with rwlock(path, "cmd1", [], [foo]): with pytest.raises(LockError): with rwlock(path, "cmd2", [foo], []): pass with rwlock(path, "cmd1", [], [foo]): with pytest.raises(LockError): with rwlock(path, "cmd2", [], [foo]): pass