aboutsummaryrefslogtreecommitdiff
path: root/include/conda.h
blob: f0314790ef3128f7772e3a40991a7eda390944ef (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
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
//! @file conda.h
#ifndef STASIS_CONDA_H
#define STASIS_CONDA_H

#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
#include "core.h"
#include "download.h"

#define CONDA_INSTALL_PREFIX "conda"
#define PYPI_INDEX_DEFAULT "https://pypi.org/simple"

#define PKG_USE_PIP 0
#define PKG_USE_CONDA 1

#define PKG_NOT_FOUND 0
#define PKG_FOUND 1

#define PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET (-10)
#define PKG_E_SUCCESS (PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET + 0)
#define PKG_INDEX_PROVIDES_E_INTERNAL_MODE_UNKNOWN (PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET + 1)
#define PKG_INDEX_PROVIDES_E_INTERNAL_LOG_HANDLE (PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET + 2)
#define PKG_INDEX_PROVIDES_E_MANAGER_RUNTIME (PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET + 3)
#define PKG_INDEX_PROVIDES_E_MANAGER_SIGNALED (PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET + 4)
#define PKG_INDEX_PROVIDES_E_MANAGER_EXEC (PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET + 5)
#define PKG_INDEX_PROVIDES_FAILED(ECODE) (ECODE <= PKG_INDEX_PROVIDES_ERROR_MESSAGE_OFFSET)

struct MicromambaInfo {
    char *micromamba_prefix;    //!< Path to write micromamba binary
    char *conda_prefix;         //!< Path to install conda base tree
};

/**
 * Execute micromamba
 * @param info MicromambaInfo data structure (must be populated before use)
 * @param command printf-style formatter string
 * @param ... variadic arguments
 * @return exit code
 */
int micromamba(struct MicromambaInfo *info, char *command, ...);

/**
 * Execute Python
 * Python interpreter is determined by PATH
 *
 * ```c
 * if (python_exec("-c 'printf(\"Hello world\")'")) {
 *     fprintf(stderr, "Hello world failed\n");
 *     exit(1);
 * }
 * ```
 *
 * @param args arguments to pass to interpreter
 * @return exit code from python interpreter
 */
int python_exec(const char *args);

/**
 * Execute Pip
 * Pip is determined by PATH
 *
 * ```c
 * if (pip_exec("freeze")) {
 *     fprintf(stderr, "pip freeze failed\n");
 *     exit(1);
 * }
 * ```
 *
 * @param args arguments to pass to Pip
 * @return exit code from Pip
 */
int pip_exec(const char *args);

/**
 * Execute conda (or if possible, mamba)
 * Conda/Mamba is determined by PATH
 *
 * ```c
 * if (conda_exec("env list")) {
 *     fprintf(stderr, "Failed to list conda environments\n");
 *     exit(1);
 * }
 * ```
 *
 * @param args arguments to pass to Conda
 * @return exit code from Conda
 */
int conda_exec(const char *args);

/**
 * Configure the runtime environment to use Conda/Mamba
 *
 * ```c
 * if (conda_activate("/path/to/conda/installation", "base")) {
 *     fprintf(stderr, "Failed to activate conda's base environment\n");
 *     exit(1);
 * }
 * ```
 *
 * @param root directory where conda is installed
 * @param env_name the conda environment to activate
 * @return 0 on success, -1 on error
 */
int conda_activate(const char *root, const char *env_name);

/**
 * Configure the active conda installation for headless operation
 */
int conda_setup_headless();

/**
 * Creates a Conda environment from a YAML config
 *
 * ```c
 * if (conda_env_create_from_uri("myenv", "https://myserver.tld/environment.yml")) {
 *     fprintf(stderr, "Environment creation failed\n");
 *     exit(1);
 * }
 * ```
 *
 * @param name Name of new environment to create
 * @param uri /path/to/environment.yml
 * @param uri file:///path/to/environment.yml
 * @param uri http://myserver.tld/environment.yml
 * @param uri https://myserver.tld/environment.yml
 * @param uri ftp://myserver.tld/environment.yml
 * @return exit code from "conda"
 */
int conda_env_create_from_uri(char *name, char *uri);

/**
 * Create a Conda environment using generic package specs
 *
 * ```c
 * // Create a basic environment without any conda packages
 * if (conda_env_create("myenv", "3.11", NULL)) {
 *     fprintf(stderr, "Environment creation failed\n");
 *     exit(1);
 * }
 *
 * // Create a basic environment and install conda packages
 * if (conda_env_create("myenv", "3.11", "hstcal fitsverify")) {
 *     fprintf(stderr, "Environment creation failed\n");
 *     exit(1);
 * }
 * ```
 *
 * @param name Environment name
 * @param python_version Desired version of Python
 * @param packages Packages to install (or NULL)
 * @return exit code from "conda"
 */
int conda_env_create(char *name, char *python_version, char *packages);

/**
 * Remove a Conda environment
 *
 * ```c
 * if (conda_env_remove("myenv")) {
 *     fprintf(stderr, "Unable to remove conda environment\n");
 *     exit(1);
 * }
 * ```
 *
 * @param name Environment name
 * @return exit code from "conda"
 */
int conda_env_remove(char *name);

/**
 * Export a Conda environment in YAML format
 *
 * ```c
 * if (conda_env_export("myenv", "./", "myenv.yml")) {
 *     fprintf(stderr, "Unable to export environment\n");
 *     exit(1);
 * }
 * ```
 *
 * @param name Environment name to export
 * @param output_dir Destination directory
 * @param output_filename Destination file name
 * @return exit code from "conda"
 */
int conda_env_export(char *name, char *output_dir, char *output_filename);

/**
 * Run "conda index" on a local conda channel
 *
 * ```c
 * if (conda_index("/path/to/channel")) {
 *     fprintf(stderr, "Unable to index requested path\n");
 *     exit(1);
 * }
 * ```
 *
 * @param path Top-level directory of conda channel
 * @return exit code from "conda"
 */
int conda_index(const char *path);

/**
 * Determine whether a package index contains a package
 *
 * ```c
 * int result = pkg_index_provides(USE_PIP, NULL, "numpy>1.26");
 * if (PKG_INDEX_PROVIDES_FAILED(result)) {
 *     fprintf(stderr, "failed: %s\n", pkg_index_provides_strerror(result));
 *     exit(1);
 * } else if (result == PKG_NOT_FOUND) {
 *     // package does not exist upstream
 * } else {
 *     // package exists upstream
 * }
 * ```
 *
 * @param mode USE_PIP
 * @param mode USE_CONDA
 * @param index a file system path or url pointing to a simple index or conda channel
 * @param spec a pip package specification (e.g. `name==1.2.3`)
 * @param spec a conda package specification (e.g. `name=1.2.3`)
 * @return PKG_NOT_FOUND, if not found
 * @return PKG_FOUND, if found
 * @return PKG_E_INDEX_PROVIDES_{ERROR}, on error (see conda.h)
 */
int pkg_index_provides(int mode, const char *index, const char *spec);
const char *pkg_index_provides_strerror(int code);

char *conda_get_active_environment();

#endif //STASIS_CONDA_H