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

Specify script_runner using variables? - For, .e.g., Python virtualenv #1125

Closed
SamuelMarks opened this issue Jul 25, 2024 · 1 comment
Closed
Assignees

Comments

@SamuelMarks
Copy link
Contributor

SamuelMarks commented Jul 25, 2024

Feature Description

Can script_runner evaluate its variables?

Describe The Solution You'd Like

I know I can use shebang but this syntax makes more sense and would be cleaner especially when files are referenced or python -m <module_name> <arguments> are provided without resorting to @shell or @duckscript.

Code Sample

[config]
skip_core_tasks = true

[env]
VENV_DIR = "/tmp/venvs"
UV_DIR = "/tmp/venvs"
VENV_NAME = "venv"

[tasks.default]
clear = true
command = "echo"
args = ["default task"]
dependencies = ["pythontask"]


[tasks.venv]
script_runner = "@duckscript"
script = '''
if not is_file ${VENV_DIR}/${VENV_NAME}
    if is_windows
        # TODO: uv supports Windows, should be easy to impl here.
    else
        if not is_file ${UV_DIR}/uv/bin/uv
            if not is_file uv-installer.sh
                exec curl --proto "=https" --tlsv1.2 -OLsSf https://github.com/astral-sh/uv/releases/download/0.2.29/uv-installer.sh
            end
            exec env -i CARGO_HOME=${UV_DIR}/uv HOME=${UV_DIR}/uv /usr/bin/bash uv-installer.sh --no-modify-path
            rm uv-installer.sh
        end
        exec ${UV_DIR}/uv/bin/uv venv ${VENV_DIR}/${VENV_NAME}
        if is_file requirements.txt
            exec env -i PATH=${VENV_DIR}/${VENV_NAME}/bin ${UV_DIR}/uv/bin/uv pip install -r requirements.txt
        end
    end
end
'''

[tasks.pythontask]
script_runner = "${VENV_DIR}/${VENV_NAME}/bin/python"
script_extension = "py"
script = '''
import sys
# import pip; print(pip.__path__)
print(sys.executable, "\n", sys.version, sep="")
print("Python task running in Python")
'''
dependencies = ["venv"]
[cargo-make] INFO - Running Task: pythontask
[cargo-make] INFO - Execute Command: "${VENV_DIR}/${VENV_NAME}/bin/python" "/tmp/fsio_WYhdLBybEa.py"
[cargo-make] ERROR - Unable to execute script.

PS: To elaborate the idea, here's an example of executing a file that does work, but a script_runner variant makes more sense semantically IMHO:

[tasks.myfile_py]
script_runner = "@shell"
script = '''
${VENV_DIR}/${VENV_NAME}/bin/python myfile.py
'''
@sagiegurari
Copy link
Owner

this is now released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants