File size: 6,192 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | # Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
"""CLI implementation for `conda init`.
Prepares the user's profile for running conda, and sets up the conda shell interface.
"""
from __future__ import annotations
from argparse import SUPPRESS
from logging import getLogger
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from argparse import ArgumentParser, Namespace, _SubParsersAction
log = getLogger(__name__)
def configure_parser(sub_parsers: _SubParsersAction, **kwargs) -> ArgumentParser:
from ..auxlib.ish import dals
from ..base.constants import COMPATIBLE_SHELLS
from ..common.compat import on_win
from ..common.constants import NULL
from .helpers import add_parser_json
summary = "Initialize conda for shell interaction."
description = summary
epilog = dals(
"""
Key parts of conda's functionality require that it interact directly with the shell
within which conda is being invoked. The `conda activate` and `conda deactivate` commands
specifically are shell-level commands. That is, they affect the state (e.g. environment
variables) of the shell context being interacted with. Other core commands, like
`conda create` and `conda install`, also necessarily interact with the shell environment.
They're therefore implemented in ways specific to each shell. Each shell must be configured
to make use of them.
The --condabin option adds the "$CONDA_PREFIX/condabin" directory to the PATH environment variable.
This directory only contains the conda executable and does not contain other executables from other
packages installed in the base environment. On most shells, a small snippet is added to the
shell profile. For CMD, the PATH environment variable is modified directly in the registry.
This command makes changes to your system that are specific and customized for each shell.
To see the specific files and locations on your system that will be affected before, use
the '--dry-run' flag. To see the exact changes that are being or will be made to each
location, use the '--verbose' flag.
IMPORTANT: After running `conda init`, most shells will need to be closed and restarted for
changes to take effect.
"""
)
p = sub_parsers.add_parser(
"init",
help=summary,
description=description,
epilog=epilog,
**kwargs,
)
p.add_argument(
"--dev",
action="store_true",
help=SUPPRESS,
default=NULL,
)
p.add_argument(
"--all",
action="store_true",
help="Initialize all currently available shells.",
default=NULL,
)
setup_type_group = p.add_argument_group("setup type")
setup_type_group.add_argument(
"--install",
action="store_true",
help=SUPPRESS,
default=NULL,
)
setup_type_group.add_argument(
"--condabin",
action="store_true",
help="Add 'condabin/' directory to PATH only. Does not install the shell function.",
default=NULL,
)
setup_type_group.add_argument(
"--user",
action="store_true",
dest="user",
help="Initialize conda for the current user (default).",
default=True,
)
setup_type_group.add_argument(
"--no-user",
action="store_false",
dest="user",
help="Don't initialize conda for the current user.",
)
setup_type_group.add_argument(
"--system",
action="store_true",
help="Initialize conda for all users on the system.",
default=NULL,
)
setup_type_group.add_argument(
"--reverse",
action="store_true",
help="Undo effects of last conda init.",
default=NULL,
)
p.add_argument(
"shells",
nargs="*",
choices=COMPATIBLE_SHELLS,
metavar="SHELLS",
help=(
"One or more shells to be initialized. If not given, the default value is 'bash' on "
"unix and 'cmd.exe' & 'powershell' on Windows. Use the '--all' flag to initialize all "
f"shells. Available shells: {sorted(COMPATIBLE_SHELLS)}"
),
default=["cmd.exe", "powershell"] if on_win else ["bash"],
)
if on_win:
p.add_argument(
"--anaconda-prompt",
action="store_true",
help="Add an 'Anaconda Prompt' icon to your desktop.",
default=NULL,
)
add_parser_json(p)
p.add_argument(
"-d",
"--dry-run",
action="store_true",
help="Only display what would have been done.",
)
p.set_defaults(func="conda.cli.main_init.execute")
return p
def execute(args: Namespace, parser: ArgumentParser) -> int:
from ..base.constants import COMPATIBLE_SHELLS
from ..base.context import context
from ..common.compat import on_win
from ..core.initialize import (
add_condabin_to_path,
initialize,
initialize_dev,
install,
)
from ..exceptions import ArgumentError
if args.install:
return install(context.conda_prefix)
selected_shells: tuple[str, ...]
if args.all:
selected_shells = COMPATIBLE_SHELLS
else:
selected_shells = tuple(args.shells)
if args.dev:
if len(selected_shells) != 1:
raise ArgumentError("--dev can only handle one shell at a time right now")
return initialize_dev(selected_shells[0])
elif args.condabin:
for_user = args.user and not args.system
anaconda_prompt = on_win and args.anaconda_prompt
return add_condabin_to_path(
context.conda_prefix, selected_shells, for_user, args.system, args.reverse
)
else:
for_user = args.user and not args.system
anaconda_prompt = on_win and args.anaconda_prompt
return initialize(
context.conda_prefix,
selected_shells,
for_user,
args.system,
anaconda_prompt,
args.reverse,
)
|