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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
import core.cpuid : isX86_64;
import std.array;
import std.conv;
import std.file;
import std.stdio;
import std.string;
import std.system;
import std.path;
import std.process;
import std.typecons;
import util;
/**
Interact with a `conda` installation. If `install_prefix` does
not exist, miniconda will be downloaded and installed in the current
directory.
*/
class Conda {
import std.net.curl : download;
/// Gate to prevent PATH clobbering or reconfiguration
public bool initialized = false;
/// channel URIs (order preserved)
public string[] channels;
/// path to install miniconda (or existing miniconda installation)
public string install_prefix;
/// which version of miniconda to install
public string installer_version = "4.5.12";
/// which variant (python "2", or "3")
public string installer_variant = "3";
/// the runtime environment used for all shell executions
public string[string] env;
private string[string] env_orig;
private const string url_base = "https://repo.continuum.io";
private const string url_miniconda = join([this.url_base, "miniconda"], "/");
private string url_installer;
this() {
env = getenv();
env_orig = env.dup;
this.url_installer = join([this.url_miniconda, this.installer_file()], "/");
}
void dump_env_shell() {
foreach (pair; this.env.byKeyValue()) {
writeln(pair.key ~ "=" ~ pair.value);
}
}
/// there isn't a good way to determine the CPU type in `phobos`
private string arch() {
if (isX86_64()) {
return "x86_64";
}
else if (!isX86_64()) {
return "x86";
}
throw new Exception("Unsupported CPU");
}
/// generate conda platform string
private string platform() {
import std.system : OS, os;
string report;
switch (os) {
default:
throw new Exception("Unsupported OS");
case OS.linux:
report = "Linux";
break;
case OS.osx:
report = "MacOSX";
break;
case OS.win32:
case OS.win64:
report = "Windows";
break;
}
return report;
}
/// determine if the installation prefix
bool installed() {
if (!this.install_prefix.empty && this.install_prefix.exists) {
return true;
}
return false;
}
/// determine if "our" conda is "the conda" in the environment
bool in_env() {
string path = this.env.get("PATH", "");
if (path.empty || this.install_prefix.empty) {
return false;
}
foreach (string record; split(path, pathSeparator)) {
if (record == this.install_prefix ~ pathSeparator ~ "bin") {
return true;
}
}
return false;
}
/// determine if the installer exists
private bool have_installer() {
if (!this.installer_file().exists) {
return false;
}
return true;
}
/// generate an installer filename name based on gathered specs
private string installer_file() {
string ext = ".sh";
version (Windows) { ext = ".exe"; }
string filename = join([
"Miniconda" ~ this.installer_variant,
this.installer_version,
this.platform(),
this.arch()
], "-") ~ ext;
return filename;
}
/// install miniconda into `install_prefix`
bool installer() {
if (this.in_env() || this.install_prefix.exists) {
writefln("Miniconda is already installed: %s", this.install_prefix);
return true;
} else if (this.install_prefix.empty) {
this.install_prefix = absolutePath("./miniconda");
} else {
this.install_prefix = absolutePath(this.install_prefix);
}
if (this.have_installer()) {
writeln("Miniconda installation script already exists");
} else {
writeln("Downloading " ~ this.installer_file());
download(this.url_installer, this.installer_file());
}
// execute installation (batch mode)
auto installer = this.sh(
"bash "
~ this.installer_file()
~ " -b"
~ " -p "
~ this.install_prefix);
if (installer != 0) {
return false;
}
return true;
}
/// Generate a .condarc inside the root of the `install_prefix`
void configure_headless() {
auto fp = File(chainPath(this.install_prefix, ".condarc").array, "w+");
fp.write("changeps1: False\n");
fp.write("always_yes: True\n");
fp.write("quiet: True\n");
fp.write("auto_update_conda: False\n");
fp.write("notify_outdated_conda: False\n");
fp.write("rollback_enabled: False\n");
fp.write("channels:\n");
if (this.channels.empty) {
fp.write(" - defaults\n");
} else {
foreach (channel; this.channels) {
fp.write(" - " ~ channel ~ "\n");
}
}
}
/// Add conda to the runtime environment and configure it for general use
void initialize() {
if (this.initialized) {
writeln("Conda installation has already been initialized");
return;
}
this.env["PATH"] = join(
[cast(string)chainPath(this.install_prefix, "bin").array,
this.env["PATH"]],
pathSeparator);
this.configure_headless();
this.fix_setuptools();
this.initialized = true;
}
/// Activates a conda environment by name
void activate(string name) {
this.env_orig = this.env.dup;
string[string] env_new = getenv(this.env, "source activate " ~ name);
this.env = env_new.dup;
this.fix_setuptools();
}
/// Restores the last recorded environment
/// TODO: `env` should be able to handle nested environments
void deactivate() {
this.env = this.env_orig.dup;
}
/// Execute a conda command
int run(string command) {
auto proc = this.sh("conda " ~ command);
return proc;
}
/// ditto
/// returns process object
auto run_block(string command) {
auto proc = this.sh_block("conda " ~ command);
return proc;
}
/// Execute shell command
int sh(string command) {
banner('#', command);
auto proc = spawnShell(command, env=this.env);
scope(exit) wait(proc);
return wait(proc);
}
/// ditto
/// returns process object
auto sh_block(string command) {
auto proc = executeShell(command, env=this.env);
return proc;
}
/// Generate additive command line arguments
string multiarg(string flag, string[] arr) {
if (arr.empty)
return "";
return flag ~ " " ~ arr.join(" " ~ flag ~ " ");
}
/// Wildcard search prefix for extracted packages
string[] scan_packages(string pattern="*") {
string[] result;
string pkgdir = chainPath(this.install_prefix, "pkgs").array;
if (!pkgdir.exists) {
throw new Exception(pkgdir ~ " does not exist");
}
foreach (DirEntry e; dirEntries(pkgdir, pattern, SpanMode.shallow)) {
if (e.isFile || e.name.endsWith(dirSeparator ~ "cache")) {
continue;
}
result ~= baseName(e.name);
}
return result;
}
/// determine if a conda environment is present
bool env_exists(string name) {
return buildPath(this.install_prefix, "envs", name).exists;
}
/// return system path to `name`ed environment
string env_where(string name) {
if (this.env_exists(name)) {
return buildPath(this.install_prefix, "envs", name);
}
return null;
}
/// return current conda environment
string env_current() {
return this.env.get("CONDA_PREFIX", null);
}
/// returns site-packages directory for the active Python interpreter
string site() {
return this.sh_block("python -c 'import site; print(site.getsitepackages()[0])'").output.strip;
}
/// conda does not like setuptools.
/// this allows `pip` to [un]install packages
void fix_setuptools() {
string pthfile = buildPath(this.site(), "easy-install.pth");
if (!pthfile.exists) {
// inject easy-install.pth
File(pthfile, "w+").write("");
}
}
string dump_env_yaml(string filename=null) {
string args;
if (filename !is null) {
args = "--file " ~ filename;
}
auto proc = this.run_block("env export " ~ args);
return proc.output;
}
string dump_env_explicit(string filename=null) {
auto proc = this.run_block("list --explicit");
if (filename !is null) {
auto file = File(filename, "w+");
file.write(proc.output);
}
return proc.output;
}
// Note: This is to see what pip sees. It will not be useful to an end
// user.
string dump_env_freeze(string filename=null) {
auto proc = this.sh_block("pip freeze");
if (filename !is null) {
auto file = File(filename, "w+");
file.write(proc.output);
}
return proc.output;
}
}
|