Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert network-share mount points between platforms using path-mapping
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