aboutsummaryrefslogtreecommitdiff
path: root/src/clients/instruo/instruo.cc
blob: b8f8e1c4905d6594e727e3ea07df613e2f40f5b3 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 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 <output/colour.hh>
#include <paludis/args/do_help.hh>
#include "command_line.hh"
#include <paludis/about.hh>
#include <paludis/action.hh>
#include <paludis/package_id.hh>
#include <paludis/util/system.hh>
#include <paludis/util/join.hh>
#include <paludis/util/log.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/map.hh>
#include <paludis/util/tr1_functional.hh>
#include <paludis/util/visitor-impl.hh>
#include <paludis/util/visitor_cast.hh>
#include <paludis/util/set.hh>
#include <paludis/environments/no_config/no_config_environment.hh>
#include <paludis/package_database.hh>
#include <paludis/query.hh>
#include <paludis/metadata_key.hh>
#include <iostream>
#include <fstream>
#include <map>

using namespace paludis;
using std::cout;
using std::cerr;
using std::endl;

namespace
{
    struct KeyValidator :
        ConstVisitor<MetadataKeyVisitorTypes>
    {
        void visit(const MetadataStringKey & k)
        {
            const std::string & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataSizeKey & k)
        {
            long PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataPackageIDKey & k)
        {
            const tr1::shared_ptr<const PackageID> & PALUDIS_ATTRIBUTE((unused)) p(k.value());
        }

        void visit(const MetadataTimeKey & k)
        {
            time_t PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataContentsKey & k)
        {
            const tr1::shared_ptr<const Contents> & PALUDIS_ATTRIBUTE((unused)) c(k.value());
        }

        void visit(const MetadataFSEntryKey & k)
        {
            const FSEntry & PALUDIS_ATTRIBUTE((unused)) c(k.value());
        }

        void visit(const MetadataRepositoryMaskInfoKey & k)
        {
            const tr1::shared_ptr<const RepositoryMaskInfo> & PALUDIS_ATTRIBUTE((unused)) i(k.value());
        }

        void visit(const MetadataSpecTreeKey<RestrictSpecTree> & k)
        {
            const tr1::shared_ptr<RestrictSpecTree::ConstItem> & PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataSpecTreeKey<ProvideSpecTree> & k)
        {
            const tr1::shared_ptr<ProvideSpecTree::ConstItem> & PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataSpecTreeKey<FetchableURISpecTree> & k)
        {
            const tr1::shared_ptr<FetchableURISpecTree::ConstItem> & PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataSpecTreeKey<SimpleURISpecTree> & k)
        {
            const tr1::shared_ptr<SimpleURISpecTree::ConstItem> & PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataSpecTreeKey<LicenseSpecTree> & k)
        {
            const tr1::shared_ptr<LicenseSpecTree::ConstItem> & PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataSpecTreeKey<DependencySpecTree> & k)
        {
            const tr1::shared_ptr<DependencySpecTree::ConstItem> & PALUDIS_ATTRIBUTE((unused)) t(k.value());
        }

        void visit(const MetadataCollectionKey<PackageIDSequence> & k)
        {
            const tr1::shared_ptr<const PackageIDSequence> & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataCollectionKey<Set<std::string> > & k)
        {
            const tr1::shared_ptr<const Set<std::string> > & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataCollectionKey<FSEntrySequence> & k)
        {
            const tr1::shared_ptr<const FSEntrySequence> & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataCollectionKey<KeywordNameSet> & k)
        {
            const tr1::shared_ptr<const KeywordNameSet> & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataCollectionKey<IUseFlagSet> & k)
        {
            const tr1::shared_ptr<const IUseFlagSet> & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataCollectionKey<UseFlagNameSet> & k)
        {
            const tr1::shared_ptr<const UseFlagNameSet> & PALUDIS_ATTRIBUTE((unused)) s(k.value());
        }

        void visit(const MetadataSectionKey & k)
        {
            std::for_each(indirect_iterator(k.begin_metadata()),
                    indirect_iterator(k.end_metadata()), accept_visitor(*this));
        }
    };
}

