File size: 5,665 Bytes
71687cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""CLI implementation for `conda rename`.

Renames an existing environment by cloning it and then removing the original environment.
"""

from __future__ import annotations

import os
from functools import partial
from pathlib import Path
from typing import TYPE_CHECKING

from conda.deprecations import deprecated
from conda.exceptions import CondaEnvException

if TYPE_CHECKING:
    from argparse import ArgumentParser, Namespace, _SubParsersAction


def configure_parser(sub_parsers: _SubParsersAction, **kwargs) -> ArgumentParser:
    from ..auxlib.ish import dals
    from .helpers import add_output_and_prompt_options, add_parser_prefix

    summary = "Rename an existing environment."
    description = dals(
        f"""
        {summary}

        This command renames a conda environment via its name (-n/--name) or
        its prefix (-p/--prefix).

        The base environment and the currently-active environment cannot be renamed.
        """
    )
    epilog = dals(
        """
        Examples::

            conda rename -n test123 test321

            conda rename --name test123 test321

            conda rename -p path/to/test123 test321

            conda rename --prefix path/to/test123 test321

        """
    )

    p = sub_parsers.add_parser(
        "rename",
        help=summary,
        description=description,
        epilog=epilog,
        **kwargs,
    )
    # Add name and prefix args
    add_parser_prefix(p)

    p.add_argument("destination", help="New name for the conda environment.")

    add_output_and_prompt_options(p)

    p.set_defaults(func="conda.cli.main_rename.execute")

    return p


@deprecated("25.9", "26.3", addendum="Use PrefixData.validate_path()")
def check_protected_dirs(prefix: str | Path, json: bool = False) -> None:
    """Ensure that the new prefix does not contain protected directories."""
    from conda.core.prefix_data import PrefixData

    if PrefixData(Path(prefix).parent).is_environment():
        raise CondaEnvException(
            f"The specified prefix '{prefix}' "
            "appears to be a top level directory within an existing conda environment "
            "(i.e., {history_file} exists). Creating an environment in this location "
            "has the potential to irreversibly corrupt your conda installation and/or "
            "other conda environments, please choose a different location for your "
            "new conda environment. Aborting.",
            json,
        )


@deprecated(
    "25.9",
    "26.3",
    addendum="Use PrefixData.validate_path(), PrefixData.validate_name()",
)
def validate_src() -> str:
    """
    Ensure that we are receiving at least one valid value for the environment
    to be renamed and that the "base" environment is not being renamed
    """
    from ..base.context import context
    from .install import validate_prefix_exists

    prefix = Path(context.target_prefix)
    validate_prefix_exists(prefix)

    if prefix.samefile(context.root_prefix):
        raise CondaEnvException("The 'base' environment cannot be renamed")
    if context.active_prefix and prefix.samefile(context.active_prefix):
        raise CondaEnvException("Cannot rename the active environment")
    else:
        check_protected_dirs(prefix)

    return str(prefix)


def execute(args: Namespace, parser: ArgumentParser) -> int:
    """Executes the command for renaming an existing environment."""
    from ..base.constants import DRY_RUN_PREFIX
    from ..base.context import context
    from ..cli import install
    from ..core.prefix_data import PrefixData
    from ..gateways.disk.delete import rm_rf
    from ..gateways.disk.update import rename_context

    # Validate source
    source_prefix_data = PrefixData.from_context()
    source_prefix_data.assert_environment()
    if source_prefix_data.is_base():
        raise CondaEnvException("The 'base' environment cannot be renamed")
    if context.active_prefix and source_prefix_data.prefix_path.samefile(
        context.active_prefix
    ):
        raise CondaEnvException("Cannot rename the active environment")

    if source_prefix_data == PrefixData(context.default_activation_prefix):
        raise CondaEnvException(
            "Cannot rename an environment if it is configured as `default_activation_env`."
        )

    source = str(source_prefix_data.prefix_path)

    # Validate destination
    if os.sep in args.destination:
        dest_prefix_data = PrefixData(args.destination)
        dest_prefix_data.validate_path(expand_path=True)
    else:
        dest_prefix_data = PrefixData.from_name(args.destination)
    destination = str(dest_prefix_data.prefix_path)
    if not args.yes and dest_prefix_data.exists():
        raise CondaEnvException(
            f"The environment '{dest_prefix_data.prefix_path}' already exists. Override with --yes."
        )

    def clone_and_remove() -> None:
        actions: tuple[partial, ...] = (
            partial(
                install.clone,
                source,
                destination,
                quiet=context.quiet,
                json=context.json,
            ),
            partial(rm_rf, source),
        )

        # We now either run collected actions or print dry run statement
        for func in actions:
            if args.dry_run:
                print(f"{DRY_RUN_PREFIX} {func.func.__name__} {','.join(func.args)}")
            else:
                func()

    if args.yes:
        with rename_context(destination, dry_run=args.dry_run):
            clone_and_remove()
    else:
        clone_and_remove()
    return 0