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

BUG: symlink dirs treated as files during indexing #1087

Merged
merged 2 commits into from
Sep 18, 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
19 changes: 17 additions & 2 deletions bids/layout/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ def _index_dir(self, path, config, force=None):
# Get lists of 1st-level subdirectories and files in the path directory
_, dirnames, filenames = next(path.fs.walk(path.path))

# Symbolic links are returned as filenames even if they point to directories
# Temporary list to store symbolic links that need to be removed from filenames
links_to_dirs = []

# Find directories in filenames
for possible_link in filenames:
full_path = path / possible_link
if full_path.is_dir():
links_to_dirs.append(possible_link)

dirnames.extend(links_to_dirs)

for link in links_to_dirs:
filenames.remove(link)

# Move any directories suffixed with .zarr to filenames
zarrnames = [entry for entry in dirnames if Path(entry).suffix == '.zarr']
dirnames = [entry for entry in dirnames if Path(entry).suffix != '.zarr']
Expand Down Expand Up @@ -332,7 +347,7 @@ def load_json(path):

to_store = (file_ents, payload, bf.path)
file_data[key][bf._dirname].append(to_store)

# To avoid integrity errors, track primary keys we've seen
seen_assocs = set()

Expand Down Expand Up @@ -494,7 +509,7 @@ def create_association_pair(src, dst, kind, kind2=None):
self.session.add(all_entities[md_key])
tag = _create_tag_dict(bf, all_entities[md_key], md_val, is_metadata=True)
all_tag_dicts.append(tag)

self.session.bulk_save_objects(all_objs)
self.session.bulk_insert_mappings(Tag, all_tag_dicts)
self.session.commit()
Expand Down
19 changes: 19 additions & 0 deletions bids/layout/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,3 +1140,22 @@ def test_indexer_patterns(fname):
[re.compile(r"/\.")],
root=root,
) is (".datalad" in fname)


def test_symlinks_in_path(tmp_path):

src_ds = os.path.abspath(join(get_test_data_path(), '7t_trt'))
src_sub = join(src_ds, 'sub-04')

# This will be a normal directory with symlink contents
ds_with_links = Path(tmp_path / '7t_trt').absolute()

os.makedirs(ds_with_links, exist_ok=False)

link_ds_description = ds_with_links / 'dataset_description.json'
link_sub = ds_with_links / 'sub-04'

os.symlink(join(src_ds, 'dataset_description.json'), link_ds_description)
os.symlink(src_sub, link_sub)

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