-
Notifications
You must be signed in to change notification settings - Fork 13
/
_cli.py
158 lines (138 loc) · 5.22 KB
/
_cli.py
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
import argparse
import os
import warnings
from ._config import Output, load_config_from_file
from ._constants import default_dependency_file_path
from ._rapids_dependency_file_generator import (
delete_existing_files,
make_dependency_files,
)
from ._version import __version__ as version
def validate_args(argv):
parser = argparse.ArgumentParser(
description=f"Generates dependency files for RAPIDS libraries (version: {version})."
)
parser.add_argument(
"-c",
"--config",
default=default_dependency_file_path,
help="Path to YAML config file.",
)
parser.add_argument(
"--clean",
nargs="?",
default=None,
const="",
help=(
"Delete any files previously created by dfg before running. An optional "
"path to clean may be provided, otherwise the current working directory "
"is used as the root from which to clean."
),
)
codependent_args = parser.add_argument_group("optional, but codependent")
codependent_args.add_argument(
"--file-key",
help="The file key from `dependencies.yaml` to generate.",
)
codependent_args.add_argument(
"--file_key",
dest="file_key_deprecated",
help="The file key from `dependencies.yaml` to generate. DEPRECATED: Use --file-key instead.",
)
codependent_args.add_argument(
"--output",
help="The output file type to generate.",
choices=[
x.value
for x in [
Output.CONDA,
Output.PYPROJECT,
Output.REQUIREMENTS,
]
],
)
codependent_args.add_argument(
"--matrix",
help=(
"String representing which matrix combination should be generated, "
'such as `--matrix "cuda=11.5;arch=x86_64"`. May also be an empty string.'
),
)
parser.add_argument(
"--prepend-channel",
action="append",
default=[],
dest="prepend_channels",
help=(
"A string representing a conda channel to prepend to the list of "
"channels. This option is only valid with --output "
f"{Output.CONDA.value} or no --output. May be specified multiple times."
),
)
parser.add_argument(
"--prepend-channels",
dest="prepend_channels_deprecated",
help=(
"A string representing a list of conda channels to prepend to the list of "
"channels. Channels should be separated by a semicolon, such as "
'`--prepend-channels "my_channel;my_other_channel"`. This option is '
f"only valid with --output {Output.CONDA.value} or no --output. "
"DEPRECATED: Use --prepend-channel instead."
),
)
args = parser.parse_args(argv)
if args.file_key_deprecated:
if args.file_key:
raise ValueError("The --file_key (deprecated) and --file-key arguments cannot be specified together.")
warnings.warn("The use of --file_key is deprecated. Use -f or --file-key instead.")
args.file_key = args.file_key_deprecated
dependent_arg_keys = ["file_key", "output", "matrix"]
dependent_arg_values = [getattr(args, key) is None for key in dependent_arg_keys]
if any(dependent_arg_values) and not all(dependent_arg_values):
raise ValueError(
"The following arguments must be used together:"
+ "".join([f"\n {x}" for x in ["--file-key", "--output", "--matrix"]])
)
if args.prepend_channels_deprecated:
if args.prepend_channels:
raise ValueError(
"The --prepend-channels (deprecated) and --prepend-channel arguments cannot be specified together."
)
warnings.warn("The use of --prepend-channels is deprecated. Use --prepend-channel instead.")
args.prepend_channels = args.prepend_channels_deprecated.split(";")
if args.prepend_channels and args.output and args.output != Output.CONDA.value:
raise ValueError(f"--prepend-channel is only valid with --output {Output.CONDA.value}")
# If --clean was passed without arguments, default to cleaning from the root of the
# tree where the config file is.
if args.clean == "":
args.clean = os.path.dirname(os.path.abspath(args.config))
return args
def generate_matrix(matrix_arg):
if not matrix_arg:
return None
matrix = {}
for matrix_column in matrix_arg.split(";"):
key, val = matrix_column.split("=")
matrix[key] = [val]
return matrix
def main(argv=None):
args = validate_args(argv)
parsed_config = load_config_from_file(args.config)
matrix = generate_matrix(args.matrix)
to_stdout = all([args.file_key, args.output, args.matrix is not None])
if to_stdout:
file_keys = [args.file_key]
output = {Output(args.output)}
else:
file_keys = list(parsed_config.files.keys())
output = None
if args.clean:
delete_existing_files(args.clean)
make_dependency_files(
parsed_config=parsed_config,
file_keys=file_keys,
output=output,
matrix=matrix,
prepend_channels=args.prepend_channels,
to_stdout=to_stdout,
)