Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect shebang lines in post-deploy scripts (see deployment docs) #1841

Merged
merged 3 commits into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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