aboutsummaryrefslogtreecommitdiff
path: root/src/clients/adjutrix/downgrade_check.cc
blob: 7f3fc87192b5133b7e5862cc939ecfae11dfafd7 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * 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 "downgrade_check.hh"
#include "command_line.hh"
#include <paludis/query.hh>
#include <paludis/repositories/gentoo/portage_repository.hh>
#include <paludis/util/sr.hh>
#include <paludis/util/compare.hh>
#include <paludis/util/tokeniser.hh>
#include <fstream>
#include <iostream>
#include <map>

using namespace paludis;

namespace paludis
{
#include <src/clients/adjutrix/downgrade_check-sr.hh>
}

#include <src/clients/adjutrix/downgrade_check-sr.cc>

namespace
{
    int
    build_one_list(NoConfigEnvironment & env, std::ostream & f)
    {
        std::tr1::shared_ptr<const PackageDatabaseEntryCollection> matches(
                env.package_database()->query(query::NotMasked(), qo_group_by_slot));

        QualifiedPackageName old_package("dummy/dummy");
        SlotName old_slot("dummy");
        VersionSpec best_version("0");
        for (PackageDatabaseEntryCollection::Iterator m(matches->begin()), m_end(matches->end()) ;
                m != m_end ; ++m)
        {
            SlotName slot(env.package_database()->fetch_repository(m->repository)->version_metadata(
                        m->name, m->version)->slot);

            if (m->name != old_package || slot != old_slot)
            {
                f << old_package << " " << old_slot << " " << best_version << std::endl;

                old_package = m->name;
                old_slot = slot;
                best_version = m->version;
            }
        }

        f << old_package << " " << old_slot << " " << best_version << std::endl;

        return 0;
    }

    std::string
    make_filename(const PortageRepository::ProfilesIterator & p, bool unstable)
    {
        std::string result;
        FSEntry f(p->path);
        while (f.basename() != "profiles" && f != FSEntry("/"))
        {
            result = f.basename() + (result.empty() ? "" : "_" + result);
            f = f.dirname();
        }

        result.append(unstable ? ".unstable.txt" : ".stable.txt");

        return result;
    }

    std::string
    make_desc(const PortageRepository::ProfilesIterator & p, bool unstable)
    {
        std::string result;
        FSEntry f(p->path);
        while (f.basename() != "profiles" && f != FSEntry("/"))
        {
            result = f.basename() + (result.empty() ? "" : "/" + result);
            f = f.dirname();
        }

        result.append(unstable ? " (unstable)" : " (stable)");

        return result;
    }

    void
    load_list(std::map<QPNS, VersionSpec> & map, std::istream & f)
    {
        std::string s;
        while (std::getline(f, s))
        {
            std::vector<std::string> tokens;
            WhitespaceTokeniser::get_instance()->tokenise(s, std::back_inserter(tokens));
            if (tokens.size() != 3)
                throw ConfigurationError("Bad line '" + s + "'");

            map.insert(std::make_pair(QPNS(QualifiedPackageName(tokens.at(0)),
                            SlotName(tokens.at(1))), VersionSpec(tokens.at(2))));
        }
    }

    int
    check_one_list(NoConfigEnvironment & env, std::istream & f1,
            std::istream & f2, std::multimap<QPNS, std::string> & results,
            const std::string & desc)
    {
        int exit_status(0);

        std::map<QPNS, VersionSpec> before, after;

        load_list(before, f1);
        load_list(after, f2);

        for (std::map<QPNS, VersionSpec>::const_iterator b(before.begin()), b_end(before.end()) ;
                b != b_end ; ++b)
        {
            std::map<QPNS, VersionSpec>::const_iterator a(after.find(b->first));
            if (after.end() == a)
            {
                if (! env.package_database()->query(query::Matches(PackageDepSpec(
                                    std::tr1::shared_ptr<QualifiedPackageName>(new QualifiedPackageName(b->first.name)),
                                    std::tr1::shared_ptr<CategoryNamePart>(),
                                    std::tr1::shared_ptr<PackageNamePart>(),
                                    std::tr1::shared_ptr<VersionRequirements>(),
                                    vr_and,
                                    std::tr1::shared_ptr<SlotName>(new SlotName(b->first.slot)))),
                            qo_whatever)->empty())
                {
                    results.insert(std::make_pair(b->first, stringify(b->second) + " -> nothing on " + desc));
                    exit_status |= 2;
                }
            }
            else if (b->second > a->second)
            {
                results.insert(std::make_pair(b->first, stringify(b->second) + " -> " +
                            stringify(a->second) + " on " + desc));
                exit_status |= 4;
            }
        }

        return exit_status;
    }
}

int
do_build_downgrade_check_list(NoConfigEnvironment & env)
{
    int exit_status(0);

    FSEntry output_dir(*CommandLine::get_instance()->begin_parameters());
    if (! output_dir.mkdir())
        throw ConfigurationError("Output directory already exists");

    for (PortageRepository::ProfilesIterator p(env.portage_repository()->begin_profiles()),
            p_end(env.portage_repository()->end_profiles()) ; p != p_end ; ++p)
    {
        for (int i = 0 ; i < 2 ; ++i)
        {
            env.set_accept_unstable(i);
            env.portage_repository()->set_profile(p);
            std::string n(make_filename(p, i));
            std::cerr << "Generating " << n << "..." << std::endl;
            std::ofstream f(stringify(output_dir / n).c_str());
            exit_status |= build_one_list(env, f);
        }
    }

    return exit_status;
}

int
do_downgrade_check(NoConfigEnvironment & env)
{
    int exit_status(0);

    FSEntry before_dir(*CommandLine::get_instance()->begin_parameters());
    if (! before_dir.is_directory())
        throw ConfigurationError("First input directory is not a directory");

    FSEntry after_dir(*next(CommandLine::get_instance()->begin_parameters()));
    if (! after_dir.is_directory())
        throw ConfigurationError("Second input directory is not a directory");

    std::multimap<QPNS, std::string> results;

    for (PortageRepository::ProfilesIterator p(env.portage_repository()->begin_profiles()),
            p_end(env.portage_repository()->end_profiles()) ; p != p_end ; ++p)
    {
        for (int i = 0 ; i < 2 ; ++i)
        {
            env.set_accept_unstable(i);
            env.portage_repository()->set_profile(p);
            std::string n(make_filename(p, i)), desc(make_desc(p, i));

            if ((before_dir / n).exists() && (after_dir / n).exists())
            {
                std::cerr << "Checking " << n << "..." << std::endl;

                std::ifstream f1(stringify(before_dir / n).c_str());
                std::ifstream f2(stringify(after_dir / n).c_str());

                exit_status |= check_one_list(env, f1, f2, results, desc);
            }
            else
                std::cerr << "Skipping " << n << "..." << std::endl;
        }
    }

    QPNS old_qpns(QualifiedPackageName("dummy/dummmy"), SlotName("dummy"));
    for (std::multimap<QPNS, std::string>::const_iterator r(results.begin()), r_end(results.end()) ;
            r != r_end ; ++r)
    {
        if (old_qpns != r->first)
        {
            std::cout << r->first.name << " :" << r->first.slot << std::endl;
            old_qpns = r->first;
        }

        std::cout << "  " << r->second << std::endl;
    }

    return exit_status;
}