aboutsummaryrefslogtreecommitdiff
path: root/src/cli/stasis_indexer/readmes.c
blob: 3dedb5c21e48dd15b1101b55be5cd4188aa1ee59 (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
#include "core.h"
#include "readmes.h"

int indexer_readmes(struct Delivery **ctx, const size_t nelem) {
    size_t nelem_real = 0;
    struct Delivery **latest_deliveries = get_latest_deliveries(ctx, nelem, &nelem_real);
    if (!latest_deliveries) {
        if (errno) {
            return -1;
        }
        return 0;
    }

    char indexfile[PATH_MAX] = {0};
    sprintf(indexfile, "%s/README.md", (*ctx)->storage.delivery_dir);

    FILE *indexfp = fopen(indexfile, "w+");
    if (!indexfp) {
        fprintf(stderr, "Unable to open %s for writing\n", indexfile);
        return -1;
    }
    struct StrList *archs = get_architectures(latest_deliveries, nelem_real);
    struct StrList *platforms = get_platforms(latest_deliveries, nelem_real);

    fprintf(indexfp, "# %s-%s\n\n", (*ctx)->meta.name, (*ctx)->meta.version);
    fprintf(indexfp, "## Current Release\n\n");
    strlist_sort(platforms, STASIS_SORT_ALPHA);
    strlist_sort(archs, STASIS_SORT_ALPHA);
    strlist_reverse(archs);
    for (size_t p = 0; p < strlist_count(platforms); p++) {
        char *platform = strlist_item(platforms, p);
        for (size_t a = 0; a < strlist_count(archs); a++) {
            char *arch = strlist_item(archs, a);
            int have_combo = 0;
            for (size_t i = 0; i < nelem_real; i++) {
                if (latest_deliveries[i]->system.platform) {
                    if (strstr(latest_deliveries[i]->system.platform[DELIVERY_PLATFORM_RELEASE], platform) &&
                        strstr(latest_deliveries[i]->system.arch, arch)) {
                        have_combo = 1;
                    }
                }
            }
            if (!have_combo) {
                continue;
            }
            fprintf(indexfp, "### %s-%s\n\n", platform, arch);
            for (size_t i = 0; i < nelem_real; i++) {
                char link_name[PATH_MAX] = {0};
                char readme_name[PATH_MAX] = {0};
                char conf_name[PATH_MAX] = {0};
                char conf_name_relative[PATH_MAX] = {0};
                if (!latest_deliveries[i]->meta.name) {
                    continue;
                }
                sprintf(link_name, "latest-py%s-%s-%s.yml", latest_deliveries[i]->meta.python_compact, latest_deliveries[i]->system.platform[DELIVERY_PLATFORM_RELEASE], latest_deliveries[i]->system.arch);
                sprintf(readme_name, "README-py%s-%s-%s.md", latest_deliveries[i]->meta.python_compact, latest_deliveries[i]->system.platform[DELIVERY_PLATFORM_RELEASE], latest_deliveries[i]->system.arch);
                sprintf(conf_name, "%s.ini", latest_deliveries[i]->info.release_name);
                sprintf(conf_name_relative, "../config/%s.ini", latest_deliveries[i]->info.release_name);
                if (strstr(link_name, platform) && strstr(link_name, arch)) {
                    fprintf(indexfp, "- Python %s\n", latest_deliveries[i]->meta.python);
                    fprintf(indexfp, "  - Info: [README](%s)\n", readme_name);
                    fprintf(indexfp, "  - Release: [Conda Environment YAML](%s)\n", link_name);
                    fprintf(indexfp, "  - Receipt: [STASIS input file](%s)\n", conf_name_relative);

                    char *pattern = NULL;
                    asprintf(&pattern, "*%s*%s*",
                             latest_deliveries[i]->info.build_number,
                             strstr((*ctx)->rules.release_fmt, "%p") ? latest_deliveries[i]->meta.python_compact : "" );
                    if (!pattern) {
                        SYSERROR("%s", "Unable to allocate bytes for pattern");
                        return -1;
                    }
                    struct StrList *docker_images = get_docker_images(latest_deliveries[i], pattern);
                    if (docker_images
                        && strlist_count(docker_images)
                        && !strcmp(latest_deliveries[i]->system.platform[DELIVERY_PLATFORM_RELEASE], "linux")) {
                        fprintf(indexfp, "  - Docker: ");
                        fprintf(indexfp, "[Archive](../packages/docker/%s)\n", path_basename(strlist_item(docker_images, 0)));
                    }
                    guard_strlist_free(&docker_images);
                    guard_free(pattern);
                }
            }
            fprintf(indexfp, "\n");
        }
        fprintf(indexfp, "\n");
    }

    fprintf(indexfp, "## Releases\n");
    int current_rc = (*ctx)->meta.rc;
    for (size_t i = 0; i < nelem_real; i++) {
        struct Delivery *current = ctx[i];
        if (current_rc > current->meta.rc) {
            current_rc = current->meta.rc;
            fprintf(indexfp, "\n\n---\n\n");
        }
        fprintf(indexfp, "### %s\n", current->info.release_name);
        fprintf(indexfp, "- Info: [README](README-%s.md)\n", current->info.release_name);
        fprintf(indexfp, "- Release: [Conda Environment YAML](%s.yml)\n", current->info.release_name);
        fprintf(indexfp, "- Receipt: [STASIS input file](../config/%s.ini)\n", current->info.release_name);

        char *pattern = NULL;
        asprintf(&pattern, "*%s*%s*",
                 current->info.build_number,
                 strstr((*ctx)->rules.release_fmt, "%p") ? current->meta.python_compact : "" );
        if (!pattern) {
            SYSERROR("%s", "Unable to allocate bytes for pattern");
            return -1;
        }

        struct StrList *docker_images = get_docker_images(current, pattern);
        if (docker_images
                && strlist_count(docker_images)
                && !strcmp(current->system.platform[DELIVERY_PLATFORM_RELEASE], "linux")) {
            fprintf(indexfp, "- Docker: \n");
            fprintf(indexfp, "[Archive](../packages/docker/%s)\n", path_basename(strlist_item(docker_images, 0)));
        }
        guard_strlist_free(&docker_images);
        guard_free(pattern);
    }
    fprintf(indexfp, "\n");

    guard_strlist_free(&archs);
    guard_strlist_free(&platforms);
    fclose(indexfp);

    // "latest_deliveries" is an array of pointers to ctxs[]. Do not free the contents of the array.
    guard_free(latest_deliveries);
    return 0;
}