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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
//
// Created by jhunk on 5/14/23.
//
#include <unistd.h>
#include "conda.h"
int micromamba(struct MicromambaInfo *info, char *command, ...) {
struct utsname sys;
uname(&sys);
tolower_s(sys.sysname);
if (!strcmp(sys.sysname, "darwin")) {
strcpy(sys.sysname, "osx");
}
if (!strcmp(sys.machine, "x86_64")) {
strcpy(sys.machine, "64");
}
char url[PATH_MAX];
sprintf(url, "https://micro.mamba.pm/api/micromamba/%s-%s/latest", sys.sysname, sys.machine);
char installer_path[PATH_MAX];
sprintf(installer_path, "%s/latest", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
if (access(installer_path, F_OK)) {
download(url, installer_path, NULL);
}
char mmbin[PATH_MAX];
sprintf(mmbin, "%s/micromamba", info->micromamba_prefix);
if (access(mmbin, F_OK)) {
char untarcmd[PATH_MAX * 2];
mkdirs(info->micromamba_prefix, 0755);
sprintf(untarcmd, "tar -xvf %s -C %s --strip-components=1 bin/micromamba 1>/dev/null", installer_path, info->micromamba_prefix);
int untarcmd_status = system(untarcmd);
if (untarcmd_status) {
return -1;
}
}
char cmd[STASIS_BUFSIZ];
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "%s -r %s -p %s ", mmbin, info->conda_prefix, info->conda_prefix);
va_list args;
va_start(args, command);
vsprintf(cmd + strlen(cmd), command, args);
va_end(args);
mkdirs(info->conda_prefix, 0755);
char rcpath[PATH_MAX];
sprintf(rcpath, "%s/.condarc", info->conda_prefix);
touch(rcpath);
setenv("CONDARC", rcpath, 1);
setenv("MAMBA_ROOT_PREFIX", info->conda_prefix, 1);
int status = system(cmd);
unsetenv("MAMBA_ROOT_PREFIX");
return status;
}
int python_exec(const char *args) {
char command[PATH_MAX];
memset(command, 0, sizeof(command));
snprintf(command, sizeof(command) - 1, "python %s", args);
msg(STASIS_MSG_L3, "Executing: %s\n", command);
return system(command);
}
int pip_exec(const char *args) {
char command[PATH_MAX];
memset(command, 0, sizeof(command));
snprintf(command, sizeof(command) - 1, "python -m pip %s", args);
msg(STASIS_MSG_L3, "Executing: %s\n", command);
return system(command);
}
int pip_index_provides(const char *index_url, const char *name, const char *version) {
char cmd[PATH_MAX] = {0};
char name_local[255];
char version_local[255] = {0};
char spec[255] = {0};
if (isempty((char *) name) < 0) {
// no package name means nothing to do.
return -1;
}
// Fix up the package name
strncpy(name_local, name, sizeof(name_local) - 1);
tolower_s(name_local);
lstrip(name_local);
strip(name_local);
if (version) {
// Fix up the package version
strncpy(version_local, version, sizeof(version_local) - 1);
tolower_s(version_local);
lstrip(version_local);
strip(version_local);
sprintf(spec, "==%s", version);
}
char logfile[] = "/tmp/STASIS-package_exists.XXXXXX";
int logfd = mkstemp(logfile);
if (logfd < 0) {
perror(logfile);
remove(logfile); // fail harmlessly if not present
return -1;
}
int status = 0;
struct Process proc;
memset(&proc, 0, sizeof(proc));
proc.redirect_stderr = 1;
strcpy(proc.f_stdout, logfile);
// Do an installation in dry-run mode to see if the package exists in the given index.
snprintf(cmd, sizeof(cmd) - 1, "python -m pip install --dry-run --no-deps --index-url=%s %s%s", index_url, name_local, spec);
status = shell(&proc, cmd);
// Print errors only when shell() itself throws one
// If some day we want to see the errors thrown by pip too, use this condition instead: (status != 0)
if (status < 0) {
FILE *fp = fdopen(logfd, "r");
if (!fp) {
remove(logfile);
return -1;
} else {
char line[BUFSIZ] = {0};
fflush(stdout);
fflush(stderr);
while (fgets(line, sizeof(line) - 1, fp) != NULL) {
fprintf(stderr, "%s", line);
}
fflush(stderr);
fclose(fp);
}
}
remove(logfile);
return proc.returncode == 0;
}
int conda_exec(const char *args) {
char command[PATH_MAX];
const char *mamba_commands[] = {
"build",
"install",
"update",
"create",
"list",
"search",
"run",
"info",
"clean",
"activate",
"deactivate",
NULL
};
char conda_as[6];
memset(conda_as, 0, sizeof(conda_as));
strcpy(conda_as, "conda");
for (size_t i = 0; mamba_commands[i] != NULL; i++) {
if (startswith(args, mamba_commands[i])) {
strcpy(conda_as, "mamba");
break;
}
}
snprintf(command, sizeof(command) - 1, "%s %s", conda_as, args);
msg(STASIS_MSG_L3, "Executing: %s\n", command);
return system(command);
}
char *conda_runtime_dump(const char *root) {
const char *init_script_conda = "/etc/profile.d/conda.sh";
const char *init_script_mamba = "/etc/profile.d/mamba.sh";
int state = 0;
char cmd[BUFSIZ] = {0};
sprintf(cmd, "source %s%s; source %s%s; env; declare -f", root, init_script_conda, root, init_script_mamba);
char *output = shell_output(cmd, &state);
if (state) {
guard_free(output);
return NULL;
}
return output;
}
int conda_activate(const char *root, const char *env_name) {
int fd = -1;
FILE *fp = NULL;
char logfile[PATH_MAX] = {0};
struct Process proc;
memset(&proc, 0, sizeof(proc));
char *full_runtime = conda_runtime_dump(root);
if (!full_runtime) {
SYSERROR("%s", "Failed to read variables and functions from the runtime environment");
return -1;
}
// Set the path to our stdout log
// Emulate mktemp()'s behavior. Give us a unique file name, but don't use
// the file handle at all. We'll open it as a FILE stream soon enough.
sprintf(logfile, "%s/%s", globals.tmpdir, "shell_XXXXXX");
fd = mkstemp(logfile);
if (fd < 0) {
perror(logfile);
return -1;
}
close(fd);
// Configure our process for output to a log file
strcpy(proc.f_stdout, logfile);
// Fully activate conda and record its effect on the runtime environment
char command[PATH_MAX * 3];
snprintf(command, sizeof(command) - 1, "%s\nconda activate %s &>/dev/null\nenv -0\n", full_runtime, env_name);
guard_free(full_runtime);
int retval = shell(&proc, command);
if (retval) {
// it didn't work; drop out for cleanup
remove(logfile);
return retval;
}
// Parse the log file:
// 1. Extract the environment keys and values from the sub-shell
// 2. Apply it to STASIS's runtime environment
// 3. Now we're ready to execute conda commands anywhere
fp = fopen(proc.f_stdout, "r");
if (!fp) {
perror(logfile);
return -1;
}
while (!feof(fp)) {
char buf[STASIS_BUFSIZ] = {0};
int ch = 0;
size_t z = 0;
// We are ingesting output from "env -0" and can't use fgets()
// Copy each character into the buffer until we encounter '\0' or EOF
while (z < sizeof(buf) && (ch = (int) fgetc(fp)) != 0) {
if (ch == EOF) {
break;
}
buf[z] = (char) ch;
z++;
}
buf[strlen(buf)] = 0;
if (!strlen(buf)) {
continue;
}
char **part = split(buf, "=", 1);
if (!part) {
perror("unable to split environment variable buffer");
return -1;
}
if (!part[0]) {
msg(STASIS_MSG_WARN | STASIS_MSG_L1, "Invalid environment variable key ignored: '%s'\n", buf);
} else if (!part[1]) {
msg(STASIS_MSG_WARN | STASIS_MSG_L1, "Invalid environment variable value ignored: '%s'\n", buf);
} else {
setenv(part[0], part[1], 1);
}
GENERIC_ARRAY_FREE(part);
}
fclose(fp);
remove(logfile);
return 0;
}
int conda_check_required() {
int status = 0;
struct StrList *result = NULL;
char cmd[PATH_MAX] = {0};
const char *conda_minimum_viable_tools[] = {
"boa",
"conda-build",
"conda-verify",
NULL
};
// Construct a "conda list" command that searches for all required packages
// using conda's (python's) regex matching
strcat(cmd, "conda list '");
for (size_t i = 0; conda_minimum_viable_tools[i] != NULL; i++) {
strcat(cmd, "^");
strcat(cmd, conda_minimum_viable_tools[i]);
if (conda_minimum_viable_tools[i + 1] != NULL) {
strcat(cmd, "|");
}
}
strcat(cmd, "' | cut -d ' ' -f 1");
// Verify all required packages are installed
char *cmd_out = shell_output(cmd, &status);
if (cmd_out) {
size_t found = 0;
result = strlist_init();
strlist_append_tokenize(result, cmd_out, "\n");
for (size_t i = 0; i < strlist_count(result); i++) {
char *item = strlist_item(result, i);
if (isempty(item) || startswith(item, "#")) {
continue;
}
for (size_t x = 0; conda_minimum_viable_tools[x] != NULL; x++) {
if (!strcmp(item, conda_minimum_viable_tools[x])) {
found++;
}
}
}
if (found < (sizeof(conda_minimum_viable_tools) / sizeof(*conda_minimum_viable_tools)) - 1) {
guard_free(cmd_out);
guard_strlist_free(&result);
return 1;
}
guard_free(cmd_out);
guard_strlist_free(&result);
} else {
msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "The base package requirement check could not be performed\n");
return 2;
}
return 0;
}
int conda_setup_headless() {
if (globals.verbose) {
conda_exec("config --system --set quiet false");
} else {
// Not verbose, so squelch conda's noise
conda_exec("config --system --set quiet true");
}
// Configure conda for headless CI
conda_exec("config --system --set auto_update_conda false"); // never update conda automatically
conda_exec("config --system --set notify_outdated_conda false"); // never notify about outdated conda version
conda_exec("config --system --set always_yes true"); // never prompt for input
conda_exec("config --system --set safety_checks disabled"); // speedup
conda_exec("config --system --set rollback_enabled false"); // speedup
conda_exec("config --system --set report_errors false"); // disable data sharing
conda_exec("config --system --set solver libmamba"); // use a real solver
char cmd[PATH_MAX];
size_t total = 0;
if (globals.conda_packages && strlist_count(globals.conda_packages)) {
memset(cmd, 0, sizeof(cmd));
strcpy(cmd, "install ");
total = strlist_count(globals.conda_packages);
for (size_t i = 0; i < total; i++) {
char *item = strlist_item(globals.conda_packages, i);
if (isempty(item)) {
continue;
}
sprintf(cmd + strlen(cmd), "'%s'", item);
if (i < total - 1) {
strcat(cmd, " ");
}
}
if (conda_exec(cmd)) {
msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "Unable to install user-defined base packages (conda)\n");
return 1;
}
}
if (globals.pip_packages && strlist_count(globals.pip_packages)) {
memset(cmd, 0, sizeof(cmd));
strcpy(cmd, "install ");
total = strlist_count(globals.pip_packages);
for (size_t i = 0; i < total; i++) {
char *item = strlist_item(globals.pip_packages, i);
if (isempty(item)) {
continue;
}
sprintf(cmd + strlen(cmd), "'%s'", item);
if (i < total - 1) {
strcat(cmd, " ");
}
}
if (pip_exec(cmd)) {
msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "Unable to install user-defined base packages (pip)\n");
return 1;
}
}
if (conda_check_required()) {
msg(STASIS_MSG_ERROR | STASIS_MSG_L2, "Your STASIS configuration lacks the bare"
" minimum software required to build conda packages."
" Please fix it.\n");
return 1;
}
if (globals.always_update_base_environment) {
if (conda_exec("update --all")) {
fprintf(stderr, "conda update was unsuccessful\n");
return 1;
}
}
return 0;
}
int conda_env_create_from_uri(char *name, char *uri) {
char env_command[PATH_MAX];
sprintf(env_command, "env create -n %s -f %s", name, uri);
return conda_exec(env_command);
}
int conda_env_create(char *name, char *python_version, char *packages) {
char env_command[PATH_MAX];
sprintf(env_command, "create -n %s python=%s %s", name, python_version, packages ? packages : "");
return conda_exec(env_command);
}
int conda_env_remove(char *name) {
char env_command[PATH_MAX];
sprintf(env_command, "env remove -n %s", name);
return conda_exec(env_command);
}
int conda_env_export(char *name, char *output_dir, char *output_filename) {
char env_command[PATH_MAX];
sprintf(env_command, "env export -n %s -f %s/%s.yml", name, output_dir, output_filename);
return conda_exec(env_command);
}
int conda_index(const char *path) {
char command[PATH_MAX];
sprintf(command, "index %s", path);
return conda_exec(command);
}
|