| |
| |
| from __future__ import annotations |
|
|
| import ntpath |
| import os |
| import posixpath |
| import re |
| from functools import partial |
| from typing import TYPE_CHECKING |
|
|
| if TYPE_CHECKING: |
| from . import PathType |
|
|
|
|
| def nt_to_posix(path: PathType, prefix: PathType | None, cygdrive: bool = False) -> str: |
| """ |
| A fallback implementation of `cygpath --unix`. |
| |
| Args: |
| path: The path to convert. |
| prefix: The Windows style prefix directory to use for the conversion. |
| If not provided, no checks for root paths will be made. |
| cygdrive: Whether to use the Cygwin-style drive prefix. |
| """ |
| path = os.fspath(path) |
| prefix = os.fspath(prefix) if prefix else None |
|
|
| if ntpath.pathsep in path: |
| return posixpath.pathsep.join( |
| converted |
| for path in path.split(ntpath.pathsep) |
| if (converted := nt_to_posix(path, prefix, cygdrive)) |
| ) |
|
|
| |
| if not path: |
| return path |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| subs = 0 |
|
|
| |
| |
| if ntpath.isabs(path) or path in ("C:", "c:"): |
| |
| if prefix: |
| |
| norm_path = ntpath.normpath(path) |
| |
| if path[-1] in "/\\": |
| norm_path += ntpath.sep |
| |
| norm_path, subs = _get_RE_WIN_ROOT(prefix).subn(_to_unix_root, norm_path) |
| |
| if subs: |
| path = norm_path |
|
|
| |
| if not subs: |
| path, subs = RE_WIN_MOUNT.subn(_to_unix_mount, path) |
|
|
| |
| if not subs: |
| path = RE_WIN_DRIVE.sub(partial(_to_unix_drive, cygdrive=cygdrive), path) |
|
|
| return _resolve_path(path, posixpath.sep) |
|
|
|
|
| def _get_root(prefix: str) -> str: |
| |
| prefix = ntpath.normpath(prefix) |
|
|
| |
| |
| |
| |
| |
| |
| |
| return ntpath.join(prefix, "Library") |
|
|
|
|
| def _get_RE_WIN_ROOT(prefix: str) -> re.Pattern: |
| root = _get_root(prefix) |
| return re.compile( |
| rf""" |
| ^ |
| {re.escape(root)} |
| (?P<path>.*)? |
| $ |
| """, |
| flags=re.VERBOSE, |
| ) |
|
|
|
|
| def _to_unix_root(match: re.Match) -> str: |
| return match.group("path") or "/" |
|
|
|
|
| RE_WIN_MOUNT = re.compile( |
| r""" |
| ^ |
| [/\\]{2}( |
| (?P<mount>[^/\\]+) |
| (?P<path>.*)? |
| )? |
| $ |
| """, |
| flags=re.VERBOSE, |
| ) |
|
|
|
|
| def _to_unix_mount(match: re.Match) -> str: |
| mount = match.group("mount") or "" |
| path = match.group("path") or "" |
| return f"//{mount}{path}" |
|
|
|
|
| RE_WIN_DRIVE = re.compile( |
| r""" |
| ^ |
| (?P<drive>[A-Za-z]): |
| (?P<path>[/\\]+.*)? |
| $ |
| """, |
| flags=re.VERBOSE, |
| ) |
|
|
|
|
| def _to_unix_drive(match: re.Match, cygdrive: bool) -> str: |
| drive = match.group("drive").lower() |
| path = match.group("path") or "" |
| return f"{'/cygdrive' if cygdrive else ''}/{drive}{path}" |
|
|
|
|
| def posix_to_nt(path: PathType, prefix: PathType | None, cygdrive: bool = False) -> str: |
| """ |
| A fallback implementation of `cygpath --windows`. |
| |
| Args: |
| path: The path to convert. |
| prefix: The Windows style prefix directory to use for the conversion. |
| If not provided, no checks for root paths will be made. |
| cygdrive: Unused. Present to keep the signature consistent with `nt_to_posix`. |
| """ |
| path = os.fspath(path) |
| prefix = os.fspath(prefix) if prefix else None |
|
|
| if posixpath.pathsep in path: |
| return ntpath.pathsep.join( |
| posix_to_nt(path, prefix) for path in path.split(posixpath.pathsep) |
| ) |
|
|
| |
| if not path: |
| return "." |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| if posixpath.isabs(path): |
| |
| subs = 0 |
|
|
| |
| path, subs = RE_UNIX_DRIVE.subn(_to_win_drive, path) |
|
|
| |
| if not subs: |
| path, subs = RE_UNIX_MOUNT.subn(_to_win_mount, path) |
|
|
| |
| if prefix and not subs: |
| root = _get_root(prefix) |
| path = RE_UNIX_ROOT.sub(partial(_to_win_root, root=root), path) |
|
|
| return _resolve_path(path, ntpath.sep) |
|
|
|
|
| RE_UNIX_DRIVE = re.compile( |
| r""" |
| ^ |
| (/cygdrive)? |
| /(?P<drive>[A-Za-z]) |
| (/+(?P<path>.*)?)? |
| $ |
| """, |
| flags=re.VERBOSE, |
| ) |
|
|
|
|
| def _to_win_drive(match: re.Match) -> str: |
| drive = match.group("drive").upper() |
| path = match.group("path") or "" |
| return f"{drive}:\\{path}" |
|
|
|
|
| RE_UNIX_MOUNT = re.compile( |
| r""" |
| ^ |
| /{2}( |
| (?P<mount>[^/]+) |
| (?P<path>/+.*)? |
| )? |
| $ |
| """, |
| flags=re.VERBOSE, |
| ) |
|
|
|
|
| def _to_win_mount(match: re.Match) -> str: |
| mount = match.group("mount") or "" |
| path = match.group("path") or "" |
| return f"\\\\{mount}{path}" |
|
|
|
|
| RE_UNIX_ROOT = re.compile( |
| r""" |
| ^ |
| (?P<path>/.*) |
| $ |
| """, |
| flags=re.VERBOSE, |
| ) |
|
|
|
|
| def _to_win_root(match: re.Match, root: str) -> str: |
| path = match.group("path") |
| return f"{root}{path}" |
|
|
|
|
| def _resolve_path(path: str, sep: str) -> str: |
| leading = "" |
| if match := re.match(r"^([/\\]+)(.*)$", path): |
| leading, path = match.groups() |
| sep = re.escape(sep) |
| return re.sub(r"[/\\]", sep, leading) + re.sub(r"[/\\]+", sep, path) |
|
|
|
|
| def resolve_paths(paths: str, pathsep: str, sep: str) -> str: |
| return pathsep.join(_resolve_path(path, sep) for path in paths.split(pathsep)) |
|
|