aboutsummaryrefslogtreecommitdiff
path: root/src/clients/cave/cmd_display_resolution.cc
blob: cdb983550e84513d3c4c1948be77bfe11bf224b6 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009 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 "cmd_display_resolution.hh"
#include "cmd_resolve_cmdline.hh"
#include "exceptions.hh"
#include "command_command_line.hh"
#include "formats.hh"
#include "colour_formatter.hh"
#include <paludis/args/do_help.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/system.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/join.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/options.hh>
#include <paludis/util/set.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/resolver/resolutions.hh>
#include <paludis/resolver/serialise.hh>
#include <paludis/resolver/reason.hh>
#include <paludis/resolver/sanitised_dependencies.hh>
#include <paludis/resolver/resolution.hh>
#include <paludis/resolver/decision.hh>
#include <paludis/resolver/destinations.hh>
#include <paludis/resolver/constraint.hh>
#include <paludis/resolver/resolver.hh>
#include <paludis/resolver/qpn_s.hh>
#include <paludis/package_id.hh>
#include <paludis/version_spec.hh>
#include <paludis/metadata_key.hh>
#include <paludis/choice.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/match_package.hh>
#include <paludis/repository.hh>
#include <paludis/package_dep_spec_properties.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/selection.hh>
#include <paludis/environment.hh>

#include <set>
#include <iterator>
#include <iostream>
#include <cstdlib>

using namespace paludis;
using namespace cave;
using namespace paludis::resolver;

using std::cout;
using std::endl;

namespace
{
    struct DisplayResolutionCommandLine :
        CaveCommandCommandLine
    {
        ResolveCommandLineDisplayOptions display_options;

        DisplayResolutionCommandLine() :
            display_options(this)
        {
            add_environment_variable("PALUDIS_SERIALISED_RESOLUTION_FD",
                    "The file descriptor on which the serialised resolution can be found.");
        }

        virtual std::string app_name() const
        {
            return "cave display-resolution";
        }

        virtual std::string app_synopsis() const
        {
            return "Displays a dependency resolution created using 'cave resolve'.";
        }

        virtual std::string app_description() const
        {
            return "Displays a dependency resolution created using 'cave resolve'. Mostly for "
                "internal use; most users will not use this command directly.";
        }
    };

    struct ReasonNameGetter
    {
        const bool verbose;

        ReasonNameGetter(const bool v) :
            verbose(v)
        {
        }

        std::pair<std::string, bool> visit(const DependencyReason & r) const
        {
            if (r.sanitised_dependency().spec().if_block())
                return std::make_pair(stringify(*r.sanitised_dependency().spec().if_block())
                        + " from " + (verbose ? stringify(*r.from_id()) : stringify(r.from_id()->name())),
                        true);
            else
            {
                if (verbose)
                {
                    std::string as;
                    if (r.sanitised_dependency().any_of_alternatives())
                        as = " (chosen from || ( " + join(r.sanitised_dependency().any_of_alternatives()->begin(),
                                        r.sanitised_dependency().any_of_alternatives()->end(), " ") + " ) )";

                    return std::make_pair(stringify(*r.sanitised_dependency().spec().if_package())
                            + " from " + stringify(*r.from_id()) + ", key '"
                            + r.sanitised_dependency().metadata_key_human_name() + "'"
                            + (r.sanitised_dependency().active_dependency_labels_as_string().empty() ? "" :
                                ", labelled '" + r.sanitised_dependency().active_dependency_labels_as_string() + "'")
                            + as,
                            false);
                }
                else
                    return std::make_pair(stringify(r.from_id()->name()), false);
            }
        }

        std::pair<std::string, bool> visit(const TargetReason &) const
        {
            return std::make_pair("target", true);
        }

        std::pair<std::string, bool> visit(const SetReason & r) const
        {
            std::pair<std::string, bool> rr(r.reason_for_set()->accept_returning<std::pair<std::string, bool> >(*this));
            return std::make_pair(rr.first + " (" + stringify(r.set_name()) + ")", rr.second);
        }

        std::pair<std::string, bool> visit(const PresetReason &) const
        {
            return std::make_pair("", false);
        }
    };

