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

Fix load attachments for same shared cache/cache #317

Merged
merged 3 commits into from
May 29, 2024
Merged
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
5 changes: 5 additions & 0 deletions audb/core/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def _cached_versions(
# This fixes https://github.com/audeering/audb/issues/101
if cache_root is None and os.path.exists(default_cache_root(shared=True)):
df = pd.concat((df, cached(name=name, shared=True)))
# Ensure to remove duplicates,
# which can occur if cache and shared cache
# point to the same folder.
# Compare https://github.com/audeering/audb/issues/314
df = df[~df.index.duplicated(keep="first")]

cached_versions = []
for flavor_root, row in df.iterrows():
Expand Down
65 changes: 65 additions & 0 deletions tests/test_attachments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os

import pytest

import audeer
import audformat

import audb


@pytest.fixture(scope="function", autouse=False)
def same_cache():
r"""Set shared cache to same folder as user cache."""
current_shared_cache = audb.config.SHARED_CACHE_ROOT
audb.config.SHARED_CACHE_ROOT = audb.config.CACHE_ROOT

yield

audb.config.SHARED_CACHE_ROOT = current_shared_cache


def test_loading_attachments_from_cache(tmpdir, repository, same_cache):
r"""Ensures loading attachments from cache works.

As reported in https://github.com/audeering/audb/issues/314,
loading an attachment for a database
fails to acquire a lock,
if the database has a previous version in the cache,
and ``audb.config.CACHE_ROOT`` and ``audb.config.SHARED_CACHE_ROOT``
point to the same folder.

"""
# Create version 1.0.0 of database,
# publish and load
db_name = "db"
db_version = "1.0.0"
db_root = audeer.mkdir(audeer.path(tmpdir, db_name))
db = audformat.Database(db_name)
db.description = f"Version {db_version} of database."
filename = "file.txt"
with open(audeer.path(db_root, filename), "w") as file:
file.write(f"{filename}\n")
db.attachments[filename] = audformat.Attachment(filename)
db.save(db_root)

audb.publish(db_root, db_version, repository)

db = audb.load(db_name, version=db_version, verbose=False)

# Create version 2.0.0 of database,
# and publish and load
db = audb.load_to(db_root, db_name, version=db_version)
db_version = "2.0.0"
db.description = f"Version {db_version} of database."
db.save(db_root)

audb.publish(db_root, db_version, repository)

db = audb.load(db_name, version=db_version, verbose=False)

# Ensure attachment file is loaded
assert list(db.attachments) == [filename]
for attachment in list(db.attachments):
for file in db.attachments[attachment].files:
assert os.path.exists(audeer.path(db.root, file))
Loading