slurm-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- slurm_cli/__init__.py +17 -0
- slurm_cli/cli.py +5113 -0
- slurm_cli/py.typed +0 -0
- slurm_cli/utils/__init__.py +1 -0
- slurm_cli/utils/accounts.py +544 -0
- slurm_cli/utils/associations.py +871 -0
- slurm_cli/utils/autocomplete_helpers.py +266 -0
- slurm_cli/utils/base_resource.py +349 -0
- slurm_cli/utils/config.py +88 -0
- slurm_cli/utils/coordinators.py +426 -0
- slurm_cli/utils/events.py +695 -0
- slurm_cli/utils/job_filter.py +551 -0
- slurm_cli/utils/jobs.py +854 -0
- slurm_cli/utils/node_filter.py +778 -0
- slurm_cli/utils/nodes.py +774 -0
- slurm_cli/utils/partitions.py +837 -0
- slurm_cli/utils/prefix_utils.py +1050 -0
- slurm_cli/utils/profiles.py +1360 -0
- slurm_cli/utils/qos.py +883 -0
- slurm_cli/utils/reservations.py +665 -0
- slurm_cli/utils/resources.py +341 -0
- slurm_cli/utils/slurm_config.py +73 -0
- slurm_cli/utils/users.py +649 -0
- slurm_cli/utils/utils.py +21 -0
- slurm_cli-1.0.0.dist-info/METADATA +216 -0
- slurm_cli-1.0.0.dist-info/RECORD +29 -0
- slurm_cli-1.0.0.dist-info/WHEEL +4 -0
- slurm_cli-1.0.0.dist-info/entry_points.txt +3 -0
- slurm_cli-1.0.0.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"""Common bash autocomplete helper functions for slurm-cli.
|
|
2
|
+
|
|
3
|
+
This module provides reusable bash functions that are shared across
|
|
4
|
+
all resource-specific autocomplete generators.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .resources import Resource
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_common_autocomplete_functions() -> str:
|
|
11
|
+
"""Generate common bash functions for autocomplete.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
Bash script string with helper functions.
|
|
15
|
+
"""
|
|
16
|
+
return f"""
|
|
17
|
+
# ==============================================================================
|
|
18
|
+
# Common autocomplete helper functions
|
|
19
|
+
# ==============================================================================
|
|
20
|
+
|
|
21
|
+
# Cache timeout in seconds
|
|
22
|
+
_SLURM_CACHE_TIMEOUT={Resource.CACHE_TIMEOUT}
|
|
23
|
+
|
|
24
|
+
# Check if cache update is disabled via environment variable
|
|
25
|
+
# Returns 0 if updates are disabled, 1 otherwise
|
|
26
|
+
_slurm_cache_updates_disabled() {{
|
|
27
|
+
local val="${{SLURM_CLI_NO_CACHE_UPDATE:-}}"
|
|
28
|
+
val="${{val,,}}" # lowercase
|
|
29
|
+
[[ "$val" == "1" || "$val" == "y" || "$val" == "yes" || "$val" == "true" ]]
|
|
30
|
+
}}
|
|
31
|
+
|
|
32
|
+
# Ensure cache file exists and is fresh, update if needed
|
|
33
|
+
# Usage: _slurm_ensure_cache "/tmp/slurm_cli_nodes.json" "nodes"
|
|
34
|
+
_slurm_ensure_cache() {{
|
|
35
|
+
local file="$1"
|
|
36
|
+
local resource="$2"
|
|
37
|
+
|
|
38
|
+
# Skip if updates are disabled
|
|
39
|
+
_slurm_cache_updates_disabled && return
|
|
40
|
+
|
|
41
|
+
local needs_update=0
|
|
42
|
+
|
|
43
|
+
if [[ ! -f "$file" ]]; then
|
|
44
|
+
needs_update=1
|
|
45
|
+
else
|
|
46
|
+
# Check if file is older than timeout
|
|
47
|
+
local now=$(date +%s)
|
|
48
|
+
local mtime=$(stat -c %Y "$file" 2>/dev/null || echo 0)
|
|
49
|
+
local age=$((now - mtime))
|
|
50
|
+
if [[ $age -gt $_SLURM_CACHE_TIMEOUT ]]; then
|
|
51
|
+
needs_update=1
|
|
52
|
+
fi
|
|
53
|
+
fi
|
|
54
|
+
|
|
55
|
+
if [[ $needs_update -eq 1 ]]; then
|
|
56
|
+
# Update cache silently in foreground (need results immediately)
|
|
57
|
+
# Try the invoked binary first (handles ./slurm-cli), then fall back to PATH
|
|
58
|
+
local _cli="${{COMP_WORDS[0]:-slurm-cli}}"
|
|
59
|
+
"$_cli" show "$resource" --style=json >/dev/null 2>&1 || \
|
|
60
|
+
slurm-cli show "$resource" --style=json >/dev/null 2>&1
|
|
61
|
+
fi
|
|
62
|
+
}}
|
|
63
|
+
|
|
64
|
+
# Complete with compgen, handling empty current word
|
|
65
|
+
# Usage: _slurm_complete "word1 word2 word3" "$cur"
|
|
66
|
+
_slurm_complete() {{
|
|
67
|
+
local words="$1"
|
|
68
|
+
local cur="$2"
|
|
69
|
+
if [[ -z "$cur" ]]; then
|
|
70
|
+
COMPREPLY=($(compgen -W "$words"))
|
|
71
|
+
else
|
|
72
|
+
COMPREPLY=($(compgen -W "$words" -- "$cur"))
|
|
73
|
+
fi
|
|
74
|
+
}}
|
|
75
|
+
|
|
76
|
+
# Get names from a JSON cache file
|
|
77
|
+
# Usage: _slurm_cache_get "/tmp/file.json" ".field[].name" -> "name1 name2 name3"
|
|
78
|
+
_slurm_cache_get() {{
|
|
79
|
+
local file="$1"
|
|
80
|
+
local jq_expr="$2"
|
|
81
|
+
if [ -f "$file" ]; then
|
|
82
|
+
jq -r "$jq_expr" "$file" 2>/dev/null | tr '\\n' ' '
|
|
83
|
+
fi
|
|
84
|
+
}}
|
|
85
|
+
|
|
86
|
+
# Parse key=value format and set _key and _val variables
|
|
87
|
+
# Usage: _slurm_parse_keyval "$cur" "$prev"
|
|
88
|
+
# After call: $_key = key name (lowercase), $_val = value part
|
|
89
|
+
# Returns: 0 if key=value found, 1 otherwise
|
|
90
|
+
_slurm_parse_keyval() {{
|
|
91
|
+
local cur="$1"
|
|
92
|
+
local prev="$2"
|
|
93
|
+
_key=""
|
|
94
|
+
_val=""
|
|
95
|
+
|
|
96
|
+
if [[ $cur == = ]]; then
|
|
97
|
+
# cur is just "=", key is previous word
|
|
98
|
+
_key="${{prev,,}}"
|
|
99
|
+
_val=""
|
|
100
|
+
return 0
|
|
101
|
+
elif [[ $cur == *=* ]]; then
|
|
102
|
+
# cur contains "=" (e.g., "key=val" or "key=")
|
|
103
|
+
_key="${{cur%%=*}}"
|
|
104
|
+
_key="${{_key,,}}"
|
|
105
|
+
_val="${{cur#*=}}"
|
|
106
|
+
return 0
|
|
107
|
+
elif [[ $prev == = ]]; then
|
|
108
|
+
# prev is "=", key is two words back
|
|
109
|
+
_key="${{COMP_WORDS[COMP_CWORD-2]}}"
|
|
110
|
+
_key="${{_key,,}}"
|
|
111
|
+
_val="$cur"
|
|
112
|
+
return 0
|
|
113
|
+
fi
|
|
114
|
+
return 1
|
|
115
|
+
}}
|
|
116
|
+
|
|
117
|
+
# Parse key=value, key+=value, or key-=value format
|
|
118
|
+
# Usage: _slurm_parse_keyval_ext "$cur" "$prev"
|
|
119
|
+
# After call: $_key = key name (lowercase), $_val = value part, $_op = operator (=, +=, -=)
|
|
120
|
+
# Returns: 0 if found, 1 otherwise
|
|
121
|
+
_slurm_parse_keyval_ext() {{
|
|
122
|
+
local cur="$1"
|
|
123
|
+
local prev="$2"
|
|
124
|
+
_key=""
|
|
125
|
+
_val=""
|
|
126
|
+
_op=""
|
|
127
|
+
|
|
128
|
+
# Check cur for key+=val, key-=val, or key=val
|
|
129
|
+
if [[ $cur == *[-+]=* ]] || [[ $cur == *=* ]]; then
|
|
130
|
+
_key="${{cur%%[-+]*=*}}"
|
|
131
|
+
_key="${{_key%%=*}}"
|
|
132
|
+
_key="${{_key,,}}"
|
|
133
|
+
_val="${{cur#*=}}"
|
|
134
|
+
[[ $cur == *+=* ]] && _op="+=" || {{ [[ $cur == *-=* ]] && _op="-=" || _op="="; }}
|
|
135
|
+
return 0
|
|
136
|
+
# Check if prev is =, +=, or -=
|
|
137
|
+
elif [[ $prev == "=" ]] || [[ $prev == "+=" ]] || [[ $prev == "-=" ]]; then
|
|
138
|
+
_key="${{COMP_WORDS[COMP_CWORD-2]}}"
|
|
139
|
+
_key="${{_key,,}}"
|
|
140
|
+
_val="$cur"
|
|
141
|
+
_op="$prev"
|
|
142
|
+
return 0
|
|
143
|
+
# Check if prev ends with operator
|
|
144
|
+
elif [[ $prev == *[-+]= ]] || [[ $prev == *= ]]; then
|
|
145
|
+
_key="${{prev%%[-+]*=}}"
|
|
146
|
+
_key="${{_key%%=}}"
|
|
147
|
+
_key="${{_key,,}}"
|
|
148
|
+
_val="$cur"
|
|
149
|
+
_op="${{prev##*[!-+=]}}"
|
|
150
|
+
return 0
|
|
151
|
+
fi
|
|
152
|
+
return 1
|
|
153
|
+
}}
|
|
154
|
+
|
|
155
|
+
# Complete value and add key= (or key+= / key-=) prefix back if needed
|
|
156
|
+
# Usage: _slurm_complete_value "word1 word2" "$_key" "$_val" "$cur"
|
|
157
|
+
# If cur contains =, prefix is added back to completions
|
|
158
|
+
# If prev is = (bash split on =), do NOT add prefix (it's already typed)
|
|
159
|
+
# Uses $_op if set by _slurm_parse_keyval_ext (defaults to "=")
|
|
160
|
+
_slurm_complete_value() {{
|
|
161
|
+
local words="$1"
|
|
162
|
+
local key="$2"
|
|
163
|
+
local val="$3"
|
|
164
|
+
local cur="$4"
|
|
165
|
+
local op="${{_op:-=}}"
|
|
166
|
+
|
|
167
|
+
COMPREPLY=($(compgen -W "$words" -- "$val"))
|
|
168
|
+
|
|
169
|
+
# Only add key+op prefix when cur contains = (bash didn't split)
|
|
170
|
+
# If bash split on =, the key= is already on command line
|
|
171
|
+
if [[ $cur == *=* && ${{#COMPREPLY[@]}} -gt 0 ]]; then
|
|
172
|
+
COMPREPLY=("${{COMPREPLY[@]/#/$key$op}}")
|
|
173
|
+
fi
|
|
174
|
+
}}
|
|
175
|
+
|
|
176
|
+
# Cache accessor functions for common resources
|
|
177
|
+
# Each function ensures cache is fresh before reading
|
|
178
|
+
_slurm_cache_accounts() {{
|
|
179
|
+
_slurm_ensure_cache "/tmp/slurm_cli_accounts.json" "accounts"
|
|
180
|
+
_slurm_cache_get "/tmp/slurm_cli_accounts.json" '.accounts[].name'
|
|
181
|
+
}}
|
|
182
|
+
|
|
183
|
+
_slurm_cache_qos() {{
|
|
184
|
+
_slurm_ensure_cache "/tmp/slurm_cli_qos.json" "qos"
|
|
185
|
+
_slurm_cache_get "/tmp/slurm_cli_qos.json" '.qos[].name'
|
|
186
|
+
}}
|
|
187
|
+
|
|
188
|
+
_slurm_cache_partitions() {{
|
|
189
|
+
_slurm_ensure_cache "/tmp/slurm_cli_partitions.json" "partitions"
|
|
190
|
+
_slurm_cache_get "/tmp/slurm_cli_partitions.json" 'keys[]'
|
|
191
|
+
}}
|
|
192
|
+
|
|
193
|
+
_slurm_cache_nodes() {{
|
|
194
|
+
_slurm_ensure_cache "/tmp/slurm_cli_nodes.json" "nodes"
|
|
195
|
+
_slurm_cache_get "/tmp/slurm_cli_nodes.json" 'keys[]'
|
|
196
|
+
}}
|
|
197
|
+
|
|
198
|
+
_slurm_cache_users() {{
|
|
199
|
+
_slurm_ensure_cache "/tmp/slurm_cli_users.json" "users"
|
|
200
|
+
_slurm_cache_get "/tmp/slurm_cli_users.json" '.users[].name'
|
|
201
|
+
}}
|
|
202
|
+
|
|
203
|
+
_slurm_cache_reservations() {{
|
|
204
|
+
_slurm_ensure_cache "/tmp/slurm_cli_reservations.json" "reservations"
|
|
205
|
+
_slurm_cache_get "/tmp/slurm_cli_reservations.json" 'keys[]'
|
|
206
|
+
}}
|
|
207
|
+
|
|
208
|
+
_slurm_cache_jobs() {{
|
|
209
|
+
_slurm_ensure_cache "/tmp/slurm_cli_jobs.json" "jobs"
|
|
210
|
+
_slurm_cache_get "/tmp/slurm_cli_jobs.json" '.jobs[].job_id'
|
|
211
|
+
}}
|
|
212
|
+
|
|
213
|
+
# Node filter prefixes for nodes= parameter
|
|
214
|
+
# Supports: ALL, partition=, state=, user=, reservation=
|
|
215
|
+
_slurm_node_filter_prefixes="ALL partition= state= user= reservation= drain="
|
|
216
|
+
|
|
217
|
+
# Complete nodes= value with either direct nodes or filter prefixes
|
|
218
|
+
# Usage: _slurm_complete_nodes_value "$_val" "$cur" ["$_key"]
|
|
219
|
+
# The prefix for completions is derived from $cur to preserve case
|
|
220
|
+
_slurm_complete_nodes_value() {{
|
|
221
|
+
local val="$1"
|
|
222
|
+
local cur="$2"
|
|
223
|
+
local key="${{3:-nodes}}"
|
|
224
|
+
local cached_nodes="$(_slurm_cache_nodes)"
|
|
225
|
+
|
|
226
|
+
# Derive prefix from cur by removing val from the end (preserves original case)
|
|
227
|
+
# Note: When bash splits on '=', cur is empty and we should NOT add prefix
|
|
228
|
+
# because bash already has the key= part typed
|
|
229
|
+
local prefix=""
|
|
230
|
+
if [[ "$cur" == *=* && -n "$val" ]]; then
|
|
231
|
+
prefix="${{cur%"$val"}}"
|
|
232
|
+
elif [[ "$cur" == *=* && "$cur" != "=" ]]; then
|
|
233
|
+
prefix="$cur"
|
|
234
|
+
fi
|
|
235
|
+
|
|
236
|
+
# Check if value starts with a filter prefix
|
|
237
|
+
if [[ "$val" == partition=* ]]; then
|
|
238
|
+
local filter_val="${{val#partition=}}"
|
|
239
|
+
local partitions="$(_slurm_cache_partitions)"
|
|
240
|
+
COMPREPLY=($(compgen -W "$partitions" -- "$filter_val"))
|
|
241
|
+
[[ -n "$prefix" && ${{#COMPREPLY[@]}} -gt 0 ]] && COMPREPLY=("${{COMPREPLY[@]/#/${{prefix}}partition=}}")
|
|
242
|
+
elif [[ "$val" == state=* ]]; then
|
|
243
|
+
local filter_val="${{val#state=}}"
|
|
244
|
+
local states="idle alloc drain down mixed comp"
|
|
245
|
+
COMPREPLY=($(compgen -W "$states" -- "$filter_val"))
|
|
246
|
+
[[ -n "$prefix" && ${{#COMPREPLY[@]}} -gt 0 ]] && COMPREPLY=("${{COMPREPLY[@]/#/${{prefix}}state=}}")
|
|
247
|
+
elif [[ "$val" == user=* ]]; then
|
|
248
|
+
local filter_val="${{val#user=}}"
|
|
249
|
+
local users="$(_slurm_cache_users)"
|
|
250
|
+
COMPREPLY=($(compgen -W "$users" -- "$filter_val"))
|
|
251
|
+
[[ -n "$prefix" && ${{#COMPREPLY[@]}} -gt 0 ]] && COMPREPLY=("${{COMPREPLY[@]/#/${{prefix}}user=}}")
|
|
252
|
+
elif [[ "$val" == reservation=* ]]; then
|
|
253
|
+
local filter_val="${{val#reservation=}}"
|
|
254
|
+
local reservations="$(_slurm_cache_reservations)"
|
|
255
|
+
COMPREPLY=($(compgen -W "$reservations" -- "$filter_val"))
|
|
256
|
+
[[ -n "$prefix" && ${{#COMPREPLY[@]}} -gt 0 ]] && COMPREPLY=("${{COMPREPLY[@]/#/${{prefix}}reservation=}}")
|
|
257
|
+
elif [[ "$val" == drain=* ]]; then
|
|
258
|
+
COMPREPLY=("REGEXP")
|
|
259
|
+
[[ -n "$prefix" ]] && COMPREPLY=("${{prefix}}drain=REGEXP")
|
|
260
|
+
else
|
|
261
|
+
# Not a filter prefix, show filter prefixes and cached nodes
|
|
262
|
+
COMPREPLY=($(compgen -W "$_slurm_node_filter_prefixes $cached_nodes" -- "$val"))
|
|
263
|
+
[[ ${{#COMPREPLY[@]}} -gt 0 && -n "$prefix" ]] && COMPREPLY=("${{COMPREPLY[@]/#/$prefix}}")
|
|
264
|
+
fi
|
|
265
|
+
}}
|
|
266
|
+
""" # noqa: E501
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""Base class for Slurm resource management."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Any, Dict
|
|
5
|
+
|
|
6
|
+
from rich.markup import escape
|
|
7
|
+
|
|
8
|
+
from .resources import Resource
|
|
9
|
+
from .utils import console
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class BaseSlurmResource:
|
|
13
|
+
"""Base class for all Slurm resources."""
|
|
14
|
+
|
|
15
|
+
_WIDTH = None
|
|
16
|
+
_TIME_PATTERNS = [
|
|
17
|
+
# YYYY-MM-DD[THH:MM[:SS]]
|
|
18
|
+
re.compile(
|
|
19
|
+
r"^(?P<date>\d{4}-\d{2}-\d{2})(?:[T ](?P<h>\d{1,2}):"
|
|
20
|
+
r"(?P<m>\d{1,2})(?::(?P<s>\d{1,2}))?)?$"
|
|
21
|
+
),
|
|
22
|
+
# [D-]HH:MM:SS (e.g., 2-12:30:00)
|
|
23
|
+
re.compile(
|
|
24
|
+
r"^(?:(?P<days>\d+)-)?(?P<h>\d{1,2}):(?P<m>\d{1,2}):"
|
|
25
|
+
r"(?P<s>\d{1,2})$"
|
|
26
|
+
),
|
|
27
|
+
# HH:MM:SS
|
|
28
|
+
re.compile(r"^(?P<h>\d{1,2}):(?P<m>\d{1,2}):(?P<s>\d{1,2})$"),
|
|
29
|
+
# MMDDYY
|
|
30
|
+
re.compile(r"^(?P<month>\d{2})(?P<day>\d{2})(?P<year>\d{2})$"),
|
|
31
|
+
# MM/DD/YY
|
|
32
|
+
re.compile(
|
|
33
|
+
r"^(?P<month>\d{2})/(?P<day>\d{2})/(?P<year>\d{2})$"
|
|
34
|
+
),
|
|
35
|
+
# MM.DD.YY
|
|
36
|
+
re.compile(
|
|
37
|
+
r"^(?P<month>\d{2})\.(?P<day>\d{2})\.(?P<year>\d{2})$"
|
|
38
|
+
),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
# Subclasses should define their valid_args
|
|
42
|
+
valid_args: Dict[str, Dict[str, str]] = {}
|
|
43
|
+
|
|
44
|
+
# Subclasses can define aliases for argument names (short -> canonical)
|
|
45
|
+
arg_aliases: Dict[str, str] = {}
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def get_profile_fields(cls) -> Dict[str, str]:
|
|
49
|
+
"""Return field names and descriptions for profile templates.
|
|
50
|
+
|
|
51
|
+
Subclasses should override this to provide their specific fields.
|
|
52
|
+
"""
|
|
53
|
+
return {}
|
|
54
|
+
|
|
55
|
+
@classmethod
|
|
56
|
+
def _check_args(
|
|
57
|
+
cls,
|
|
58
|
+
kwargs: Any,
|
|
59
|
+
set: Dict[str, Any],
|
|
60
|
+
add: Dict[str, Any],
|
|
61
|
+
delete: Dict[str, Any],
|
|
62
|
+
) -> bool:
|
|
63
|
+
"""Check if the arguments are valid."""
|
|
64
|
+
for key, value in kwargs.items():
|
|
65
|
+
arg_type = None
|
|
66
|
+
if key[-1] == "+":
|
|
67
|
+
key = key[:-1]
|
|
68
|
+
arg_type = "add"
|
|
69
|
+
elif key[-1] == "-":
|
|
70
|
+
key = key[:-1]
|
|
71
|
+
arg_type = "delete"
|
|
72
|
+
|
|
73
|
+
# Resolve aliases first (e.g., start -> starttime)
|
|
74
|
+
if key in cls.arg_aliases:
|
|
75
|
+
key = cls.arg_aliases[key]
|
|
76
|
+
|
|
77
|
+
# Try to match key as a prefix of exactly one valid_args key
|
|
78
|
+
matches = [
|
|
79
|
+
k for k in cls.valid_args.keys() if k.startswith(key)
|
|
80
|
+
]
|
|
81
|
+
if len(matches) == 1:
|
|
82
|
+
full_key = matches[0]
|
|
83
|
+
elif key not in cls.valid_args.keys():
|
|
84
|
+
console.print(f"Invalid argument: {key}!")
|
|
85
|
+
return False
|
|
86
|
+
elif len(matches) > 1:
|
|
87
|
+
console.print(
|
|
88
|
+
f"Ambiguous argument: {key}. Matches: {', '.join(matches)}"
|
|
89
|
+
)
|
|
90
|
+
return False
|
|
91
|
+
else:
|
|
92
|
+
full_key = key
|
|
93
|
+
key_type = cls.valid_args[full_key]["type"]
|
|
94
|
+
if key_type == "int":
|
|
95
|
+
try:
|
|
96
|
+
set[full_key] = int(value)
|
|
97
|
+
except ValueError:
|
|
98
|
+
console.print(
|
|
99
|
+
f"Invalid integer argument: {full_key}={value}"
|
|
100
|
+
)
|
|
101
|
+
return False
|
|
102
|
+
elif key_type == "list":
|
|
103
|
+
set[full_key] = value.split(",")
|
|
104
|
+
elif key_type == "memory":
|
|
105
|
+
try:
|
|
106
|
+
if value.endswith("M"):
|
|
107
|
+
set[full_key] = int(value.rstrip("M")) * 1024
|
|
108
|
+
elif value.endswith("G"):
|
|
109
|
+
set[full_key] = (
|
|
110
|
+
int(value.rstrip("G")) * 1024 * 1024
|
|
111
|
+
)
|
|
112
|
+
else:
|
|
113
|
+
console.print(
|
|
114
|
+
f"Invalid memory argument: {full_key}={value}"
|
|
115
|
+
)
|
|
116
|
+
return False
|
|
117
|
+
except ValueError:
|
|
118
|
+
console.print(
|
|
119
|
+
f"Invalid memory argument: {full_key}={value}"
|
|
120
|
+
)
|
|
121
|
+
return False
|
|
122
|
+
elif key_type == "time":
|
|
123
|
+
try:
|
|
124
|
+
set[full_key] = cls._parse_time_value(value)
|
|
125
|
+
except Exception:
|
|
126
|
+
console.print(
|
|
127
|
+
f"Invalid time argument: {full_key}={value}"
|
|
128
|
+
)
|
|
129
|
+
return False
|
|
130
|
+
elif key_type[0] == "[" and key_type[-1] == "]":
|
|
131
|
+
valid_values = [
|
|
132
|
+
v.strip() for v in key_type[1:-1].split(",")
|
|
133
|
+
]
|
|
134
|
+
if value.lower() not in valid_values:
|
|
135
|
+
console.print(
|
|
136
|
+
f"Invalid list argument: {full_key}={value}."
|
|
137
|
+
" Valid values are: "
|
|
138
|
+
f"{', '.join(valid_values)}"
|
|
139
|
+
)
|
|
140
|
+
return False
|
|
141
|
+
set[full_key] = value.lower()
|
|
142
|
+
# qos, partition, account, group, qos
|
|
143
|
+
elif key_type in ["qos", "partition", "account", "group"]:
|
|
144
|
+
value = value.lower()
|
|
145
|
+
# TODO: Check if the value is a valid qos, partition,
|
|
146
|
+
# account, or group
|
|
147
|
+
elif key_type == "nodes":
|
|
148
|
+
# Skip validation - nodes from filters (partition=, state=,
|
|
149
|
+
# user=, reservation=) are already validated by Slurm.
|
|
150
|
+
# Slurm will report errors if nodes don't exist.
|
|
151
|
+
set[full_key] = value
|
|
152
|
+
else:
|
|
153
|
+
console.print(
|
|
154
|
+
f"Invalid argument: {full_key}={value}. "
|
|
155
|
+
f"{key_type} not found"
|
|
156
|
+
)
|
|
157
|
+
return False
|
|
158
|
+
if arg_type:
|
|
159
|
+
# Remove from set dict if it was added during validation
|
|
160
|
+
set.pop(full_key, None)
|
|
161
|
+
if arg_type == "add":
|
|
162
|
+
add[full_key] = value
|
|
163
|
+
elif arg_type == "delete":
|
|
164
|
+
delete[full_key] = value
|
|
165
|
+
else:
|
|
166
|
+
set[full_key] = value
|
|
167
|
+
return True
|
|
168
|
+
|
|
169
|
+
@classmethod
|
|
170
|
+
def _parse_time_value(cls, val):
|
|
171
|
+
"""Parse time values in various formats."""
|
|
172
|
+
# Try to parse as integer seconds
|
|
173
|
+
try:
|
|
174
|
+
return int(val)
|
|
175
|
+
except ValueError:
|
|
176
|
+
pass
|
|
177
|
+
|
|
178
|
+
# Accepts:
|
|
179
|
+
# - now+count time-units
|
|
180
|
+
# - tomorrow
|
|
181
|
+
# - HH:MM:SS
|
|
182
|
+
# - MMDDYY
|
|
183
|
+
# - MM/DD/YY
|
|
184
|
+
# - MM.DD.YY
|
|
185
|
+
# - YYYY-MM-DD[THH:MM[:SS]]
|
|
186
|
+
# - [D-]HH:MM:SS (e.g., 2-12:30:00)
|
|
187
|
+
if val.startswith("now"):
|
|
188
|
+
return val
|
|
189
|
+
elif val.startswith("tomorrow"):
|
|
190
|
+
return val
|
|
191
|
+
|
|
192
|
+
m = None
|
|
193
|
+
for time_pattern in cls._TIME_PATTERNS:
|
|
194
|
+
m = time_pattern.match(val)
|
|
195
|
+
if m:
|
|
196
|
+
break
|
|
197
|
+
if not m:
|
|
198
|
+
raise ValueError("Invalid time format")
|
|
199
|
+
|
|
200
|
+
# Use groupdict to safely access optional groups
|
|
201
|
+
groups = m.groupdict()
|
|
202
|
+
date = groups.get("date")
|
|
203
|
+
days = int(groups["days"]) if groups.get("days") else 0
|
|
204
|
+
hh = int(groups["h"]) if groups.get("h") else 0
|
|
205
|
+
mm = int(groups["m"]) if groups.get("m") else 0
|
|
206
|
+
ss = int(groups["s"]) if groups.get("s") else 0
|
|
207
|
+
|
|
208
|
+
total_seconds = days * 86400 + hh * 3600 + mm * 60 + ss
|
|
209
|
+
|
|
210
|
+
if date:
|
|
211
|
+
# If date is present, return a datetime object
|
|
212
|
+
return f"{date}T{hh:02d}:{mm:02d}:{ss:02d}"
|
|
213
|
+
else:
|
|
214
|
+
# Otherwise, return total seconds as int
|
|
215
|
+
return total_seconds
|
|
216
|
+
|
|
217
|
+
@classmethod
|
|
218
|
+
def max_width(cls) -> int:
|
|
219
|
+
"""Get the maximum width of the console."""
|
|
220
|
+
if cls._WIDTH is None:
|
|
221
|
+
cls._WIDTH = console.width
|
|
222
|
+
return cls._WIDTH
|
|
223
|
+
|
|
224
|
+
@classmethod
|
|
225
|
+
def print_dict_pretty(cls, data: dict) -> bool:
|
|
226
|
+
"""Print a dictionary in a pretty format."""
|
|
227
|
+
line_len = 2
|
|
228
|
+
something_was_printed = False
|
|
229
|
+
console.print(" ", end="")
|
|
230
|
+
for k in sorted(data.keys()):
|
|
231
|
+
value = data[k]
|
|
232
|
+
if isinstance(value, list):
|
|
233
|
+
if len(value) == 0:
|
|
234
|
+
continue
|
|
235
|
+
value = ",".join(value)
|
|
236
|
+
else:
|
|
237
|
+
value = str(value)
|
|
238
|
+
if str(value) == "":
|
|
239
|
+
continue
|
|
240
|
+
line_len += len(k) + len(value) + 2
|
|
241
|
+
if line_len > cls.max_width():
|
|
242
|
+
console.print("\n ", end="")
|
|
243
|
+
line_len = 0
|
|
244
|
+
console.print(f"[label]{k}[/]: [str]{value}[/]", end=" ")
|
|
245
|
+
if len(k) + len(value) + 2 > cls.max_width():
|
|
246
|
+
console.print("\n ", end="")
|
|
247
|
+
line_len = 0
|
|
248
|
+
something_was_printed = True
|
|
249
|
+
if something_was_printed:
|
|
250
|
+
console.print()
|
|
251
|
+
return something_was_printed
|
|
252
|
+
|
|
253
|
+
@classmethod
|
|
254
|
+
def print_dict_pretty_def(
|
|
255
|
+
cls, data: dict, value_types: dict
|
|
256
|
+
) -> bool:
|
|
257
|
+
"""Print a dictionary skipping the default values
|
|
258
|
+
in a pretty format."""
|
|
259
|
+
line_len = 2
|
|
260
|
+
something_was_printed = False
|
|
261
|
+
width = cls.max_width()
|
|
262
|
+
for key in sorted(data.keys()):
|
|
263
|
+
value = data[key]
|
|
264
|
+
if key in value_types:
|
|
265
|
+
if value == value_types[key]["def"]:
|
|
266
|
+
continue
|
|
267
|
+
line_len += len(key) + len(value) + 2
|
|
268
|
+
if line_len > width:
|
|
269
|
+
console.print("\n ", end="")
|
|
270
|
+
line_len = 0
|
|
271
|
+
style = (
|
|
272
|
+
"allow"
|
|
273
|
+
if re.match(r"allow", key)
|
|
274
|
+
else (
|
|
275
|
+
"deny"
|
|
276
|
+
if re.match(r"deny", key)
|
|
277
|
+
else ("qos" if re.match(r"qos", key) else "str")
|
|
278
|
+
)
|
|
279
|
+
)
|
|
280
|
+
console.print(f"{key}: [{style}]{value}[/]", end=" ")
|
|
281
|
+
something_was_printed = True
|
|
282
|
+
if something_was_printed:
|
|
283
|
+
console.print()
|
|
284
|
+
return something_was_printed
|
|
285
|
+
|
|
286
|
+
@classmethod
|
|
287
|
+
def print_dict_pretty_flags_def(
|
|
288
|
+
cls,
|
|
289
|
+
data: dict,
|
|
290
|
+
value_types: dict,
|
|
291
|
+
) -> bool:
|
|
292
|
+
"""Print a dictionary skipping the default values
|
|
293
|
+
in a pretty format."""
|
|
294
|
+
line_len = 2
|
|
295
|
+
something_was_printed = False
|
|
296
|
+
width = cls.max_width()
|
|
297
|
+
console.print(" ", end="")
|
|
298
|
+
for key in sorted(data.keys()):
|
|
299
|
+
value = data[key]
|
|
300
|
+
if key in value_types:
|
|
301
|
+
if value == value_types[key]["def"]:
|
|
302
|
+
continue
|
|
303
|
+
line_len += len(key) + len(value) + 1
|
|
304
|
+
if line_len > width:
|
|
305
|
+
console.print("\n ", end="")
|
|
306
|
+
line_len = 0
|
|
307
|
+
if value == "YES":
|
|
308
|
+
console.print(f"[green]{escape(key)}[/]", end=" ")
|
|
309
|
+
else:
|
|
310
|
+
console.print(f"[red]{escape(key)}[/]", end=" ")
|
|
311
|
+
something_was_printed = True
|
|
312
|
+
if something_was_printed:
|
|
313
|
+
console.print()
|
|
314
|
+
return something_was_printed
|
|
315
|
+
|
|
316
|
+
@classmethod
|
|
317
|
+
def expand_nodenames(cls, pattern: str) -> list:
|
|
318
|
+
"""
|
|
319
|
+
Expand a Slurm-style node name pattern like "node[01,03,10-12]"
|
|
320
|
+
into a list of node names.
|
|
321
|
+
|
|
322
|
+
Example:
|
|
323
|
+
"node[01,03,10-12]" ->
|
|
324
|
+
["node01","node03","node10","node11","node12"]
|
|
325
|
+
"""
|
|
326
|
+
|
|
327
|
+
# Match prefix and bracketed part
|
|
328
|
+
m = re.match(r"^(.*)\[(.+)\]$", pattern)
|
|
329
|
+
if not m:
|
|
330
|
+
# No brackets, return as single node
|
|
331
|
+
return [pattern]
|
|
332
|
+
|
|
333
|
+
prefix = m.group(1)
|
|
334
|
+
inside = m.group(2)
|
|
335
|
+
result = []
|
|
336
|
+
|
|
337
|
+
# Split by comma, handle ranges and single values
|
|
338
|
+
for part in inside.split(","):
|
|
339
|
+
if "-" in part:
|
|
340
|
+
start, end = part.split("-", 1)
|
|
341
|
+
# Preserve leading zeros
|
|
342
|
+
width = max(len(start), len(end))
|
|
343
|
+
for i in range(int(start), int(end) + 1):
|
|
344
|
+
node = f"{prefix}{str(i).zfill(width)}"
|
|
345
|
+
result.append(node)
|
|
346
|
+
else:
|
|
347
|
+
node = f"{prefix}{part}"
|
|
348
|
+
result.append(node)
|
|
349
|
+
return result
|