Skip to content

Commit

Permalink
Merge pull request #1087 from cookpa/traverse_link_dirs
Browse files Browse the repository at this point in the history
BUG: symlink dirs treated as files during indexing
  • Loading branch information
effigies authored Sep 18, 2024
2 parents 95c7aa9 + 6ecffc7 commit b6d1b29
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
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"))

0 comments on commit b6d1b29

Please sign in to comment.