def METHOD_NAME(self, bidsdir):
    bidsdir = Path(bidsdir)
    if bidsdir.is_symlink():
        raise ValueError("Will not write to symlink")
    bidsdir.mkdir(parents=True, exist_ok=True)
    bids_paths = set()
    dataset_description_path = bidsdir / "dataset_description.json"
    bids_paths.add(dataset_description_path)
    dataset_description = {
        "Name": self.database.sha1,
        "BIDSVersion": bids_version,
        "DatasetType": "raw",
    }
    with open(dataset_description_path, "w") as f:
        json.dump(dataset_description, f, indent=4)
    # image files
    for bids_path, file_path in self.file_paths.items():
        assert bids_path is not None
        bids_path = Path(bidsdir) / bids_path
        bids_paths.add(bids_path)
        bids_path.parent.mkdir(parents=True, exist_ok=True)
        if bids_path.is_file():
            continue  # ignore real files
        elif bids_path.is_symlink():
            if bids_path.resolve() == Path(file_path).resolve():
                continue  # nothing to be done
            else:
                bids_path.unlink()  # symlink points to different file
        relative_file_path = relpath(file_path, start=bids_path.parent)
        bids_path.symlink_to(relative_file_path)
    # sidecar files
    for bids_path in self.file_paths.keys():
        metadata = self._metadata.get(bids_path)
        if metadata is not None and len(metadata) > 0:
            basename, _ = split_ext(bids_path)
            sidecar_path = (
                Path(bidsdir) / Path(bids_path).parent / f"{basename}.json"
            )
            bids_paths.add(sidecar_path)
            jsonstr = json.dumps(metadata, indent=4, sort_keys=False)
            if sidecar_path.is_file():
                with open(sidecar_path, "r") as f:
                    if jsonstr == f.read():
                        continue
            with open(sidecar_path, "w") as f:
                f.METHOD_NAME(jsonstr)
    # remove unnecessary files
    files_to_keep = set()
    for bids_path in bids_paths:
        relative_bids_path = relpath(bids_path, start=bidsdir)
        # use relative paths to limit parents to bidsdir
        files_to_keep.add(relative_bids_path)
        files_to_keep.update(map(str, Path(relative_bids_path).parents))
    for file_path in rlistdir(bidsdir):
        relative_file_path = relpath(file_path, start=bidsdir)
        if relative_file_path not in files_to_keep:
            p = Path(file_path)
            if not p.is_dir():
                p.unlink()
            else:
                rmtree(p)