aboutsummaryrefslogtreecommitdiff
path: root/src/wheel.c
blob: a4ddbff6ee33f5fc7fba3a4d1036cff5ea6719c5 (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
#include "wheel.h"

struct Wheel *get_wheel_file(const char *basepath, const char *name, char *to_match[]) {
    DIR *dp;
    struct dirent *rec;
    struct Wheel *result = NULL;
    char package_path[PATH_MAX];
    char package_name[NAME_MAX];

    strcpy(package_name, name);
    tolower_s(package_name);
    sprintf(package_path, "%s/%s", basepath, package_name);

    dp = opendir(package_path);
    if (!dp) {
        return NULL;
    }

    while ((rec = readdir(dp)) != NULL) {
        if (!strcmp(rec->d_name, ".") || !strcmp(rec->d_name, "..")) {
            continue;
        }
        char filename[NAME_MAX];
        strcpy(filename, rec->d_name);
        char *ext = strstr(filename, ".whl");
        if (ext) {
            *ext = '\0';
        } else {
            // not a wheel file. nothing to do
            continue;
        }

        size_t match = 0;
        size_t pattern_count = 0;
        for (; to_match[pattern_count] != NULL; pattern_count++) {
            if (strstr(filename, to_match[pattern_count])) {
                match++;
            }
        }

        if (!startswith(rec->d_name, name) || match != pattern_count) {
            continue;
        }

        result = calloc(1, sizeof(*result));
        result->path_name = realpath(package_path, NULL);
        result->file_name = strdup(rec->d_name);

        size_t parts_total;
        char **parts = split(filename, "-", 0);
        for (parts_total = 0; parts[parts_total] != NULL; parts_total++);
        if (parts_total < 6) {
            // no build tag
            result->distribution = strdup(parts[0]);
            result->version = strdup(parts[1]);
            result->build_tag = NULL;
            result->python_tag = strdup(parts[2]);
            result->abi_tag = strdup(parts[3]);
            result->platform_tag = strdup(parts[4]);
        } else {
            // has build tag
            result->distribution = strdup(parts[0]);
            result->version = strdup(parts[1]);
            result->build_tag = strdup(parts[2]);
            result->python_tag = strdup(parts[3]);
            result->abi_tag = strdup(parts[4]);
            result->platform_tag = strdup(parts[5]);
        }
        split_free(parts);
        break;
    }
    closedir(dp);
    return result;
}