Created
April 29, 2022 21:53
-
-
Save in03/8a2fb36f10a01d876be1c9f3c13cb2df to your computer and use it in GitHub Desktop.
Convert network-share mount points between platforms using path-mapping
This file contains 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
import platform | |
import os | |
def resolve_path_map(rpath, path_map_list, strict=True): | |
target_os = platform.system().lower() | |
rpath = os.path.normpath(rpath) | |
for path_maps in path_map_list: | |
for map in path_maps: | |
for origin_os, origin_map in map.items(): | |
origin_map = os.path.normpath(origin_map) | |
if origin_map in rpath: | |
print(f"Mapping {origin_os.capitalize()} -> {target_os.capitalize()}") | |
for map in path_maps: | |
for k, target_map in map.items(): | |
if k == target_os: | |
print(f"Substituting target OS root path {target_map}") | |
return rpath.replace(origin_map, target_map) | |
if strict: | |
return None | |
return rpath | |
if __name__ == "__main__": | |
# Lists of list of dictionaries | |
# Add as needed | |
path_maps = [ | |
[ | |
{"windows": "Z:\\"}, | |
{"linux": "/mnt/ActiveProject"}, | |
{"darwin": "/volumes/ActiveProjects"}, | |
], | |
[ | |
{"windows": "A:\\"}, | |
{"linux": "/mnt/ArchivedProjects"}, | |
{"darwin": "/volumes/ArchivedProjects"}, | |
], | |
[ | |
{"windows": "R:\\"}, | |
{"linux": "/mnt/ProjectCache"}, | |
{"darwin": "/volumes/ProjectCache"}, | |
], | |
] | |
new_path = resolve_path_map("/mnt/ProjectCache/Project_1", path_map_list=path_maps) | |
print(new_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment