Return a string which is a relative path from directory to dest such that directory/bestrelpath == dest. The paths must be either both absolute or both relative. If no such path can be determined, returns dest.
(directory: Path, dest: Path)
| 1008 | |
| 1009 | |
| 1010 | def bestrelpath(directory: Path, dest: Path) -> str: |
| 1011 | """Return a string which is a relative path from directory to dest such |
| 1012 | that directory/bestrelpath == dest. |
| 1013 | |
| 1014 | The paths must be either both absolute or both relative. |
| 1015 | |
| 1016 | If no such path can be determined, returns dest. |
| 1017 | """ |
| 1018 | assert isinstance(directory, Path) |
| 1019 | assert isinstance(dest, Path) |
| 1020 | if dest == directory: |
| 1021 | return os.curdir |
| 1022 | # Find the longest common directory. |
| 1023 | base = commonpath(directory, dest) |
| 1024 | # Can be the case on Windows for two absolute paths on different drives. |
| 1025 | # Can be the case for two relative paths without common prefix. |
| 1026 | # Can be the case for a relative path and an absolute path. |
| 1027 | if not base: |
| 1028 | return str(dest) |
| 1029 | reldirectory = directory.relative_to(base) |
| 1030 | reldest = dest.relative_to(base) |
| 1031 | return os.path.join( |
| 1032 | # Back from directory to base. |
| 1033 | *([os.pardir] * len(reldirectory.parts)), |
| 1034 | # Forward from base to dest. |
| 1035 | *reldest.parts, |
| 1036 | ) |
| 1037 | |
| 1038 | |
| 1039 | def safe_exists(p: Path) -> bool: |