pathlib: add analogues to py.path.local's bestrelpath and common
An equivalent for these py.path.local functions is needed for some upcoming py.path -> pathlib conversions.
This commit is contained in:
@@ -569,3 +569,35 @@ def visit(
|
||||
for entry in entries:
|
||||
if entry.is_dir(follow_symlinks=False) and recurse(entry):
|
||||
yield from visit(entry.path, recurse)
|
||||
|
||||
|
||||
def commonpath(path1: Path, path2: Path) -> Optional[Path]:
|
||||
"""Return the common part shared with the other path, or None if there is
|
||||
no common part."""
|
||||
try:
|
||||
return Path(os.path.commonpath((str(path1), str(path2))))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def bestrelpath(directory: Path, dest: Path) -> str:
|
||||
"""Return a string which is a relative path from directory to dest such
|
||||
that directory/bestrelpath == dest.
|
||||
|
||||
If no such path can be determined, returns dest.
|
||||
"""
|
||||
if dest == directory:
|
||||
return os.curdir
|
||||
# Find the longest common directory.
|
||||
base = commonpath(directory, dest)
|
||||
# Can be the case on Windows.
|
||||
if not base:
|
||||
return str(dest)
|
||||
reldirectory = directory.relative_to(base)
|
||||
reldest = dest.relative_to(base)
|
||||
return os.path.join(
|
||||
# Back from directory to base.
|
||||
*([os.pardir] * len(reldirectory.parts)),
|
||||
# Forward from base to dest.
|
||||
*reldest.parts,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user