Skip to content

Commit

Permalink
Move path_identity to conda.common.path (conda#14068)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Aug 1, 2024
1 parent 589cac4 commit 126b105
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 22 deletions.
25 changes: 13 additions & 12 deletions conda/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
)
from .base.context import ROOT_ENV_NAME, context, locate_prefix_by_name
from .common.compat import FILESYSTEM_ENCODING, on_win
from .common.path import path_identity as _path_identity
from .common.path import paths_equal
from .deprecations import deprecated

Expand Down Expand Up @@ -1001,7 +1002,7 @@ def native_path_to_unix(
if paths is None:
return None
elif not on_win:
return path_identity(paths)
return _path_identity(paths)

# short-circuit if we don't get any paths
paths = paths if isinstance(paths, str) else tuple(paths)
Expand Down Expand Up @@ -1052,7 +1053,7 @@ def unix_path_to_native(
if paths is None:
return None
elif not on_win:
return path_identity(paths)
return _path_identity(paths)

# short-circuit if we don't get any paths
paths = paths if isinstance(paths, str) else tuple(paths)
Expand Down Expand Up @@ -1100,13 +1101,13 @@ def unix_path_to_native(
return tuple(win_path.split(ntpath.pathsep))


def path_identity(paths: str | Iterable[str] | None) -> str | tuple[str, ...] | None:
if paths is None:
return None
elif isinstance(paths, str):
return os.path.normpath(paths)
else:
return tuple(os.path.normpath(path) for path in paths)
deprecated.constant(
"25.3",
"25.9",
"path_identity",
_path_identity,
addendum="Use `conda.common.path.path_identity` instead.",
)


def backslash_to_forwardslash(
Expand Down Expand Up @@ -1230,7 +1231,7 @@ class XonshActivator(_Activator):
pathsep_join = ";".join if on_win else ":".join
sep = "/"
path_conversion = staticmethod(
backslash_to_forwardslash if on_win else path_identity
backslash_to_forwardslash if on_win else _path_identity
)
# 'scripts' really refer to de/activation scripts, not scripts in the language per se
# xonsh can piggy-back activation scripts from other languages depending on the platform
Expand All @@ -1257,7 +1258,7 @@ def _hook_preamble(self) -> str:
class CmdExeActivator(_Activator):
pathsep_join = ";".join
sep = "\\"
path_conversion = staticmethod(path_identity)
path_conversion = staticmethod(_path_identity)
script_extension = ".bat"
tempfile_extension = ".bat"
command_join = "\n"
Expand Down Expand Up @@ -1322,7 +1323,7 @@ def _hook_preamble(self) -> str:
class PowerShellActivator(_Activator):
pathsep_join = ";".join if on_win else ":".join
sep = "\\" if on_win else "/"
path_conversion = staticmethod(path_identity)
path_conversion = staticmethod(_path_identity)
script_extension = ".ps1"
tempfile_extension = None # output to stdout
command_join = "\n"
Expand Down
13 changes: 13 additions & 0 deletions conda/common/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
if TYPE_CHECKING:
from typing import Iterable, Sequence

PathType = str | os.PathLike

log = getLogger(__name__)

PATH_MATCH_REGEX = (
Expand Down Expand Up @@ -402,3 +404,14 @@ def is_package_file(path):
# NOTE: not using CONDA_TARBALL_EXTENSION_V1 or CONDA_TARBALL_EXTENSION_V2 to comply with
# import rules and to avoid a global lookup.
return path[-6:] == ".conda" or path[-8:] == ".tar.bz2"


def path_identity(
paths: PathType | Iterable[PathType] | None,
) -> str | tuple[str, ...] | None:
if paths is None:
return None
elif isinstance(paths, (str, os.PathLike)):
return os.path.normpath(paths)
else:
return tuple(os.path.normpath(path) for path in paths)
23 changes: 14 additions & 9 deletions conda/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
from . import CondaError
from .auxlib.compat import Utf8NamedTemporaryFile, shlex_split_unicode
from .common.compat import isiterable, on_win
from .common.path import path_identity as _path_identity
from .common.path import win_path_to_unix
from .common.url import path_to_url
from .deprecations import deprecated

log = logging.getLogger(__name__)


def path_identity(path):
"""Used as a dummy path converter where no conversion necessary"""
return path
deprecated.constant(
"25.3",
"25.9",
"path_identity",
_path_identity,
addendum="Use `conda.common.path.path_identity` instead.",
)


def unix_path_to_win(path, root_prefix=""):
Expand Down Expand Up @@ -111,8 +116,8 @@ def human_bytes(n):
echo="echo",
env_script_suffix=".sh",
nul="2>/dev/null",
path_from=path_identity,
path_to=path_identity,
path_from=_path_identity,
path_to=_path_identity,
pathsep=":",
printdefaultenv="echo $CONDA_DEFAULT_ENV",
printpath="echo $PATH",
Expand Down Expand Up @@ -168,8 +173,8 @@ def human_bytes(n):
# printdefaultenv='echo $CONDA_DEFAULT_ENV',
# printpath="echo %PATH%",
# exe="powershell.exe",
# path_from=path_identity,
# path_to=path_identity,
# path_from=_path_identity,
# path_to=_path_identity,
# slash_convert = ("/", "\\"),
# ),
"cmd.exe": dict(
Expand All @@ -191,8 +196,8 @@ def human_bytes(n):
printpath="@echo %PATH%",
exe="cmd.exe",
shell_args=["/d", "/c"],
path_from=path_identity,
path_to=path_identity,
path_from=_path_identity,
path_to=_path_identity,
slash_convert=("/", "\\"),
sep="\\",
pathsep=";",
Expand Down
20 changes: 20 additions & 0 deletions news/14068-path-identity
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Enhancements

* <news item>

### Bug fixes

* <news item>

### Deprecations

* Mark `conda.activate.path_identity` as pending deprecation. Use `conda.common.path.path_identity` instead. (#14068)
* Mark `conda.utils.path_identity` as pending deprecation. Use `conda.common.path.path_identity` instead. (#14068)

### Docs

* <news item>

### Other

* <news item>
25 changes: 25 additions & 0 deletions tests/common/test_path.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from logging import getLogger
from pathlib import Path

from conda.common.path import (
get_major_minor_version,
missing_pyc_files,
path_identity,
url_to_path,
win_path_backout,
)
Expand Down Expand Up @@ -168,3 +170,26 @@ def test_get_major_minor_version_no_dot():
assert get_major_minor_version("bin/python3.10", False) == "310"
assert get_major_minor_version("lib/python310/site-packages/", False) == "310"
assert get_major_minor_version("python3", False) is None


def test_path_identity(tmp_path: Path) -> None:
# None
assert path_identity(None) is None

# str | os.PathLike
assert path_identity("") == "."
assert path_identity(".") == "."
assert path_identity("./") == "."
assert path_identity("relative") == "relative"
assert path_identity(str(tmp_path)) == str(tmp_path)
assert path_identity(tmp_path) == str(tmp_path)

# Iterable[str | os.PathLike]
assert path_identity(("", ".", "./", "relative", str(tmp_path), tmp_path)) == (
".",
".",
".",
"relative",
str(tmp_path),
str(tmp_path),
)
15 changes: 14 additions & 1 deletion tests/test_activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import platform
import sys
from contextlib import nullcontext
from functools import lru_cache
from itertools import chain
from logging import getLogger
Expand All @@ -21,7 +22,7 @@

import pytest

from conda import CONDA_PACKAGE_ROOT, CONDA_SOURCE_ROOT, CondaError
from conda import CONDA_PACKAGE_ROOT, CONDA_SOURCE_ROOT, CondaError, activate
from conda import __version__ as conda_version
from conda.activate import (
CmdExeActivator,
Expand Down Expand Up @@ -3505,3 +3506,15 @@ def test_metavars_force_uppercase(
assert "ONE" in export_vars
assert "FOUR" in unset_vars
assert "SIX" in export_vars


@pytest.mark.parametrize(
"function,raises",
[
("path_identity", TypeError),
],
)
def test_deprecations(function: str, raises: type[Exception] | None) -> None:
raises_context = pytest.raises(raises) if raises else nullcontext()
with pytest.deprecated_call(), raises_context:
getattr(activate, function)()
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def get_test_dir() -> Path:
("win_path_to_cygwin", TypeError),
("cygwin_path_to_win", TypeError),
("translate_stream", TypeError),
("path_identity", TypeError),
],
)
def test_deprecations(function: str, raises: type[Exception] | None) -> None:
Expand Down

0 comments on commit 126b105

Please sign in to comment.