Skip to content

Commit 7834627

Browse files
committed
feat: CLI options can be supplied using env vars
Although sbatch already allows providing values for options through SBATCH_ prefixed env vars, not all options are supported. This change adds support for all options of sbatch through a GRIDTK_SUBMIT_ prefix. For example, to always get emails when a job finishes, you can set GRIDTK_SUBMIT_MAIL_USER to your email address and GRIDTK_SUBMIT_MAIL_TYPE to END. This is useful when you have a set of options that you always want to use, but don't want to specify them every time you submit a job.
1 parent 56c5441 commit 7834627

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,26 @@ This is a fast, convenient, and **recommended** way to submit a job without havi
198198
script and since everything is tracked by GridTK, you still benefit from the same
199199
reproducibility guarantees.
200200

201+
### Environment Variables for CLI Options
202+
203+
While sbatch already allows providing values for some options through `SBATCH_` prefixed environment variables, not all options are supported. GridTK adds support for all CLI options through the `GRIDTK_SUBMIT_` prefix. For example:
204+
205+
```bash
206+
# Set default email notification settings
207+
export GRIDTK_SUBMIT_MAIL_USER=your.email@example.com
208+
export GRIDTK_SUBMIT_MAIL_TYPE=END
209+
210+
# Use debug partition by default
211+
export GRIDTK_SUBMIT_PARTITION=debug
212+
213+
# Now submit a job - it will use these settings automatically
214+
gridtk submit job.sh
215+
```
216+
217+
This is useful when you have a set of options that you always want to use, but don't want to specify them every time you submit a job.
218+
219+
Of course, other gridtk commands such as `gridtk resubmit` options can also be set using environment variables like: `export GRIDTK_RESUBMIT_STATE=ALL`.
220+
201221
### Job Dependencies
202222

203223
To submit a job that depends on another job, use the `--dependency` flag:

src/gridtk/cli.py

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def _job_filters_decorator(function):
142142
context_settings={
143143
"show_default": True,
144144
"help_option_names": ["--help", "-h"],
145+
"auto_envvar_prefix": "GRIDTK",
145146
},
146147
)
147148
@click.option(

tests/test_gridtk.py

+39
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,45 @@ def test_submit_wrap(mock_check_output, runner):
202202
)
203203

204204

205+
@patch("subprocess.check_output")
206+
def test_submit_with_env_vars(mock_check_output: Mock, runner):
207+
"""Test that environment variables with GRIDTK_SUBMIT_ prefix are properly converted to CLI options."""
208+
mock_check_output.return_value = _sbatch_output(123456789)
209+
with runner.isolated_filesystem():
210+
result = runner.invoke(
211+
cli,
212+
["submit", "job.sh"],
213+
env={
214+
"GRIDTK_SUBMIT_MAIL_USER": "test@example.com",
215+
"GRIDTK_SUBMIT_MAIL_TYPE": "END",
216+
"GRIDTK_SUBMIT_PARTITION": "debug",
217+
},
218+
)
219+
220+
assert_click_runner_result(result)
221+
assert "1" in result.output
222+
223+
mock_check_output.assert_called_with(
224+
[
225+
"sbatch",
226+
"--job-name",
227+
"gridtk",
228+
"--output",
229+
"logs/gridtk.%j.out",
230+
"--error",
231+
"logs/gridtk.%j.out",
232+
"--mail-type",
233+
"END",
234+
"--mail-user",
235+
"test@example.com",
236+
"--partition",
237+
"debug",
238+
"job.sh",
239+
],
240+
text=True,
241+
)
242+
243+
205244
@patch("subprocess.check_output")
206245
def test_submit_triple_dash(mock_check_output: Mock, runner):
207246
mock_check_output.return_value = _sbatch_output(123456789)

0 commit comments

Comments
 (0)