summaryrefslogtreecommitdiff
path: root/src/sbo.cpp
blob: 086ba79e33f443ff9c9f789d40e91beb549d0705 (plain)
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
#include "config.h"
#include "sbo.h"
#include <algorithm>
#include <fstream>
#include <vector>

namespace katja
{
    sbo::sbo()
        : git_binary(boost::process::search_path("git"))
        , cache_path(std::filesystem::path{ WORKDIR } / "sbo/repository")
    {
    }

    info sbo::parse_info_file(
        const std::filesystem::path& package_directory) const
    {
        std::map<std::string, std::vector<std::string>> results;
        std::filesystem::path info_path = cache_path
            / package_directory
            / package_directory.filename().replace_extension(".info");

        if (!std::filesystem::exists(info_path))
        {
            throw package_exception(package_directory.filename().string(), "Package doesn't exist.");
        }
        // Parse the info file.
        std::ifstream info_file{ info_path, std::ios::in };
        if (!info_file.is_open())
        {
            throw package_exception(package_directory.filename().string(), "Unable to open the .info file.");
        }
        std::string line;
        std::string variable_name;
        std::vector<std::string> variable_values;

        while (std::getline(info_file, line))
        {
            std::string::const_iterator current_char = line.cbegin();

            if (variable_name.empty())
            {
                current_char = std::find(current_char, line.cend(), '=');
                if (current_char == line.cbegin() || current_char == line.cend())
                {
                    break; // Expected 'variable name' or '='.
                }
                ++current_char;
                if (current_char == line.cend() || *current_char != '"')
                {
                    break; // Expected '"'.
                }
                variable_name = std::string(line.cbegin(), current_char - 1);

                variable_values.push_back("");
            }

            for (current_char += 1; current_char != line.end(); ++current_char)
            {
                if (*current_char == ' ')
                {
                    if (!variable_values.back().empty())
                    {
                        variable_values.push_back("");
                    }
                }
                else if (*current_char == '"')
                {
                    if (variable_values.back().empty())
                    {
                        variable_values.pop_back();
                    }
                    results.insert_or_assign(variable_name, variable_values);
                    variable_name = "";
                    variable_values.clear();
                    break;
                }
                else if (*current_char == '\\')
                {
                    break;
                }
                else
                {
                    variable_values.back().push_back(*current_char);
                }
            }
            if (current_char == line.cend())
            {
                break; // Expected '\' or '"'.
            }
        }
        info _info;

        _info.prgnam = results["PRGNAM"][0];
        _info.version = results["VERSION"][0];
        _info.homepage = results["HOMEPAGE"].size() == 0 ? "" : results["HOMEPAGE"][0];
        _info.download = results["DOWNLOAD"];
        _info.md5sum = results["DOWNLOAD"];
        _info.download_x86_64 = results["DOWNLOAD_X86_64"];
        _info.md5sum_x86_64 = results["MD5SUM_X86_64"];
        _info.requires = results["REQUIRES"];
        _info.maintainer = results["MAINTAINER"][0];
        _info.email = results["EMAIL"][0];

        return _info;
    }

    std::unordered_map<std::string, std::filesystem::path> sbo::collect_packages() const
    {
        std::unordered_map<std::string, std::filesystem::path> results;

        for (const auto& category_directory : std::filesystem::directory_iterator(cache_path))
        {
            if (!category_directory.is_directory())
            {
                continue;
            }
            for (const auto& package_directory : std::filesystem::directory_iterator(category_directory.path()))
            {
                results.insert({ package_directory.path().filename().string(),
                    category_directory.path().filename() });
            }
        }
        return results;
     }

    std::unordered_map<std::string, package> sbo::list(const std::set<std::string>& package_names)
    {
        std::string line;
        std::unordered_map<std::string, package> packages;
        boost::process::environment slackbuild_environment;
        slackbuild_environment["PRINT_PACKAGE_NAME"] = "y";

        std::unordered_map<std::string, std::filesystem::path> package_categories = collect_packages();

        for (const std::string& package_name : package_names)
        {
            std::unordered_map<std::string, std::filesystem::path>::iterator package_category =
                package_categories.find(package_name);

            if (package_category == package_categories.cend())
            {
                continue;
            }
            std::filesystem::path package_directory_name{ package_category->first };
            std::filesystem::path slackbuild_cwd = cache_path
                / package_category->second / package_directory_name;

            // Execute the SlackBuild to get package information.
            std::filesystem::path slackbuild_file_name =
                package_directory_name.replace_extension(".SlackBuild");
            boost::process::ipstream slackbuild_output;

            boost::process::child slackbuild_process("/bin/bash", slackbuild_file_name.native(),
                slackbuild_environment,
                boost::process::start_dir(slackbuild_cwd.native()),
                boost::process::std_out > slackbuild_output,
                boost::process::std_in.close(),
                boost::process::std_err > boost::process::null);

            std::optional<package> maybe_package;
            if (slackbuild_process.running() && std::getline(slackbuild_output, line) && !line.empty())
            {
                maybe_package = package::parse(std::filesystem::path(line).replace_extension(""));
            }
            slackbuild_process.terminate();

            if (maybe_package.has_value())
            {
                packages.insert({ maybe_package.value().name(), maybe_package.value() });
            }
        }
        return packages;
    }

    void sbo::refresh()
    {
        std::filesystem::file_status repository_status = std::filesystem::status(cache_path);

        if (std::filesystem::exists(repository_status)
            && !std::filesystem::is_directory(repository_status))
        {
            throw std::runtime_error("The working directory path \""
                + cache_path.string() + "\" exists, but it isn't a directory.");
        }
        else if (!std::filesystem::exists(repository_status))
        {
            git("clone", std::filesystem::path(),
                "git://git.slackbuilds.org/slackbuilds.git", cache_path.native());
        }
        git("remote", cache_path.native(), "update", "--prune");
        git("reset", cache_path.native(), "--hard", "origin/master");
    }

    std::forward_list<std::string> sbo::check_dependencies(const std::string& package_name)
    {
        std::unordered_map<std::string, std::filesystem::path> package_categories = collect_packages();
        ordered_set resolved;

        resolve_dependencies(package_name, package_categories, resolved);
        std::forward_list<std::string> results;

        for (const auto& dependency : resolved.get<1>())
        {
            results.push_front(dependency);
        }
        return results;
    }

    void sbo::resolve_dependencies(const std::string& package_name,
        const std::unordered_map<std::string, std::filesystem::path>& package_categories,
        ordered_set& resolved) const
    {
        std::unordered_map<std::string, std::filesystem::path>::const_iterator package_category =
            package_categories.find(package_name);

        if (package_category == package_categories.cend())
        {
            throw package_exception(package_name, "Package not found.");
        }
        info info_file = parse_info_file(package_category->second / package_category->first);

        for (const auto& package_dependency : info_file.requires)
        {
            if (resolved.find(package_dependency) == resolved.cend())
            {
                resolve_dependencies(package_dependency, package_categories, resolved);
            }
        }
        resolved.insert(package_name);
    }
}