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

/*
 * Copyright (c) 2011, 2013 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/resolver/get_sameness.hh>

#include <paludis/util/log.hh>
#include <paludis/util/join.hh>

#include <paludis/package_id.hh>
#include <paludis/choice.hh>
#include <paludis/metadata_key.hh>
#include <paludis/version_spec.hh>
#include <paludis/unformatted_pretty_printer.hh>
#include <paludis/name.hh>
#include <paludis/dep_spec.hh>
#include <paludis/slot_requirement.hh>
#include <paludis/version_requirements.hh>

#include <set>
#include <algorithm>

using namespace paludis;
using namespace paludis::resolver;

namespace
{
    /* since the EAPI5 syntax for rewritten := deps in the VDB doesn't
       allow us to tell whether the dep was originally a := kind or
       a :slot= kind, normalise them all to the same thing to avoid
       spurious differences */
    struct SloppySlotStringifier
    {
        /* this is probably covered by the generic case that just
           does a stringify, but a package format could theoretically
           define a different representation, so assume the standard
           form here for compatibility with the other two that we're
           assuming */
        std::string visit(const SlotAnyAtAllLockedRequirement &) const
        {
            return ":=";
        }

        std::string visit(const SlotAnyPartialLockedRequirement &) const
        {
            return ":=";
        }

        std::string visit(const SlotUnknownRewrittenRequirement &) const
        {
            return ":=";
        }

        std::string visit(const SlotRequirement & r) const
        {
            return stringify(r);
        }
    };

    struct ComparingPrettyPrinter :
        UnformattedPrettyPrinter
    {
        using UnformattedPrettyPrinter::prettify;

        const std::string prettify(const PackageDepSpec & s) const
        {
            /* cat/pkg[foo][bar] and cat/pkg[bar][foo] are the same, and := deps
             * are weird */
            std::set<std::string> tokens;

            if (s.package_ptr())
                tokens.insert("package:" + stringify(*s.package_ptr()));

            if (s.package_name_part_ptr())
                tokens.insert("package_name_part:" + stringify(*s.package_name_part_ptr()));

            if (s.category_name_part_ptr())
                tokens.insert("category_name_part:" + stringify(*s.category_name_part_ptr()));

            if (s.version_requirements_ptr())
            {
                for (VersionRequirements::ConstIterator r(s.version_requirements_ptr()->begin()),
                        r_end(s.version_requirements_ptr()->end()) ; r != r_end ; ++r)
                    tokens.insert("version_requirement:" + stringify(s.version_requirements_mode()) +
                            stringify(r->version_operator()) + stringify(r->version_spec()));
            }

            if (s.slot_requirement_ptr())
            {
                auto r(s.slot_requirement_ptr()->maybe_original_requirement_if_rewritten());
                tokens.insert("slot_requirement:" + (r ? r : s.slot_requirement_ptr())
                    ->accept_returning<std::string>(SloppySlotStringifier()));
            }

            if (s.in_repository_ptr())
                tokens.insert("in_repository:" + stringify(*s.in_repository_ptr()));

            if (s.installable_to_repository_ptr())
                tokens.insert("installable_to_repository:" + stringify(s.installable_to_repository_ptr()->repository())
                        + "/" + stringify(s.installable_to_repository_ptr()->include_masked()));

            if (s.from_repository_ptr())
                tokens.insert("from_repository:" + stringify(*s.from_repository_ptr()));

            if (s.installed_at_path_ptr())
                tokens.insert("installed_at_path:" + stringify(*s.installed_at_path_ptr()));

            if (s.installable_to_path_ptr())
                tokens.insert("installable_to_path:" + stringify(s.installable_to_path_ptr()->path())
                        + "/" + stringify(s.installable_to_path_ptr()->include_masked()));

            if (s.additional_requirements_ptr())
            {
                for (AdditionalPackageDepSpecRequirements::ConstIterator u(s.additional_requirements_ptr()->begin()),
                        u_end(s.additional_requirements_ptr()->end()) ; u != u_end ; ++u)
                    tokens.insert("additional_requirement:" + stringify(**u));
            }

            return "PackageDepSpec(" + join(tokens.begin(), tokens.end(), ";") + ")";
        }
    };

    bool is_same_dependencies(
        const std::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> > & a,
        const std::shared_ptr<const MetadataSpecTreeKey<DependencySpecTree> > & b)
    {
        if (! a)
            return ! b;
        else if (! b)
            return false;

        auto sa(a->pretty_print_value(ComparingPrettyPrinter(), { }));
        auto sb(b->pretty_print_value(ComparingPrettyPrinter(), { }));

        if (sa != sb)
            Log::get_instance()->message("resolver.get_sameness", ll_debug, lc_context) <<
                "Not same: [" << sa << "] [" << sb << "]";

        return sa == sb;
    }
}

