Files
dvc/dvc/fs/utils.py
T
Saugat Pachhai (सौगात) 3f5757a0a5 dvc.objects: move hashing functions to dvc.objects
This commit moves `HashedStreamReader`/`get_hash_file`/`file_md5`
into `dvc.objects.hash`.

The `get_hash_file` has been renamed to `hash_file`.
This also copies `relpath` and `is_exec` to the `dvc.fs.utils`.
2022-05-12 20:33:18 +03:00

32 lines
1.1 KiB
Python

import os
import stat
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .base import AnyFSPath
def is_exec(mode: int) -> bool:
return bool(mode & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH))
def relpath(path: "AnyFSPath", start: "AnyFSPath" = os.curdir) -> "AnyFSPath":
path = os.fspath(path)
start = os.path.abspath(os.fspath(start))
# Windows path on different drive than curdir doesn't have relpath
if os.name == "nt":
# Since python 3.8 os.realpath resolves network shares to their UNC
# path. So, to be certain that relative paths correctly captured,
# we need to resolve to UNC path first. We resolve only the drive
# name so that we don't follow any 'real' symlinks on the path
def resolve_network_drive_windows(path_to_resolve):
drive, tail = os.path.splitdrive(path_to_resolve)
return os.path.join(os.path.realpath(drive), tail)
path = resolve_network_drive_windows(os.path.abspath(path))
start = resolve_network_drive_windows(start)
if not os.path.commonprefix([start, path]):
return path
return os.path.relpath(path, start)