    void display_reasons(
            const std::tr1::shared_ptr<const Resolution> & resolution,
            const bool verbose)
    {
        std::set<std::string> reason_names, special_reason_names;
        for (Constraints::ConstIterator r(resolution->constraints()->begin()),
                r_end(resolution->constraints()->end()) ;
                r != r_end ; ++r)
        {
            ReasonNameGetter g(verbose);
            std::pair<std::string, bool> s((*r)->reason()->accept_returning<std::pair<std::string, bool> >(g));
            if (! s.first.empty())
            {
                if (s.second)
                    special_reason_names.insert(s.first);
                else
                    reason_names.insert(s.first);
            }
        }

        if ((! special_reason_names.empty()) || (! reason_names.empty()))
        {
            if (verbose)
            {
                cout << "    Because of" << endl;
                if (! special_reason_names.empty())
                    cout << "        * " << c::bold_yellow() << join(special_reason_names.begin(),
                            special_reason_names.end(), c::normal() + "\n        * " + c::bold_yellow())
                        << c::normal() << endl;

                if (! reason_names.empty())
                    cout << "        * " << join(reason_names.begin(), reason_names.end(), "\n        * ")
                        << endl;
            }
            else
            {
                cout << "    Because of ";

                if (! special_reason_names.empty())
                    cout << c::bold_yellow() << join(special_reason_names.begin(), special_reason_names.end(), ", ")
                        << c::normal();

                if (! reason_names.empty())
                {
                    if (! special_reason_names.empty())
                        cout << ", ";

                    if (reason_names.size() > 4)
                        cout << join(reason_names.begin(), next(reason_names.begin(), 3), ", ")
                            << ", " << (reason_names.size() - 3) << " more";
                    else
                        cout << join(reason_names.begin(), reason_names.end(), ", ");
                }

                cout << endl;
            }

            if ((! resolution->decision()->is_best()) && resolution->decision()->kind() == dk_installable)
                cout << c::bold_red() << "    Which prevented selection of the best candidate" << c::normal() << endl;
        }
    }

