forked from conda/conda-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
215 lines (194 loc) · 6.68 KB
/
Copy pathcli.py
File metadata and controls
215 lines (194 loc) · 6.68 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import annotations
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from .extract import DEFAULT_NUM_PROCESSORS, ExtractType, _NumProcessorsAction
if TYPE_CHECKING:
from argparse import ArgumentParser, Namespace
from collections.abc import Callable
def _add_prefix(parser: ArgumentParser) -> None:
# Prefix must be string or it will break conda's context initializer
parser.add_argument(
"--prefix",
action="store",
type=str,
required=True,
help="path to the conda environment to operate on",
)
def _add_extract(parser: ArgumentParser) -> None:
extract_group = parser.add_mutually_exclusive_group(required=True)
extract_group.add_argument(
"--conda-pkgs",
action="store_const",
const=ExtractType.PACKAGES,
dest="pkg_format",
help="extract conda packages found in prefix/pkgs",
)
extract_group.add_argument(
"--tar-from-stdin",
action="store_const",
const=ExtractType.TAR,
dest="pkg_format",
help="extract tarball from stdin",
)
parser.add_argument(
"--num-processors",
default=DEFAULT_NUM_PROCESSORS,
metavar="N",
action=_NumProcessorsAction,
help="Number of processors to use with --extract-conda-pkgs. "
"Value must be int between 0 (auto) and the number of processors. "
f"Defaults to {DEFAULT_NUM_PROCESSORS}.",
)
def _add_uninstall(parser: ArgumentParser) -> None:
parser.add_argument(
"--remove-caches",
action="store_true",
required=False,
help=(
"Removes the notices cache and runs conda --clean --all to clean package caches"
" outside the installation directory."
" This is especially useful when pkgs_dirs is set in a .condarc file."
" Not recommended with multiple conda installations when softlinks are enabled."
),
)
parser.add_argument(
"--remove-config-files",
choices=["user", "system", "all"],
default=None,
required=False,
help=(
"Removes all .condarc files."
" `user` removes the files inside the current user's"
" home directory and `system` removes all files outside of that directory."
" Not recommended when multiple conda installations are on the system"
" or when running on an environments directory."
),
)
parser.add_argument(
"--remove-user-data",
action="store_true",
required=False,
help=(
"Removes the ~/.conda directory."
" Not recommended when multiple conda installations are on the system"
" or when running on an environments directory."
),
)
def _add_windows_path(parser: ArgumentParser) -> None:
windows_path_group = parser.add_mutually_exclusive_group(required=True)
windows_path_group.add_argument(
"--append",
choices=["user", "system"],
default=None,
help=(
"Appends a prefix to the Windows PATH. "
"If the prefix already exists in PATH, the prefix will be moved "
"to the end of the PATH variable."
),
)
windows_path_group.add_argument(
"--prepend",
choices=["user", "system"],
default=None,
help=(
"Prepends a prefix to the Windows PATH."
"If the prefix already exists in PATH, the prefix will be moved "
"to the beginning of the PATH variable."
),
)
windows_path_group.add_argument(
"--remove",
choices=["user", "system"],
default=None,
help="Removes a prefix from the Windows PATH.",
)
parser.add_argument(
"--condabin",
action="store_true",
help="Adds the condabin directory under PREFIX to PATH.",
)
parser.add_argument(
"--classic",
action="store_true",
help="Adds PREFIX and additional libraries under PREFIX to PATH.",
)
def configure_parser(parser: ArgumentParser) -> None:
subparsers = parser.add_subparsers(
title="subcommand",
description="The following subcommands are available.",
dest="cmd",
required=False,
)
extract_parser = subparsers.add_parser(
"extract",
description="Extracts tarballs or conda packages.",
)
_add_prefix(extract_parser)
_add_extract(extract_parser)
uninstall_parser = subparsers.add_parser(
"uninstall",
description="Uninstalls a conda directory and all environments inside the directory.",
)
_add_prefix(uninstall_parser)
_add_uninstall(uninstall_parser)
if sys.platform != "win32":
return
windows_parser = subparsers.add_parser(
"windows",
description="Helper functions for Windows.",
)
windows_subparsers = windows_parser.add_subparsers(
title="windows subcommand",
dest="windows_cmd",
)
windows_path_parser = windows_subparsers.add_parser(
"path", description="Adds or removes a prefix to `PATH`."
)
_add_prefix(windows_path_parser)
_add_windows_path(windows_path_parser)
def execute(args: Namespace) -> None | int:
action: Callable
kwargs = {}
if args.cmd == "extract":
from .extract import extract
action = extract
kwargs.update(
{
"package_format": args.pkg_format,
"max_workers": args.num_processors,
}
)
elif args.cmd == "uninstall":
from .uninstall import uninstall
action = uninstall
kwargs.update(
{
"remove_caches": args.remove_caches,
"remove_config_files": args.remove_config_files,
"remove_user_data": args.remove_user_data,
}
)
elif args.cmd == "windows" and sys.platform == "win32":
if args.windows_cmd == "path":
from .windows.path import add_remove_path
action = add_remove_path
kwargs.update(
{
"add": args.append or args.prepend,
"condabin": args.condabin,
"classic": args.classic,
"append": args.append is not None,
"remove": args.remove,
}
)
else:
raise NotImplementedError(
f"No action available for windows subcommand '{args.windows_cmd}'."
)
else:
raise NotImplementedError(f"No action available for subcommand '{args.cmd}'.")
return action(
prefix=Path(args.prefix).expanduser().resolve(),
**kwargs,
)