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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
//
// 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 *spec) {
char cmd[PATH_MAX] = {0};
char spec_local[255] = {0};
if (isempty((char *) spec)) {
// NULL or zero-length; no package spec means there's nothing to do.
return -1;
}
// Normalize the local spec string
strncpy(spec_local, spec, sizeof(spec_local) - 1);
tolower_s(spec_local);
lstrip(spec_local);
strip(spec_local);
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", index_url, spec_local);
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);
}
int conda_activate(const char *root, const char *env_name) {
int fd = -1;
FILE *fp = NULL;
const char *init_script_conda = "/etc/profile.d/conda.sh";
const char *init_script_mamba = "/etc/profile.d/mamba.sh";
char path_conda[PATH_MAX] = {0};
char path_mamba[PATH_MAX] = {0};
char logfile[PATH_MAX] = {0};
struct Process proc;
memset(&proc, 0, sizeof(proc));
// Where to find conda's init scripts
sprintf(path_conda, "%s%s", root, init_script_conda);
sprintf(path_mamba, "%s%s", root, init_script_mamba);
// 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);
// Verify conda's init scripts are available
if (access(path_conda, F_OK) < 0) {
perror(path_conda);
remove(logfile);
return -1;
}
if (access(path_mamba, F_OK) < 0) {
perror(path_mamba);
remove(logfile);
return -1;
}
// Fully activate conda and record its effect on the runtime environment
char command[PATH_MAX * 3];
snprintf(command, sizeof(command) - 1, "set -a; source %s; source %s; conda activate %s &>/dev/null; env -0", path_conda, path_mamba, env_name);
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);
}
char *conda_get_active_environment() {
const char *name = getenv("CONDA_DEFAULT_ENV");
if (!name) {
return NULL;
}
char *result = NULL;
result = strdup(name);
if (!result) {
return NULL;
}
return result;
}
int conda_provides(const char *spec) {
struct Process proc;
memset(&proc, 0, sizeof(proc));
strcpy(proc.f_stdout, "/dev/null");
strcpy(proc.f_stderr, "/dev/null");
// It's worth noting the departure from using conda_exec() here:
// conda_exec() expects the program output to be visible to the user.
// For this operation we only need the exit value.
char cmd[PATH_MAX] = {0};
snprintf(cmd, sizeof(cmd) - 1, "mamba search --use-index-cache %s", spec);
if (shell(&proc, cmd) < 0) {
fprintf(stderr, "shell: %s", strerror(errno));
return -1;
}
return proc.returncode == 0;
}
int conda_index(const char *path) {
char command[PATH_MAX];
sprintf(command, "index %s", path);
return conda_exec(command);
}
|