    void display_resolution(
            const std::tr1::shared_ptr<Environment> & env,
            const ResolutionLists & lists,
            const DisplayResolutionCommandLine &)
    {
        Context context("When displaying chosen resolution:");

        if (lists.ordered()->empty())
        {
            if (lists.errors()->empty())
                cout << "There are no actions to carry out" << endl << endl;
            return;
        }

        cout << "These are the actions I will take, in order:" << endl << endl;

        for (Resolutions::ConstIterator c(lists.ordered()->begin()), c_end(lists.ordered()->end()) ;
                c != c_end ; ++c)
        {
            const std::tr1::shared_ptr<const PackageID> id((*c)->decision()->if_package_id());
            if (! id)
                throw InternalError(PALUDIS_HERE, "why did that happen?");

            bool is_new(false), is_upgrade(false), is_downgrade(false), is_reinstall(false),
                 other_slots(false);
            std::tr1::shared_ptr<const PackageID> old_id;

            if ((*c)->destinations()->slash())
            {
                if ((*c)->destinations()->slash()->replacing()->empty())
                {
                    is_new = true;
                    const std::tr1::shared_ptr<const PackageIDSequence> others((*env)[selection::SomeArbitraryVersion(
                            generator::Package(id->name()) &
                            generator::InRepository((*c)->destinations()->slash()->repository())
                            )]);
                    other_slots = ! others->empty();
                }
                else
                    for (PackageIDSequence::ConstIterator x((*c)->destinations()->slash()->replacing()->begin()),
                            x_end((*c)->destinations()->slash()->replacing()->end()) ;
                            x != x_end ; ++x)
                    {
                        old_id = *x;
                        if ((*x)->version() == id->version())
                            is_reinstall = true;
                        else if ((*x)->version() < id->version())
                            is_upgrade = true;
                        else if ((*x)->version() > id->version())
                            is_downgrade = true;
                    }

                /* pick the worst of what it is */
                is_upgrade = is_upgrade && (! is_reinstall) && (! is_downgrade);
                is_reinstall = is_reinstall && (! is_downgrade);
            }

            if (is_new)
            {
                if (other_slots)
                    cout << "[s] " << c::bold_blue() << id->canonical_form(idcf_no_version);
                else
                    cout << "[n] " << c::bold_blue() << id->canonical_form(idcf_no_version);
            }
            else if (is_upgrade)
                cout << "[u] " << c::blue() << id->canonical_form(idcf_no_version);
            else if (is_reinstall)
                cout << "[r] " << c::yellow() << id->canonical_form(idcf_no_version);
            else if (is_downgrade)
                cout << "[d] " << c::bold_yellow() << id->canonical_form(idcf_no_version);
            else
                throw InternalError(PALUDIS_HERE, "why did that happen?");

            cout << c::normal() << " " << id->canonical_form(idcf_version);

            if ((*c)->destinations()->slash())
            {
                cout << " to ::" << (*c)->destinations()->slash()->repository();
                if (! (*c)->destinations()->slash()->replacing()->empty())
                {
                    cout << " replacing";
                    bool first(true);
                    for (PackageIDSequence::ConstIterator x((*c)->destinations()->slash()->replacing()->begin()),
                            x_end((*c)->destinations()->slash()->replacing()->end()) ;
                            x != x_end ; ++x)
                    {
                        bool different(false);
                        std::string old_from;
                        if ((*x)->from_repositories_key())
                        {
                            for (Set<std::string>::ConstIterator k((*x)->from_repositories_key()->value()->begin()),
                                    k_end((*x)->from_repositories_key()->value()->end()) ;
                                    k != k_end ; ++k)
                            {
                                if (stringify(id->repository()->name()) != *k)
                                {
                                    if (id->from_repositories_key() && (id->from_repositories_key()->value()->end() !=
                                                id->from_repositories_key()->value()->find(*k)))
                                    {
                                    }
                                    else
                                        different = true;
                                }

                                if (old_from.empty())
                                    old_from = " from ::";
                                else
                                    old_from.append(", ::");

                                old_from.append(*k);
                            }
                        }

                        if (! first)
                            cout << ", ";
                        else
                            cout << " ";
                        first = false;

                        cout << (*x)->canonical_form(idcf_version);
                        if (different)
                            cout << old_from;
                    }
                }
            }

            cout << endl;

            if (id->choices_key())
            {
                ColourFormatter formatter(0);

                std::tr1::shared_ptr<const Choices> old_choices;
                if (old_id && old_id->choices_key())
                    old_choices = old_id->choices_key()->value();

                bool non_blank_prefix(false);
                std::string s;
                for (Choices::ConstIterator k(id->choices_key()->value()->begin()),
                        k_end(id->choices_key()->value()->end()) ;
                        k != k_end ; ++k)
                {
                    if ((*k)->hidden())
                        continue;

                    bool shown_prefix(false);
                    for (Choice::ConstIterator i((*k)->begin()), i_end((*k)->end()) ;
                            i != i_end ; ++i)
                    {
                        if (! (*i)->explicitly_listed())
                            continue;

                        if (! shown_prefix)
                        {
                            if (non_blank_prefix || ! (*k)->show_with_no_prefix())
                            {
                                shown_prefix = true;
                                if (! s.empty())
                                    s.append(" ");
                                s.append((*k)->raw_name() + ":");
                            }
                        }

                        if (! s.empty())
                            s.append(" ");

                        std::string t;
                        if ((*i)->enabled())
                        {
                            if ((*i)->locked())
                                t = formatter.format(**i, format::Forced());
                            else
                                t = formatter.format(**i, format::Enabled());
                        }
                        else
                        {
                            if ((*i)->locked())
                                t = formatter.format(**i, format::Masked());
                            else
                                t = formatter.format(**i, format::Disabled());
                        }

                        bool changed(false), added(false);
                        if ((*k)->consider_added_or_changed())
                        {
                            if (old_choices)
                            {
                                std::tr1::shared_ptr<const ChoiceValue> old_choice(
                                        old_choices->find_by_name_with_prefix((*i)->name_with_prefix()));
                                if (! old_choice)
                                    added = true;
                                else if (old_choice->enabled() != (*i)->enabled())
                                    changed = true;
                            }
                            else
                                added = true;
                        }

                        if (changed)
                        {
                            t = formatter.decorate(**i, t, format::Changed());
                        }
                        else if (added)
                        {
                            if (old_id)
                                t = formatter.decorate(**i, t, format::Added());
                        }

                        s.append(t);
                    }
                }

                if (s.empty())
                    break;

                cout << "    " << s << endl;
            }

            if (id->short_description_key())
                cout << "    \"" << id->short_description_key()->value() << "\"" << endl;

            display_reasons(*c, false);
        }

        cout << endl;
    }