int
main(int argc, char *argv[])
{
    std::string options(paludis::getenv_with_default("INSTRUO_OPTIONS", ""));
    if (! options.empty())
        options = "(" + options + ") ";
    options += join(argv + 1, argv + argc, " ");

    Context context(std::string("In program ") + argv[0] + " " + options + ":");

    try
    {
        CommandLine::get_instance()->run(argc, argv, "instruo", "INSTRUO_OPTIONS", "INSTRUO_CMDLINE");
        set_use_colour(
                (! CommandLine::get_instance()->a_no_color.specified()) &&
                (! CommandLine::get_instance()->a_report_file.specified()));

        if (CommandLine::get_instance()->a_help.specified())
            throw args::DoHelp();

        if (CommandLine::get_instance()->a_log_level.specified())
            Log::get_instance()->set_log_level(CommandLine::get_instance()->a_log_level.option());
        else
            Log::get_instance()->set_log_level(ll_qa);

        if (1 < (
                    CommandLine::get_instance()->a_generate_cache.specified() +
                    CommandLine::get_instance()->a_version.specified()
                ))
            throw args::DoHelp("you should specify exactly one action");

        if (! CommandLine::get_instance()->a_repository_directory.specified())
            CommandLine::get_instance()->a_repository_directory.set_argument(stringify(FSEntry::cwd()));

        if (CommandLine::get_instance()->a_version.specified())
        {
            cout << "instruo, part of " << PALUDIS_PACKAGE << " " << PALUDIS_VERSION_MAJOR << "."
                << PALUDIS_VERSION_MINOR << "." << PALUDIS_VERSION_MICRO << PALUDIS_VERSION_SUFFIX;
            if (! std::string(PALUDIS_SUBVERSION_REVISION).empty())
                cout << " svn " << PALUDIS_SUBVERSION_REVISION;
            cout << endl << endl;
            cout << "Paludis comes with ABSOLUTELY NO WARRANTY. Paludis is free software, and you" << endl;
            cout << "are welcome to redistribute it under the terms of the GNU General Public" << endl;
            cout << "License, version 2." << endl;

            return EXIT_SUCCESS;
        }

        if ((
                    CommandLine::get_instance()->a_repository_directory.specified() +
                    CommandLine::get_instance()->a_output_directory.specified()
            ) < 1)
            throw args::DoHelp("at least one of '--" + CommandLine::get_instance()->a_repository_directory.long_name() + "' or '--"
                    + CommandLine::get_instance()->a_output_directory.long_name() + "' must be specified");

        if (! CommandLine::get_instance()->a_master_repository_dir.specified())
            CommandLine::get_instance()->a_master_repository_dir.set_argument("/var/empty");

        if (! CommandLine::get_instance()->a_output_directory.specified())
            CommandLine::get_instance()->a_output_directory.set_argument(stringify(FSEntry::cwd()));

        tr1::shared_ptr<Map<std::string, std::string> > keys(new Map<std::string, std::string>);
        keys->insert("append_repository_name_to_write_cache", "false");
        NoConfigEnvironment env(no_config_environment::Params::create()
                .repository_dir(CommandLine::get_instance()->a_repository_directory.argument())
                .write_cache(CommandLine::get_instance()->a_output_directory.argument())
                .accept_unstable(true)
                .repository_type(no_config_environment::ncer_ebuild)
                .disable_metadata_cache(true)
                .extra_params(keys)
                .master_repository_dir(FSEntry(CommandLine::get_instance()->a_master_repository_dir.argument())));

        tr1::shared_ptr<const PackageIDSequence> ids(
                env.package_database()->query(query::Repository(env.main_repository()->name()), qo_order_by_version));
        PackageIDComparator comparator(env.package_database().get());
        std::multimap<tr1::shared_ptr<const PackageID>, std::string, tr1::reference_wrapper<const PackageIDComparator> >
            results(tr1::cref(comparator));
        unsigned success(0), total(0);

        CategoryNamePart old_cat("OLDCAT");
        for (PackageIDSequence::ConstIterator i(ids->begin()), i_end(ids->end()) ;
                i != i_end ; ++i)
        {
            Context i_context("When fetching ID '" + stringify(**i) + "':");

            if ((*i)->name().category != old_cat)
            {
                cout << "Processing " << colour(cl_package_name, stringify((*i)->name().category)) << "..." << endl;
                old_cat = (*i)->name().category;
            }

            ++total;

            try
            {
                PackageID::MetadataConstIterator eapi_i((*i)->find_metadata("EAPI"));
                if ((*i)->end_metadata() == eapi_i)
                {
                    results.insert(std::make_pair(*i, "No EAPI metadata key"));
                    continue;
                }

                if (! visitor_cast<const MetadataStringKey>(**eapi_i))
                {
                    results.insert(std::make_pair(*i, "EAPI metadata key is not a string key"));
                    continue;
                }

                if (visitor_cast<const MetadataStringKey>(**eapi_i)->value() == "UNKNOWN")
                {
                    results.insert(std::make_pair(*i, "EAPI is 'UNKNOWN'"));
                    continue;
                }

                bool metadata_errors(false);
                KeyValidator v;
                for (PackageID::MetadataConstIterator m((*i)->begin_metadata()),
                         m_end((*i)->end_metadata()); m_end != m; ++m)
                {
                    try
                    {
                        (*m)->accept(v);
                    }
                    catch (const Exception & e)
                    {
                        results.insert(std::make_pair(*i, "Error in metadata key '" + (*m)->raw_name() + "': '" + e.message() +
                                    "' (" + e.what() + ")"));
                        metadata_errors = true;
                    }
                }

                if (! metadata_errors)
                    ++success;
            }
            catch (const Exception & e)
            {
                results.insert(std::make_pair(*i, "Uncaught exception '" + e.message() + "' (" + e.what() + ")"));
            }
        }

        std::cout << std::endl;

        tr1::shared_ptr<std::ofstream> outf;
        if (CommandLine::get_instance()->a_report_file.specified())
        {
            outf.reset(new std::ofstream(CommandLine::get_instance()->a_report_file.argument().c_str()));
            if (! *outf)
            {
                std::cerr << "Cannot write to " << CommandLine::get_instance()->a_report_file.argument() << std::endl;
                return EXIT_FAILURE;
            }
        }

        std::ostream & out(outf ? *outf : cout);

        char t[255];
        time_t tt(time(0));
        if (0 == strftime(t, 255, "%c", gmtime(&tt)))
            throw InternalError(PALUDIS_HERE, "strftime failed");

        out << colour(cl_heading, "Instruo results for ") << colour(cl_repository_name, env.main_repository()->name())
            << colour(cl_heading, " on " + stringify(t) + ":") << endl << endl
            << total << " IDs, " << success << " successes, " << (total - success) << " failures" << endl << endl;

        int exit_status(0);
        tr1::shared_ptr<const PackageID> old_id;
        for (std::multimap<tr1::shared_ptr<const PackageID>, std::string, tr1::reference_wrapper<const PackageIDComparator> >::const_iterator
                r(results.begin()), r_end(results.end()) ; r != r_end ; ++r)
        {
            exit_status |= 1;
            if ((! old_id) || (*old_id != *r->first))
            {
                out << colour(cl_package_name, stringify(*r->first)) << ":" << endl;
                old_id = r->first;
            }
            out << "  " << r->second << endl;
        }
        out << endl;

        return exit_status;
    }
    catch (const paludis::args::ArgsError & e)
    {
        cerr << "Usage error: " << e.message() << endl;
        cerr << "Try " << argv[0] << " --help" << endl;
        return EXIT_FAILURE;
    }
    catch (const args::DoHelp & h)
    {
        if (h.message.empty())
        {
            cout << "Usage: " << argv[0] << " [options]" << endl;
            cout << endl;
            cout << *CommandLine::get_instance();
            return EXIT_SUCCESS;
        }
        else
        {
            cerr << "Usage error: " << h.message << endl;
            cerr << "Try " << argv[0] << " --help" << endl;
            return EXIT_FAILURE;
        }
    }
    catch (const Exception & e)
    {
        cout << endl;
        cerr << "Unhandled exception:" << endl
            << "  * " << e.backtrace("\n  * ")
            << e.message() << " (" << e.what() << ")" << endl;
        return EXIT_FAILURE;
    }
    catch (const std::exception & e)
    {
        cout << endl;
        cerr << "Unhandled exception:" << endl
            << "  * " << e.what() << endl;
        return EXIT_FAILURE;
    }
    catch (...)
    {
        cout << endl;
        cerr << "Unhandled exception:" << endl
            << "  * Unknown exception type. Ouch..." << endl;
        return EXIT_FAILURE;
    }
}