Created
July 4, 2024 00:21
-
-
Save pranavmishra90/4f07306d5168e169db3a2550c59cfb04 to your computer and use it in GitHub Desktop.
Datalad - Find the current and superdataset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get Datalad Repository Paths | |
| def datalad_dataset_paths(start_path=None, max_levels=3): | |
| import os | |
| if start_path is None: | |
| start_path = os.getcwd() | |
| def search_upwards(path, levels): | |
| current_path = path | |
| for _ in range(levels): | |
| git_path = os.path.join(current_path, '.git') | |
| datalad_config_path = os.path.join(current_path, '.datalad', 'config') | |
| if os.path.isdir(git_path) and os.path.isfile(datalad_config_path): | |
| return current_path | |
| current_path = os.path.dirname(current_path) | |
| return None | |
| current_dl_dataset = None | |
| dl_superdataset = None | |
| while start_path != os.path.dirname(start_path): | |
| temp_result = search_upwards(start_path, max_levels) | |
| if temp_result: | |
| if current_dl_dataset is None: | |
| current_dl_dataset = temp_result | |
| dl_superdataset = temp_result | |
| start_path = os.path.dirname(temp_result) | |
| else: | |
| break | |
| return current_dl_dataset, dl_superdataset | |
| def chdir_dl_dataset_root(level='current', verbosity=False): | |
| import os | |
| os.environ['current_dl_dataset'], os.environ['dl_superdataset'] = datalad_dataset_paths(start_path=None, max_levels=5) | |
| if level == ('current'): | |
| os.chdir(os.environ['current_dl_dataset']) | |
| elif level == "super": | |
| os.chdir(os.environ['dl_superdataset']) | |
| else: | |
| print("Error: incorrect level specified") | |
| os.environ["repo_root"] = os.getcwd() | |
| if verbosity==True: | |
| print(f"Changed directory to the Datalad dataset root: {os.getcwd()}") | |
| chdir_dl_dataset_root(level='super', verbosity=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment