Skip to content

Commit

Permalink
fix: respect shebang lines in post-deploy scripts (see deployment doc…
Browse files Browse the repository at this point in the history
…s) (#1841)

* fix: use configured shell executable for post deploy scripts

* Note on default shell.

* Use shebang for post-deploy script execution
  • Loading branch information
johanneskoester committed Sep 1, 2022
1 parent 58675c6 commit c26c4b6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/snakefiles/deployment.rst
Expand Up @@ -353,7 +353,15 @@ Post-deployment scripts must be placed next to their corresponding environment-f
└── interproscan.post-deploy.sh
The path of the conda environment can be accessed within the script via ``$CONDA_PREFIX``.
Importantly, if the script relies on certain shell specific syntax, (e.g. `set -o pipefail` for bash), make sure to add a matching shebang to the script, e.g.:

.. code-block:: bash
#!env bash
set -o pipefail
# ...
If no shebang line like above (``#!env bash``) is provided, the script will be executed with the ``sh`` command.

.. _conda_named_env:

Expand Down
1 change: 1 addition & 0 deletions docs/snakefiles/rules.rst
Expand Up @@ -27,6 +27,7 @@ Inside the shell command, all local and global variables, especially input and o
Here, input and output (and in general any list or tuple) automatically evaluate to a space-separated list of files (i.e. ``path/to/inputfile path/to/other/inputfile``).
From Snakemake 3.8.0 on, adding the special formatting instruction ``:q`` (e.g. ``"somecommand {input:q} {output:q}")``) will let Snakemake quote each of the list or tuple elements that contains whitespace.

By default shell commands will be invoked with ``bash`` shell (unless the workflow specifies a different default shell via ``shell.executable(...)``).

Instead of a shell command, a rule can run some python code to generate the output:

Expand Down
10 changes: 9 additions & 1 deletion snakemake/deployment/conda.py
Expand Up @@ -350,8 +350,16 @@ def execute_deployment_script(self, env_file, deploy_file):
os.path.relpath(path=deploy_file, start=os.getcwd())
)
)

# Determine interpreter from shebang or use sh as default.
interpreter = "sh"
with open(deploy_file, "r") as f:
first_line = next(iter(f))
if first_line.startswith("#!"):
interpreter = first_line[2:].strip()

shell.check_output(
self.conda.shellcmd(self.address, "sh {}".format(deploy_file)),
self.conda.shellcmd(self.address, f"{interpreter} {deploy_file}"),
stderr=subprocess.STDOUT,
)

Expand Down
5 changes: 5 additions & 0 deletions test.sh
@@ -0,0 +1,5 @@
set -o pipefail

echo $BASH

echo test

0 comments on commit c26c4b6

Please sign in to comment.