aboutsummaryrefslogtreecommitdiff
path: root/paludis/environments/paludis/output_conf.cc
blob: 5b30b4739cf01968a422a56a76853fa308392d24 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/environments/paludis/output_conf.hh>
#include <paludis/environments/paludis/bashable_conf.hh>
#include <paludis/environments/paludis/paludis_config.hh>
#include <paludis/environments/paludis/paludis_environment.hh>
#include <paludis/environments/paludis/action_to_string.hh>
#include <paludis/util/log.hh>
#include <paludis/util/private_implementation_pattern-impl.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/named_value.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/options.hh>
#include <paludis/util/make_shared_copy.hh>
#include <paludis/util/tribool.hh>
#include <paludis/util/simple_visitor_cast.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/create_output_manager_info.hh>
#include <paludis/package_id.hh>
#include <paludis/match_package.hh>
#include <paludis/action.hh>
#include <list>
#include <vector>

using namespace paludis;
using namespace paludis::paludis_environment;

namespace paludis
{
    namespace n
    {
        struct action_requirement;
        struct ignore_unfetched_requirement;
        struct manager;
        struct matches_requirement;
        struct name_requirement;
        struct output_exclusivity_requirement;
        struct type_requirement;
    }
}

namespace
{
    struct Rule
    {
        NamedValue<n::action_requirement, std::string> action_requirement;
        NamedValue<n::ignore_unfetched_requirement, Tribool> ignore_unfetched_requirement;
        NamedValue<n::manager, std::string> manager;
        NamedValue<n::matches_requirement, std::tr1::shared_ptr<PackageDepSpec> > matches_requirement;
        NamedValue<n::name_requirement, std::string> name_requirement;
        NamedValue<n::output_exclusivity_requirement, OutputExclusivity> output_exclusivity_requirement;
        NamedValue<n::type_requirement, std::string> type_requirement;
    };

    typedef std::list<Rule> RuleList;
}

namespace paludis
{
    template<>
    struct Implementation<OutputConf>
    {
        const PaludisEnvironment * const env;
        RuleList rules;

        Implementation(const PaludisEnvironment * const e) :
            env(e)
        {
        }
    };
}

OutputConf::OutputConf(const PaludisEnvironment * const e) :
    PrivateImplementationPattern<OutputConf>(new Implementation<OutputConf>(e))
{
}

OutputConf::~OutputConf()
{
}

namespace
{
    void set_rule(const Environment * const env, Rule & rule, const std::string & k, const std::string & v)
    {
        if (k == "type")
            rule.type_requirement() = v;
        else if (k == "name")
            rule.name_requirement() = v;
        else if (k == "output_exclusivity")
            rule.output_exclusivity_requirement() = destringify<OutputExclusivity>(v);
        else if (k == "matches")
            rule.matches_requirement() = make_shared_copy(parse_user_package_dep_spec(
                        v, env, UserPackageDepSpecOptions() + updso_allow_wildcards + updso_no_disambiguation));
        else if (k == "action")
            rule.action_requirement() = v;
        else if (k == "ignore_unfetched")
            rule.ignore_unfetched_requirement() = destringify<Tribool>(v);
        else
            throw PaludisConfigError("Unknown rule '" + k + "'");
    }

    struct MatchRuleVisitor
    {
        const Environment * const env;
        const Rule & rule;

        MatchRuleVisitor(const Environment * const e, const Rule & r) :
            env(e),
            rule(r)
        {
        }

        bool visit(const CreateOutputManagerForRepositorySyncInfo & i) const
        {
            if (rule.type_requirement() != "*" && rule.type_requirement() != "repository")
                return false;

            if (rule.name_requirement() != "*" && rule.name_requirement() != stringify(i.repository().name()))
                return false;

            if (rule.action_requirement() != "*" && rule.action_requirement() != "sync")
                return false;

            if (-1 != rule.output_exclusivity_requirement() &&
                    rule.output_exclusivity_requirement() != i.output_exclusivity())
                return false;

            if (rule.matches_requirement())
                return false;

            if (! rule.ignore_unfetched_requirement().is_indeterminate())
                return false;

            return true;
        }

