summaryrefslogtreecommitdiff
path: root/conda.groovy
blob: bb7d37f6109a1f6b8ef6bcbdceaf6933ab67c647 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.lang.*

class Conda {
    public String prefix
    public boolean prefix_exists
    public Map<String, String> sh_environment
    public String conda_environment

    Conda (prefix) {
        this.prefix = prefix
        this.prefix_exists = new File(this.prefix).exists()
        this.sh_environment = [:]
        this.conda_environment = ""

        /*if (this.prefix_exists) {
            println('Activating root environment')
            this.sh_environment = this.activate("root")
        }*/
    }

    private def runshell(String args, silent=false) {
        def cmd = new String[3]
        def proc_env = [:]

        if (this.sh_environment) {
            proc_env = this.sh_environment
        }

        cmd[0] = 'bash'
        cmd[1] = '-c'
        cmd[2] = args

        def process = new ProcessBuilder(cmd)
        process.redirectErrorStream(true)
        Map<String, String> env_tmp = process.environment()
        env_tmp <<= proc_env

        Process p = process.start()
        if (!silent) {
            p.inputStream.eachLine { println it}
        }
        p.waitFor()
        return p
    }

    void activate(String conda_env) {
        def records = [:]
        def bah = this.runshell("source ${prefix}/bin/activate ${conda_env} ; printenv", true)
        bah.inputStream.eachLine { line ->
            if(line) {
                def (k, v) = line.split('=', 2).collect { it.trim() }
                records."$k" = v
            }
        }
        this.sh_environment = records
        this.conda_environment = conda_env
    }

    def version() {
        return this.runshell("conda --version")
    }

    def verify() {
        this.runshell("echo \$PATH")
        this.runshell("which python")
        this.runshell("python -c 'from pprint import pprint; import sys; pprint(sys.path)'")
    }

    void create(String name, String packages) {
        println("Creating environment: ${name}")
        this.runshell("conda create --yes -n \"${name}\" ${packages}")
    }

    void install() {
    }

}

static void main(String[] args) {
    println("OK")
    c = new Conda("/users/jhunk/anaconda3")
    c.activate("root")
    println("Conda exists: ${c.prefix_exists}")
    c.verify()
    c.create("groovy", "python=3.5")
    c.activate("groovy")
    c.verify()
}