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

/*
 * Copyright (c) 2007, 2008, 2009, 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 "downgrade_check.hh"
#include "command_line.hh"
#include <paludis/package_database.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/named_value.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/package_id.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/selection.hh>
#include <paludis/metadata_key.hh>
#include <paludis/partially_made_package_dep_spec.hh>
#include <iostream>
#include <vector>
#include <map>

using namespace paludis;

namespace
{
    std::string slot_as_string(const PackageID & id)
    {
        if (id.slot_key())
            return stringify(id.slot_key()->value());
        else
            return "(none)";
    }

    int
    build_one_list(NoConfigEnvironment & env, std::ostream & f)
    {
        std::shared_ptr<const PackageIDSequence> matches(env[selection::AllVersionsGroupedBySlot(generator::All() | filter::NotMasked())]);

        QualifiedPackageName old_package("dummy/dummy");
        std::string old_slot("dummy");
        VersionSpec best_version("0", { });
        for (IndirectIterator<PackageIDSequence::ConstIterator> m(matches->begin()), m_end(matches->end()) ;
                m != m_end ; ++m)
        {
            if (m->name() != old_package || slot_as_string(*m) != old_slot)
            {
                f << old_package << " " << old_slot << " " << best_version << std::endl;

                old_package = m->name();
                old_slot = slot_as_string(*m);
                best_version = m->version();
            }
        }

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

        return 0;
    }

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

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

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

        std::map<std::pair<QualifiedPackageName, std::string>, VersionSpec> before, after;

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

        for (std::map<std::pair<QualifiedPackageName, std::string>, VersionSpec>::const_iterator
                b(before.begin()), b_end(before.end()) ;
                b != b_end ; ++b)
        {
            std::map<std::pair<QualifiedPackageName, std::string>, VersionSpec>::const_iterator
                a(after.find(b->first));
            if (after.end() == a)
            {
                PartiallyMadePackageDepSpec part_spec({ });
                part_spec.package(b->first.first);
                if ("(none)" != b->first.second)
                    part_spec.slot_requirement(std::make_shared<UserSlotExactRequirement>(SlotName(b->first.second)));
                if (! env[selection::SomeArbitraryVersion(generator::Matches(part_spec, { }))]->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);

    FSPath output_dir(*CommandLine::get_instance()->begin_parameters());
    if (! output_dir.mkdir(0755, { fspmkdo_ok_if_exists }))
        throw ConfigurationError("Output directory already exists");

    for (int i = 0 ; i < 2 ; ++i)
    {
        bool b(i);
        env.set_accept_unstable(b);
        std::cerr << "Generating " << (b ? "unstable" : "stable")  << "..." << std::endl;
        SafeOFStream f(output_dir / ((b ? "unstable" : "stable") + std::string(".txt")), -1, true);
        exit_status |= build_one_list(env, f);
    }

    return exit_status;
}

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

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

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

    std::multimap<std::pair<QualifiedPackageName, std::string>, std::string> results;

    for (int i = 0 ; i < 2 ; ++i)
    {
        bool b(i);
        env.set_accept_unstable(b);
        std::string f(b ? "unstable" : "stable");
        f.append(".txt");

        if ((before_dir / f).stat().exists() && (after_dir / f).stat().exists())
        {
            std::cerr << "Checking " << (b ? "unstable" : "stable") << "..." << std::endl;

            SafeIFStream f1(before_dir / f);
            SafeIFStream f2(after_dir / f);

            exit_status |= check_one_list(env, f1, f2, results, b ? "unstable" : "stable");
        }
        else
            std::cerr << "Skipping " << (b ? "unstable" : "stable") << "..." << std::endl;
    }

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

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

    return exit_status;
}