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: Ensure dotfiles are always filtered #1123

Merged
merged 3 commits into from
Mar 5, 2025
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
8 changes: 8 additions & 0 deletions src/bids/layout/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os.path import join
import shutil

import pytest

Expand Down Expand Up @@ -101,3 +102,10 @@ def layout_synthetic(tests_dir, request, db_dir):
def layout_synthetic_nodb(tests_dir, request, db_dir):
path = tests_dir / 'data' / 'synthetic'
return BIDSLayout(path, derivatives=True)


@pytest.fixture
def temporary_dataset(tmp_path, tests_dir):
path = tests_dir / 'data' / 'ds005'
shutil.copytree(path, tmp_path / 'ds005')
return tmp_path / 'ds005'
35 changes: 35 additions & 0 deletions src/bids/layout/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,3 +1170,38 @@ def test_symlinks_in_path(tests_dir, tmp_path):
os.symlink(src_sub, link_sub)

assert "Subjects: 1 | Sessions: 2 | Runs: 2" in str(BIDSLayout(tmp_path / "7t_trt"))


def test_ignore_dotfiles(temporary_dataset):
arbitrary_dotfile = temporary_dataset / '.dotfile'
ds_store = temporary_dataset / 'sub-01' / '.DS_Store'
osx_companion_file = temporary_dataset / '._task-mixedgamblestask_bold.json'

arbitrary_dotfile.touch()
ds_store.touch()
osx_companion_file.touch()

# Default behavior
layout = BIDSLayout(temporary_dataset, validate=False)
assert str(temporary_dataset / 'dataset_description.json') in layout.files
assert str(arbitrary_dotfile) not in layout.files
assert str(ds_store) not in layout.files
assert str(osx_companion_file) not in layout.files

# Explicit ignores do not disable dotfile filtering
indexer = BIDSLayoutIndexer(ignore=['some_ignore'])
layout = BIDSLayout(temporary_dataset, validate=False, indexer=indexer)
assert str(temporary_dataset / 'dataset_description.json') in layout.files
assert str(arbitrary_dotfile) not in layout.files
assert str(ds_store) not in layout.files
assert str(osx_companion_file) not in layout.files


def test_empty_directory(temporary_dataset):
anat = temporary_dataset / 'sub-01' / 'anat'
shutil.rmtree(anat)
anat.mkdir()

layout = BIDSLayout(temporary_dataset)

assert layout.get(subject='01', datatype='anat') == []
13 changes: 9 additions & 4 deletions src/bids/layout/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@

DEFAULT_LOCATIONS_TO_IGNORE = {
re.compile(r"^/(code|models|sourcedata|stimuli)"),
re.compile(r'/\.'),
}

ALWAYS_IGNORE = (
re.compile(r'/\.'), # dotfiles should never be indexed
)

def absolute_path_deprecation_warning():
warnings.warn("The absolute_paths argument will be removed from PyBIDS "
"in 0.14. You can easily access the relative path of "
Expand Down Expand Up @@ -156,9 +159,11 @@ def _sort_patterns(patterns, root):

def validate_indexing_args(ignore, force_index, root):
if ignore is None:
ignore = list(
DEFAULT_LOCATIONS_TO_IGNORE - set(force_index or [])
)
ignore = DEFAULT_LOCATIONS_TO_IGNORE - set(force_index or [])

ignore = list(ignore)

ignore.extend(ALWAYS_IGNORE)

# root has already been validated to be a directory
ignore = _sort_patterns(ignore, root)
Expand Down
Loading