ExistingPackageIDAttributes
paludis::resolver::get_sameness(
        const std::shared_ptr<const PackageID> & existing_id,
        const std::shared_ptr<const PackageID> & installable_id)
{
    bool is_same_version(existing_id->version() == installable_id->version());
    bool is_same(false);
    bool is_same_metadata(false);

    if (is_same_version)
    {
        is_same = true;
        is_same_metadata = true;

        std::set<ChoiceNameWithPrefix> common;
        std::shared_ptr<const Choices> installable_choices;
        std::shared_ptr<const Choices> existing_choices;

        if (existing_id->choices_key() && installable_id->choices_key())
        {
            installable_choices = installable_id->choices_key()->parse_value();
            existing_choices = existing_id->choices_key()->parse_value();

            std::set<ChoiceNameWithPrefix> i_common, u_common;
            for (Choices::ConstIterator k(installable_choices->begin()), k_end(installable_choices->end()) ;
                    k != k_end ; ++k)
            {
                if (! (*k)->consider_added_or_changed())
                    continue;

                for (Choice::ConstIterator i((*k)->begin()), i_end((*k)->end()) ;
                        i != i_end ; ++i)
                    if (co_explicit == (*i)->origin())
                        i_common.insert((*i)->name_with_prefix());
            }

            for (Choices::ConstIterator k(existing_choices->begin()), k_end(existing_choices->end()) ;
                    k != k_end ; ++k)
            {
                if (! (*k)->consider_added_or_changed())
                    continue;

                for (Choice::ConstIterator i((*k)->begin()), i_end((*k)->end()) ;
                        i != i_end ; ++i)
                    if (co_explicit == (*i)->origin())
                        u_common.insert((*i)->name_with_prefix());
            }

            std::set_intersection(
                    i_common.begin(), i_common.end(),
                    u_common.begin(), u_common.end(),
                    std::inserter(common, common.begin()));
        }

        for (std::set<ChoiceNameWithPrefix>::const_iterator f(common.begin()), f_end(common.end()) ;
                f != f_end ; ++f)
            if (installable_choices->find_by_name_with_prefix(*f)->enabled() !=
                    existing_choices->find_by_name_with_prefix(*f)->enabled())
            {
                is_same = false;
                is_same_metadata = false;
                break;
            }
    }

    if (is_same_metadata)
    {
        is_same_metadata = is_same_metadata && is_same_dependencies(existing_id->build_dependencies_key(), installable_id->build_dependencies_key());
        is_same_metadata = is_same_metadata && is_same_dependencies(existing_id->run_dependencies_key(), installable_id->run_dependencies_key());
        is_same_metadata = is_same_metadata && is_same_dependencies(existing_id->post_dependencies_key(), installable_id->post_dependencies_key());
        is_same_metadata = is_same_metadata && is_same_dependencies(existing_id->dependencies_key(), installable_id->dependencies_key());
    }

    ExistingPackageIDAttributes attrs;
    if (is_same_version) attrs += epia_is_same_version;
    if (is_same) attrs += epia_is_same;
    if (is_same_metadata) attrs += epia_is_same_metadata;

    return attrs;
}