    void display_errors(
            const std::tr1::shared_ptr<Environment> &,
            const ResolutionLists & lists,
            const DisplayResolutionCommandLine &)
    {
        Context context("When displaying errors for chosen resolution:");

        if (lists.errors()->empty())
            return;

        cout << "I encountered the following errors:" << endl << endl;

        for (Resolutions::ConstIterator c(lists.errors()->begin()), c_end(lists.errors()->end()) ;
                c != c_end ; ++c)
        {
            if ((*c)->decision()->kind() != dk_unable_to_decide)
                continue;

            if ((*c)->qpn_s().slot_name_or_null())
                cout << "[?] " << c::bold_red() << (*c)->qpn_s() << c::normal();
            else
                cout << "[?] " << c::bold_red() << (*c)->qpn_s().package() << c::normal();
            cout << " (no decision could be reached)" << endl;

            display_reasons(*c, true);
        }

        cout << endl;
    }

    void display_explanations(
            const std::tr1::shared_ptr<Environment> & env,
            const ResolutionLists & lists,
            const DisplayResolutionCommandLine & cmdline)
    {
        Context context("When displaying explanations:");

        if (cmdline.display_options.a_explain.begin_args() == cmdline.display_options.a_explain.end_args())
            return;

        std::cout << "Explaining requested decisions:" << std::endl << std::endl;

        for (args::StringSetArg::ConstIterator i(cmdline.display_options.a_explain.begin_args()),
                i_end(cmdline.display_options.a_explain.end_args()) ;
                i != i_end ; ++i)
        {
            bool any(false);
            PackageDepSpec spec(parse_user_package_dep_spec(*i, env.get(), UserPackageDepSpecOptions() + updso_allow_wildcards));
            for (Resolutions::ConstIterator r(lists.all()->begin()), r_end(lists.all()->end()) ;
                    r != r_end ; ++r)
            {
                if (! (*r)->decision()->if_package_id())
                {
                    /* decided nothing, so we can only work for cat/pkg, where
                     * either can be wildcards (we could work for :slot too,
                     * but we're lazy) */
                    if (! package_dep_spec_has_properties(spec, make_named_values<PackageDepSpecProperties>(
                                    value_for<n::has_additional_requirements>(false),
                                    value_for<n::has_category_name_part>(indeterminate),
                                    value_for<n::has_from_repository>(false),
                                    value_for<n::has_in_repository>(false),
                                    value_for<n::has_installable_to_path>(false),
                                    value_for<n::has_installable_to_repository>(false),
                                    value_for<n::has_installed_at_path>(false),
                                    value_for<n::has_package>(indeterminate),
                                    value_for<n::has_package_name_part>(indeterminate),
                                    value_for<n::has_slot_requirement>(false),
                                    value_for<n::has_tag>(false),
                                    value_for<n::has_version_requirements>(false)
                                    )))
                        continue;

                    if (spec.package_ptr() && *spec.package_ptr() != (*r)->qpn_s().package())
                        continue;
                    if (spec.package_name_part_ptr() && *spec.package_name_part_ptr() != (*r)->qpn_s().package().package())
                        continue;
                    if (spec.category_name_part_ptr() && *spec.category_name_part_ptr() != (*r)->qpn_s().package().category())
                        continue;
                }
                else
                {
                    if (! match_package(*env, spec, *(*r)->decision()->if_package_id(), MatchPackageOptions()))
                        continue;
                }

                any = true;

                std::cout << "For " << (*r)->qpn_s() << ":" << std::endl;
                std::cout << "    The following constraints were in action:" << std::endl;
                for (Constraints::ConstIterator c((*r)->constraints()->begin()),
                        c_end((*r)->constraints()->end()) ;
                        c != c_end ; ++c)
                {
                    std::cout << "      * " << (*c)->spec();

                    switch ((*c)->use_installed())
                    {
                        case ui_if_same:
                            std::cout << ", use installed if same";
                            break;
                        case ui_never:
                            std::cout << ", never using installed";
                            break;
                        case ui_only_if_transient:
                            std::cout << ", using installed only if transient";
                            break;
                        case ui_if_same_version:
                            std::cout << ", use installed if same version";
                            break;
                        case ui_if_possible:
                            std::cout << ", use installed if possible";
                            break;

                        case last_ui:
                            break;
                    }

                    for (DestinationType t(static_cast<DestinationType>(0)), t_end(last_dt) ;
                            t != t_end ; t = static_cast<DestinationType>(static_cast<int>(t) + 1))
                    {
                        if (! (*c)->to_destinations()[t])
                            continue;

                        switch (t)
                        {
                            case dt_slash:
                                std::cout << ", installing to /";
                                break;

                            case last_dt:
                                break;
                        }
                    }

                    std::cout << std::endl;
                    std::cout << "        Because of ";
                    ReasonNameGetter v(true);
                    std::cout << (*c)->reason()->accept_returning<std::pair<std::string, bool> >(v).first;
                    std::cout << std::endl;
                }

                if ((*r)->decision()->if_package_id())
                {
                    std::cout << "    The decision made was:" << std::endl;
                    std::cout << "        Use " << *(*r)->decision()->if_package_id() << std::endl;
                    if ((*r)->destinations()->slash())
                    {
                        std::cout << "        Install to / using repository " << (*r)->destinations()->slash()->repository() << std::endl;
                        if (! (*r)->destinations()->slash()->replacing()->empty())
                            for (PackageIDSequence::ConstIterator x((*r)->destinations()->slash()->replacing()->begin()),
                                    x_end((*r)->destinations()->slash()->replacing()->end()) ;
                                    x != x_end ; ++x)
                                std::cout << "            Replacing " << **x << std::endl;
                    }
                    std::cout << std::endl;
                }
                else
                    std::cout << "    No decision could be made" << std::endl << std::endl;
            }

            if (! any)
                throw args::DoHelp("There is nothing matching '" + *i + "' in the resolution set.");
        }
    }
}

