aboutsummaryrefslogtreecommitdiff
path: root/paludis/repositories/e/required_use_verifier.cc
blob: f39633007e0385e3ba827f334fa74263f9b53e17 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010 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/repositories/e/required_use_verifier.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/accept_visitor.hh>
#include <paludis/util/save.hh>
#include <paludis/util/log.hh>
#include <paludis/action.hh>
#include <paludis/metadata_key.hh>
#include <paludis/choice.hh>
#include <paludis/stringify_formatter.hh>
#include <list>
#include <algorithm>

using namespace paludis;
using namespace paludis::erepository;

namespace
{
    struct Met
    {
        int number_met;
        bool any_unmet;
    };
}

namespace paludis
{
    template <>
    struct Imp<RequiredUseVerifier>
    {
        const Environment * const env;
        const std::shared_ptr<const ERepositoryID> id;
        std::shared_ptr<Sequence<std::string> > unmet_requirements;

        std::list<Met> stack;
        bool top;

        Imp(
                const Environment * const e,
                const std::shared_ptr<const ERepositoryID> & i) :
            env(e),
            id(i),
            unmet_requirements(std::make_shared<Sequence<std::string>>()),
            top(true)
        {
            stack.push_front(Met{0, false});
        }
    };
}

RequiredUseVerifier::RequiredUseVerifier(
        const Environment * const e,
        const std::shared_ptr<const ERepositoryID> & id) :
    Pimp<RequiredUseVerifier>(e, id)
{
}

RequiredUseVerifier::~RequiredUseVerifier() = default;

bool
RequiredUseVerifier::matches(const std::string & s)
{
    if (s.empty())
        throw ActionFailedError("Could not verify empty use requirement");

    if ('!' == s.at(0))
        return ! matches(s.substr(1));

    if (! _imp->id->choices_key())
    {
        Log::get_instance()->message("e.required_use.no_choices", ll_warning, lc_context)
            << "ID '" << *_imp->id << "' has no choices, so cannot check that required use constraint '" << s << "' matches";
        return false;
    }

    auto c(_imp->id->choices_key()->value()->find_by_name_with_prefix(ChoiceNameWithPrefix(s)));
    if (! c)
    {
        Log::get_instance()->message("e.required_use.no_choice", ll_warning, lc_context)
            << "ID '" << *_imp->id << "' has no choice named '" << s << "'', so cannot check that required use constraint '" << s << "' matches";
        return false;
    }

    return c->enabled();
}

void
RequiredUseVerifier::visit(const RequiredUseSpecTree::NodeType<PlainTextDepSpec>::Type & node)
{
    if (matches(node.spec()->text()))
        ++_imp->stack.begin()->number_met;
    else
        _imp->stack.begin()->any_unmet = true;
}

void
RequiredUseVerifier::visit(const RequiredUseSpecTree::NodeType<AllDepSpec>::Type & node)
{
    _imp->stack.push_front(Met{0, false});
    {
        Save<bool> top(&_imp->top, false);
        std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));
    }

    Met met(*_imp->stack.begin());
    _imp->stack.pop_front();

    if (met.any_unmet)
        _imp->stack.begin()->any_unmet = true;
    else
        ++_imp->stack.begin()->number_met;

    if (_imp->top)
    {
        if (_imp->stack.begin()->any_unmet)
            _imp->unmet_requirements->push_back(_imp->id->required_use_key()->pretty_print_flat(StringifyFormatter()));
    }
}

void
RequiredUseVerifier::visit(const RequiredUseSpecTree::NodeType<AnyDepSpec>::Type & node)
{
    _imp->stack.push_front(Met{0, false});
    std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));

    Met met(*_imp->stack.begin());
    _imp->stack.pop_front();

    if (met.number_met > 0)
        ++_imp->stack.begin()->number_met;
    else if (met.any_unmet)
        _imp->stack.begin()->any_unmet = true;
    else
    {
        /* || ( disabled? ( bar ) ) and || ( ) are true. yay Portage! */
        ++_imp->stack.begin()->number_met;
    }
}

void
RequiredUseVerifier::visit(const RequiredUseSpecTree::NodeType<ExactlyOneDepSpec>::Type & node)
{
    _imp->stack.push_front(Met{0, false});
    std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));

    Met met(*_imp->stack.begin());
    _imp->stack.pop_front();

    if (met.number_met == 1)
        ++_imp->stack.begin()->number_met;
    else if (met.number_met > 1)
        _imp->stack.begin()->any_unmet = true;
    else if (met.any_unmet)
        _imp->stack.begin()->any_unmet = true;
    else
        ++_imp->stack.begin()->number_met;
}

void
RequiredUseVerifier::visit(const RequiredUseSpecTree::NodeType<ConditionalDepSpec>::Type & node)
{
    if (! node.spec()->condition_met())
        return;

    _imp->stack.push_front(Met{0, false});
    std::for_each(indirect_iterator(node.begin()), indirect_iterator(node.end()), accept_visitor(*this));

    Met met(*_imp->stack.begin());
    _imp->stack.pop_front();

    if (met.any_unmet)
        _imp->stack.begin()->any_unmet = true;
    else
        ++_imp->stack.begin()->number_met;
}

const std::shared_ptr<const Sequence<std::string> >
RequiredUseVerifier::unmet_requirements() const
{
    return _imp->unmet_requirements;
}

template class Pimp<RequiredUseVerifier>;