Groovy web console

subscribe to the feed Subscribe
to this
site

fat shpy

Published 1 month ago by mf
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
class env {
    static String WORKSPACE = "root"
}

def sh (... args){
    print args.script
}

def shpy(Map config) {
    def user_script = config.script ?: 'echo \'You Fool!\''
    def py2v = config.py2v ?: ''
    def py3v = config.py3v ?: ''
    def python = config.python ?: 'python3'
    def path = config.path ?: "${env.WORKSPACE}/${python}-venv"

    env_script = "module load python2/${py2v} python3/${py3v} &>/dev/null"

    // if the *user* specifies a python version, create a virtualenv
    if (config.python) {
        env_script += """
            ${python} -m virtualenv ${path}
            . ${path}/bin/activate
        """.stripIndent()
    }

    sh (
        encoding: config.encoding,
        label: config.label,
        returnStatus: config.returnStatus,
        returnStdout: config.returnStdout,
        script: env_script + user_script,
    )
}


shpy (
    script: "hello",
    py3v: "3.8",
    python: "python2.7",
    //path: "mine",
)