        bool visit(const CreateOutputManagerForPackageIDActionInfo & i) const
        {
            if (rule.type_requirement() != "*" && rule.type_requirement() != "package")
                return false;

            if (rule.name_requirement() != "*" && rule.name_requirement() != stringify(i.package_id()->name()))
                return false;

            if (rule.action_requirement() != "*" && rule.action_requirement() != action_to_string(i.action()))
                return false;

            if (-1 != rule.output_exclusivity_requirement() &&
                    rule.output_exclusivity_requirement() != i.output_exclusivity())
                return false;

            if (rule.matches_requirement() && ! match_package(*env, *rule.matches_requirement(),
                        *i.package_id(), MatchPackageOptions()))
                return false;

            if (! rule.ignore_unfetched_requirement().is_indeterminate())
            {
                const FetchAction * const fetch_action(simple_visitor_cast<const FetchAction>(i.action()));
                if (! fetch_action)
                    return false;
                if (fetch_action->options.ignore_unfetched() != rule.ignore_unfetched_requirement().is_true())
                    return false;
            }

            return true;
        }
    };

    bool match_rule(const Environment * const e, const Rule & rule, const CreateOutputManagerInfo & i)
    {
        MatchRuleVisitor v(e, rule);
        return i.accept_returning<bool>(v);
    }
}

void
OutputConf::add(const FSEntry & filename)
{
    Context context("When adding source '" + stringify(filename) + "' as an output file:");

    std::tr1::shared_ptr<LineConfigFile> f(make_bashable_conf(filename));
    if (! f)
        return;

    for (LineConfigFile::ConstIterator line(f->begin()), line_end(f->end()) ;
            line != line_end ; ++line)
    {
        std::vector<std::string> tokens;
        tokenise_whitespace_quoted(*line, std::back_inserter(tokens));

        if (tokens.empty())
            continue;

        if ("source" == tokens.at(0))
        {
            if (tokens.size() != 2)
                throw PaludisConfigError("Invalid source line '" + *line + "'");

            add(FSEntry(tokens.at(1)));
            continue;
        }

        Rule rule(make_named_values<Rule>(
                    value_for<n::action_requirement>("*"),
                    value_for<n::ignore_unfetched_requirement>(indeterminate),
                    value_for<n::manager>("unset"),
                    value_for<n::matches_requirement>(make_null_shared_ptr()),
                    value_for<n::name_requirement>("*"),
                    value_for<n::output_exclusivity_requirement>(static_cast<OutputExclusivity>(-1)),
                    value_for<n::type_requirement>("*")
                    ));

        std::vector<std::string>::const_iterator t(tokens.begin()), t_end(tokens.end());

        for ( ; t != t_end ; ++t)
        {
            if (*t == ":")
                break;

            std::string::size_type p(t->find("="));
            if (std::string::npos != p)
                set_rule(_imp->env, rule, t->substr(0, p), t->substr(p + 1));
            else
            {
                std::string r(*t);
                if (++t == t_end)
                    throw PaludisConfigError("Expected '=' but found end of line for line '" + *line + "'");

                if (*t != "=")
                    throw PaludisConfigError("Expected '=' but found '" + *t + "' for line '" + *line + "'");

                if (++t == t_end)
                    throw PaludisConfigError("Expected value but found end of for line '" + *line + "'");

                set_rule(_imp->env, rule, r, *t);
            }
        }

        if (t == t_end)
            throw PaludisConfigError("Found no ':' for line '" + *line + "'");

        if (++t == t_end)
            throw PaludisConfigError("Found no manager after ':' for line '" + *line + "'");

        rule.manager() = *t;
        _imp->rules.push_back(rule);

        if (++t != t_end)
            throw PaludisConfigError("Trailing text after manager on line '" + *line + "'");
    }
}

const std::tr1::shared_ptr<OutputManager>
OutputConf::create_output_manager(const CreateOutputManagerInfo & i) const
{
    Context context("When creating output manager:");

    for (RuleList::const_reverse_iterator r(_imp->rules.rbegin()), r_end(_imp->rules.rend()) ;
            r != r_end ; ++r)
        if (match_rule(_imp->env, *r, i))
            return _imp->env->create_named_output_manager(r->manager(), i);

    throw PaludisConfigError("No matching output manager rule specified");
}

template class PrivateImplementationPattern<paludis_environment::OutputConf>;