diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-06-24 10:21:22 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-06-24 10:21:22 -0400 |
commit | 07600eb0df0af099a1c055f0a6c88114f2e857bd (patch) | |
tree | 34e1e4e4b0f773c5e357667e33f02de56740ed5f | |
parent | ccc65c673e09c7a8f92f2f92aacb1b1b7753df8e (diff) | |
download | stasis-07600eb0df0af099a1c055f0a6c88114f2e857bd.tar.gz |
Move micromamba function out of stasis_indexer.c
* Adjust code in the indexer to accommodate the move. The function now expects a MicromambaInfo structure as its first argument.
-rw-r--r-- | include/conda.h | 14 | ||||
-rw-r--r-- | src/conda.c | 55 | ||||
-rw-r--r-- | src/stasis_indexer.c | 68 |
3 files changed, 79 insertions, 58 deletions
diff --git a/include/conda.h b/include/conda.h index 086a842..cea3f02 100644 --- a/include/conda.h +++ b/include/conda.h @@ -8,6 +8,20 @@ #define CONDA_INSTALL_PREFIX "conda" +struct MicromambaInfo { + char *micromamba_prefix; + char *conda_prefix; +}; + +/** + * 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 diff --git a/src/conda.c b/src/conda.c index 342b6af..90a8587 100644 --- a/src/conda.c +++ b/src/conda.c @@ -5,6 +5,61 @@ #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]; + 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); + system(untarcmd); + } + + 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)); diff --git a/src/stasis_indexer.c b/src/stasis_indexer.c index fb231e0..7cf0815 100644 --- a/src/stasis_indexer.c +++ b/src/stasis_indexer.c @@ -200,57 +200,6 @@ struct Delivery **get_latest_deliveries(struct Delivery ctx[], size_t nelem) { return result; } -int micromamba(const char *write_to, const char *prefix, 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); - if (access("latest", F_OK)) { - download(url, "latest", NULL); - } - - char mmbin[PATH_MAX]; - sprintf(mmbin, "%s/micromamba", write_to); - - if (access(mmbin, F_OK)) { - char untarcmd[PATH_MAX]; - mkdirs(write_to, 0755); - sprintf(untarcmd, "tar -xvf latest -C %s --strip-components=1 bin/micromamba 1>/dev/null", write_to); - system(untarcmd); - } - - char cmd[STASIS_BUFSIZ]; - memset(cmd, 0, sizeof(cmd)); - sprintf(cmd, "%s -r %s -p %s ", mmbin, prefix, prefix); - va_list args; - va_start(args, command); - vsprintf(cmd + strlen(cmd), command, args); - va_end(args); - - mkdirs(prefix, 0755); - - char rcpath[PATH_MAX]; - sprintf(rcpath, "%s/.condarc", prefix); - touch(rcpath); - - setenv("CONDARC", rcpath, 1); - setenv("MAMBA_ROOT_PREFIX", prefix, 1); - int status = system(cmd); - unsetenv("MAMBA_ROOT_PREFIX"); - - return status; -} - int indexer_make_website(struct Delivery *ctx) { char cmd[PATH_MAX]; char *inputs[] = { @@ -291,16 +240,17 @@ int indexer_make_website(struct Delivery *ctx) { int indexer_conda(struct Delivery *ctx) { int status = 0; - char prefix[PATH_MAX]; - sprintf(prefix, "%s/%s", ctx->storage.tmpdir, "indexer"); + char micromamba_prefix[PATH_MAX] = {0}; + sprintf(micromamba_prefix, "%s/bin", ctx->storage.tools_dir); + struct MicromambaInfo m = {.conda_prefix = globals.conda_install_prefix, .micromamba_prefix = micromamba_prefix}; - status += micromamba(ctx->storage.tmpdir, prefix, "config prepend --env channels conda-forge"); + status += micromamba(&m, "config prepend --env channels conda-forge"); if (!globals.verbose) { - status += micromamba(ctx->storage.tmpdir, prefix, "config set --env quiet true"); + status += micromamba(&m, "config set --env quiet true"); } - status += micromamba(ctx->storage.tmpdir, prefix, "config set --env always_yes true"); - status += micromamba(ctx->storage.tmpdir, prefix, "install conda-build"); - status += micromamba(ctx->storage.tmpdir, prefix, "run conda index %s", ctx->storage.conda_artifact_dir); + status += micromamba(&m, "config set --env always_yes true"); + status += micromamba(&m, "install conda-build"); + status += micromamba(&m, "run conda index %s", ctx->storage.conda_artifact_dir); return status; } @@ -558,6 +508,8 @@ void indexer_init_dirs(struct Delivery *ctx, const char *workdir) { exit(1); } path_store(&ctx->storage.output_dir, PATH_MAX, ctx->storage.root, "output"); + path_store(&ctx->storage.tools_dir, PATH_MAX, ctx->storage.output_dir, "tools"); + path_store(&globals.conda_install_prefix, PATH_MAX, ctx->storage.tools_dir, "conda"); path_store(&ctx->storage.cfgdump_dir, PATH_MAX, ctx->storage.output_dir, "config"); path_store(&ctx->storage.meta_dir, PATH_MAX, ctx->storage.output_dir, "meta"); path_store(&ctx->storage.delivery_dir, PATH_MAX, ctx->storage.output_dir, "delivery"); |