bool
DisplayResolutionCommand::important() const
{
    return false;
}

int
DisplayResolutionCommand::run(
        const std::tr1::shared_ptr<Environment> & env,
        const std::tr1::shared_ptr<const Sequence<std::string > > & args
        )
{
    DisplayResolutionCommandLine cmdline;
    cmdline.run(args, "CAVE", "CAVE_DISPLAY_RESOLUTION_OPTIONS", "CAVE_DISPLAY_RESOLUTION_CMDLINE");

    if (cmdline.a_help.specified())
    {
        cout << cmdline;
        return EXIT_SUCCESS;
    }

    if (getenv_with_default("PALUDIS_SERIALISED_RESOLUTION_FD", "").empty())
        throw args::DoHelp("PALUDIS_SERIALISED_RESOLUTION_FD must be provided");

    int fd(destringify<int>(getenv_with_default("PALUDIS_SERIALISED_RESOLUTION_FD", "")));
    SafeIFStream deser_stream(fd);
    const std::string deser_str((std::istreambuf_iterator<char>(deser_stream)), std::istreambuf_iterator<char>());
    Deserialiser deserialiser(env.get(), deser_str);
    Deserialisation deserialisation("ResolutionLists", deserialiser);
    ResolutionLists lists(ResolutionLists::deserialise(deserialisation));

    display_resolution(env, lists, cmdline);
    display_errors(env, lists, cmdline);
    display_explanations(env, lists, cmdline);

    return 0;
}

std::tr1::shared_ptr<args::ArgsHandler>
DisplayResolutionCommand::make_doc_cmdline()
{
    return make_shared_ptr(new DisplayResolutionCommandLine);
}