Skip to content

Commit

Permalink
fix: fix string resource definition in CLI and profile (#2627)
Browse files Browse the repository at this point in the history
### Description

The PR is related to the[ issue described for the slurm submit
executor](snakemake/snakemake-executor-plugin-slurm#18)
plugin to not correctly hand over arguments.

The test folder is named `test_slurm_resource_propagation meesters` and
may not be complete, as it covers multiple related scenarios to provoke
the error.

Note, that the tests were carried out when using a cluster:

- `$ snakemake --executor slurm -j2 --workflow-profile ./profiles/
--default-resources slurm_account=<account>`
  yields
  ``Error:
  WorkflowError:
Failed to evaluate default resources value '<function
eval_resource_expression.<locals>.callable at 0x7f5c19ed0e00>'.```
    
when the patition is defined at the rule level in a profile. (see the
directory of the test case)
    
- likewise `$ snakemake --executor slurm -j2 --workflow-profile
./profiles/ --default-resources slurm_account=<account> --set-resources
slurm_extra="--nice=150"`
  yields
```ValueError: Invalid resource definition: entries have to be defined
as RULE:RESOURCE=VALUE, with VALUE being a positive integer a quoted
string, or a Python expression (e.g. min(max(2*input.size_mb, 1000),
8000)).```
  
These errors can both be triggered on the command line and in the config
file, of course.


### QC

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [ ] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).

---------

Co-authored-by: Johannes Koester <johannes.koester@uni-due.de>
Co-authored-by: Johannes Köster <johannes.koester@tu-dortmund.de>
  • Loading branch information
3 people committed Feb 2, 2024
1 parent 6252404 commit bbd76ae
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 38 deletions.
2 changes: 1 addition & 1 deletion snakemake/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def parse_set_resources(args):
assignments = defaultdict(dict)
if args is not None:
for entry in args:
key, value = parse_key_value_arg(entry, errmsg=errmsg)
key, value = parse_key_value_arg(entry, errmsg=errmsg, strip_quotes=False)
key = key.split(":")
if len(key) != 2:
raise ValueError(errmsg)
Expand Down
5 changes: 3 additions & 2 deletions snakemake/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ def mb_to_mib(mb):
return int(math.ceil(mb * 0.95367431640625))


def parse_key_value_arg(arg, errmsg):
def parse_key_value_arg(arg, errmsg, strip_quotes=True):
try:
key, val = arg.split("=", 1)
except ValueError:
raise ValueError(errmsg + f" (Unparseable value: {repr(arg)})")
val = val.strip("'\"")
if strip_quotes:
val = val.strip("'\"")
return key, val


Expand Down
4 changes: 2 additions & 2 deletions snakemake/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,10 @@ def generic_callable(val, threads_arg, **kwargs):
if not is_file_not_found_error(e, kwargs["input"]):
# Missing input files are handled by the caller
raise WorkflowError(
"Failed to evaluate default resources value "
"Failed to evaluate resources value "
f"'{val}'.\n"
" String arguments may need additional "
"quoting. Ex: --default-resources "
"quoting. E.g.: --default-resources "
"\"tmpdir='/home/user/tmp'\".",
e,
)
Expand Down
33 changes: 0 additions & 33 deletions tests/Snakefile

This file was deleted.

9 changes: 9 additions & 0 deletions tests/test_resource_string_in_cli_or_profile/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# fails when submitted as
# $ snakemake --executor slurm -j2 --workflow-profile ./profiles/ --default-resources slurm_account=m2_zdvhpc

rule all:
input: "a.out"

rule test1:
output: "a.out"
shell: "touch {output}"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set-resources:
test1:
slurm_partition: "smp"
slurm_extra: "'--nice=150'"
11 changes: 11 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,17 @@ def test_issue1256():
assert "line 9" in stderr


def test_resource_string_in_cli_or_profile():
test_path = dpath("test_resource_string_in_cli_or_profile")
profile = os.path.join(test_path, "profiles")
# workflow profile is loaded by default
run(
test_path,
snakefile="Snakefile",
shellcmd=f"snakemake --workflow-profile {profile} -c1 --default-resources slurm_account=foo other_resource='--test'",
)


def test_queue_input():
run(dpath("test_queue_input"))

Expand Down

0 comments on commit bbd76ae

Please sign in to comment.