aboutsummaryrefslogtreecommitdiff
path: root/src/clients/cave/cmd_resolve.cc
blob: 794f3a306de5ee3d64123f3f9ae15a132b421746 (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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/* 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_resolve.hh"
#include "exceptions.hh"

#include "command_command_line.hh"

#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/args/do_help.hh>
#include <paludis/resolver/resolver.hh>
#include <paludis/resolver/resolution.hh>
#include <paludis/resolver/decision.hh>
#include <paludis/resolver/resolver_functions.hh>
#include <paludis/resolver/reason.hh>
#include <paludis/resolver/suggest_restart.hh>
#include <paludis/resolver/qpn_s.hh>
#include <paludis/resolver/constraint.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/notifier_callback.hh>
#include <paludis/generator.hh>
#include <paludis/filter.hh>
#include <paludis/selection.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/version_spec.hh>
#include <paludis/metadata_key.hh>
#include <paludis/environment.hh>

#include <iostream>
#include <cstdlib>
#include <map>

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

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

namespace
{
    struct ResolveCommandLine :
        CaveCommandCommandLine
    {
        virtual std::string app_name() const
        {
            return "cave resolve";
        }

        virtual std::string app_synopsis() const
        {
            return "Display how to resolve one or more targets, and possibly then "
                "perform that resolution.";
        }

        virtual std::string app_description() const
        {
            return "Displays how to resolve one or more targets. If instructed, then "
                "executes the relevant install and uninstall actions to perform that "
                "resolution.";
        }

//        args::ArgsGroup g_execution_options;
//        args::SwitchArg a_execute;
//        args::SwitchArg a_preserve_world;

//        args::ArgsGroup g_convenience_options;
//        args::SwitchArg a_lazy;
//        args::SwitchArg a_complete;
//        args::SwitchArg a_everything;

//        args::ArgsGroup g_resolution_options;
//        args::SwitchArg a_permit_slot_uninstalls;
//        args::SwitchArg a_permit_uninstalls;
//        args::SwitchArg a_permit_downgrades;
//        args::SwitchArg a_permit_unsafe_uninstalls;

//        args::ArgsGroup g_cleanup_options;
//        args::SwitchArg a_purge_unused_slots;
//        args::SwitchArg a_purge_unused_packages;

//        args::ArgsGroup g_failure_options;
//        args::EnumArg a_continue_on_failure;

//        args::ArgsGroup g_display_options;
//        args::EnumArg a_show_option_descriptions;
//        args::EnumArg a_show_descriptions;

//        args::ArgsGroup g_explanations;
//        args::StringSetArg a_explain;

//        args::ArgsGroup g_phase_options;
//        args::StringSetArg a_skip_phase;
//        args::StringSetArg a_abort_at_phase;
//        args::StringSetArg a_skip_until_phase;
//        args::EnumArg a_change_phases_for;

        args::ArgsGroup g_reinstall_options;
        args::EnumArg a_reinstall_targets;
        args::EnumArg a_reinstall;
        args::EnumArg a_reinstall_scm;
//        args::SwitchArg a_reinstall_for_removals;

        args::ArgsGroup g_slot_options;
        args::EnumArg a_target_slots;
        args::EnumArg a_slots;

//        args::ArgsGroup g_dependency_options;
//        args::SwitchArg a_follow_installed_build_dependencies;
//        args::SwitchArg a_ignore_installed_dependencies;

//        args::ArgsGroup g_suggestion_options;
//        args::EnumArg a_suggestions;
//        args::EnumArg a_recommendations;
//        args::StringSetArg a_take;
//        args::StringSetArg a_take_from;
//        args::StringSetArg a_discard;
//        args::StringSetArg a_discard_from;

//        args::ArgsGroup g_package_options;
//        args::StringSetArg a_prefer;
//        args::StringSetArg a_avoid;

//        args::ArgsGroup g_ordering_options;
//        args::StringSetArg a_early;
//        args::StringSetArg a_late;

//        args::ArgsGroup g_preset_options;
//        args::StringSetArg a_soft_preset;
//        args::StringSetArg a_fixed_preset;

//        args::ArgsGroup g_destination_options;
//        args::SwitchArg a_fetch;
//        args::SwitchArg a_create_binaries;
//        args::SwitchArg a_install_to_chroot;
//        args::SwitchArg a_install_to_root;

//        args::ArgsGroup g_interactivity_options;
//        args::SwitchArg a_interactive;
//        args::SwitchArg a_interactive_slots;
//        args::SwitchArg a_interactive_decisions;
//        args::SwitchArg a_interactive_ordering;

        args::ArgsGroup g_dump_options;
        args::SwitchArg a_dump;
        args::SwitchArg a_dump_dependencies;

        ResolveCommandLine() :
//            g_execution_options(this, "Execution Options", "Control execution."),
//            a_execute(&g_execution_options, "execute", 'x', "Execute the suggested actions", true),
//            a_preserve_world(&g_execution_options, "preserve-world", '1', "Do not modify the 'world' set", true),
//
//            g_convenience_options(this, "Convenience Options", "Broad behaviour options."),
//            a_lazy(&g_convenience_options, "lazy", 'z', "Do as little work as possible.", true),
//            a_complete(&g_convenience_options, "complete", 'c', "Do all optional work.", true),
//            a_everything(&g_convenience_options, "everything", 'e', "Do all optional work, and also reinstall", true),
//
//            g_resolution_options(this, "Resolution Options", "Resolution options."),
//            a_permit_slot_uninstalls(&g_resolution_options, "permit-slot-uninstalls", '\0',
//                    "Uninstall slots of packages if they are blocked by other slots of that package", true),
//            a_permit_uninstalls(&g_resolution_options, "permit-uninstalls", '\0',
//                    "Uninstall packages that are blocked", true),
//            a_permit_downgrades(&g_resolution_options, "permit-downgrades", '\0', "Permit downgrades", true),
//            a_permit_unsafe_uninstalls(&g_resolution_options, "permit-unsafe-uninstalls", '\0',
//                    "Permit uninstalls even if the uninstall isn't known to be safe", true),
//
//            g_cleanup_options(this, "Cleanup Options", "Cleanup options."),
//            a_purge_unused_slots(&g_cleanup_options, "purge-unused-slots", '\0',
//                    "Purge slots that are no longer used after an uninstall or clean", true),
//            a_purge_unused_packages(&g_cleanup_options, "purge-unused-packages", '\0',
//                    "Purge packages that are no longer used after an uninstall or clean", true),
//
//            g_failure_options(this, "Failure Options", "Failure handling options."),
//            a_continue_on_failure(&g_failure_options, "continue-on-failure", '\0',
//                    "Whether to continue after an error occurs",
//                    args::EnumArg::EnumArgOptions
//                    ("if-fetching",                "Only if we are just fetching packages")
//                    ("never",                      "Never")
//                    ("if-satisfied",               "If remaining packages' dependencies are satisfied")
//                    ("if-independent",             "If remaining packages do not depend upon any failing package")
//                    ("always",                     "Always (dangerous)"),
//                    "if-fetching"),
//
//            g_display_options(this, "Display Options", "Options relating to the resolution display."),
//            a_show_option_descriptions(&g_display_options, "show-option-descriptions", '\0',
//                    "Whether to display descriptions for package options",
//                    args::EnumArg::EnumArgOptions
//                    ("none",                       "Don't show any descriptions")
//                    ("new",                        "Show for any new options")
//                    ("changed",                    "Show for new or changed options")
//                    ("all",                        "Show all options"),
//                    "changed"
//                    ),
//            a_show_descriptions(&g_display_options, "show-descriptions", '\0',
//                    "Whether to display package descriptions",
//                    args::EnumArg::EnumArgOptions
//                    ("none",                       "Don't show any descriptions")
//                    ("new",                        "Show for new packages")
//                    ("all",                        "Show for all packages"),
//                    "new"
//                    ),
//
//            g_explanations(this, "Explanations", "Options requesting the resolver explain a particular decision "
//                    "that it made"),
//            a_explain(&g_explanations, "explain", '\0', "Explain why the resolver made a particular decision. The "
//                    "argument is a package dependency specification, so --explain dev-libs/boost or --explain qt:3"
//                    " or even --explain '*/*' (although --dump is a better way of getting highly noisy debug output)."),
//
//            g_phase_options(this, "Phase Options", "Options controlling which phases to execute. No sanity checking "
//                    "is done, allowing you to shoot as many feet off as you desire. Phase names do not have the "
//                    "src_, pkg_ or builtin_ prefix, so 'init', 'preinst', 'unpack', 'merge', 'strip' etc."),
//            a_skip_phase(&g_phase_options, "skip-phase", '\0', "Skip the named phases"),
//            a_abort_at_phase(&g_phase_options, "abort-at-phase", '\0', "Abort when a named phase is encounted"),
//            a_skip_until_phase(&g_phase_options, "skip-until-phase", '\0', "Skip every phase until a named phase is encounted"),
//            a_change_phases_for(&g_phase_options, "change-phases-for", '\0',
//                    "Control to which package or packages these phase options apply",
//                    args::EnumArg::EnumArgOptions
//                    ("all",                        "All packages")
//                    ("first",                      "Only the first package on the list")
//                    ("last",                       "Only the last package on the list"),
//                    "all"),
//
            g_reinstall_options(this, "Reinstall Options", "Control whether packages are reinstalled."),
            a_reinstall_targets(&g_reinstall_options, "reinstall-targets", 'R',
                    "Select whether to reinstall target packages",
                    args::EnumArg::EnumArgOptions
                    ("auto",                  'x', "If the target is a set, if-same, otherwise always")
                    ("always",                'a', "Always")
                    ("unless-transient",      't', "Unless the target is transient (e.g. from 'importare')"
                                                   " (default if --everything)")
                    ("unless-same",           's', "Unless the target is the same (version, option flags)")
                    ("unless-same-version",   'v', "Unless the target is the same version")
                    ("if-necessary",          'i', "If necessary"),
                    "auto"
                    ),
            a_reinstall(&g_reinstall_options, "reinstall", 'r',
                    "Select whether to reinstall packages that are not targets",
                    args::EnumArg::EnumArgOptions
                    ("always",                'a', "Always")
                    ("unless-transient",      't', "Unless the target is transient (e.g. from 'importare')"
                                                   " (default if --everything)")
                    ("unless-same",           's', "Unless the target is the same (version, option flags)"
                                                   " (default if --complete)")
                    ("unless-same-version",   'v', "Unless the target is the same version")
                    ("if-necessary",          'i', "If necessary"),
                    "if-necessary"
                    ),
            a_reinstall_scm(&g_reinstall_options, "reinstall-scm", '\0',
                    "Select whether to reinstall SCM packages that would otherwise not be reinstalled",
                    args::EnumArg::EnumArgOptions
                    ("always",                'a', "Always")
                    ("daily",                 'd', "If they were installed more than a day ago")
                    ("weekly",                'w', "If they were installed more than a week ago")
                    ("never",                 'n', "Never"),
                    "weekly"
                    ),
//            a_reinstall_for_removals(&g_reinstall_options, "reinstall-for-removals", '\0',
//                    "Select whether to rebuild packages if rebuilding would avoid an unsafe removal", true),
//
            g_slot_options(this, "Slot Options", "Control which slots are considered."),
            a_target_slots(&g_slot_options, "target-slots", 'S',
                    "Which slots to consider for targets",
                    args::EnumArg::EnumArgOptions
                    ("best-or-installed",     'x', "Consider the best slot, if it is not installed, "
                                                   "or all installed slots otherwise")
                    ("installed-or-best",     'i', "Consider all installed slots, or the best "
                                                   "installable slot if nothing is installed")
                    ("all",                   'a', "Consider all installed slots and the best installable slot"
                                                   " (default if --complete or --everything)")
                    ("best",                  'b', "Consider the best installable slot only"
                                                   " (default if --lazy)"),
                    "best-or-installed"
                    ),
            a_slots(&g_slot_options, "slots", 's',
                    "Which slots to consider for packages that are not targets",
                    args::EnumArg::EnumArgOptions
                    ("best-or-installed",     'x', "Consider the best slot, if it is not installed, "
                                                   "or all installed slots otherwise")
                    ("installed-or-best",     'i', "Consider all installed slots, or the best "
                                                   "installable slot if nothing is installed")
                    ("all",                   'a', "Consider all installed slots and the best installable slot"
                                                   " (default if --complete or --everything)")
                    ("best",                  'b', "Consider the best installable slot only"
                                                   " (default if --lazy)"),
                    "best-or-installed"
                    ),

//            g_dependency_options(this, "Dependency Options", "Control which dependencies are followed."),
//            a_follow_installed_build_dependencies(&g_dependency_options, "follow-installed-build-dependencies", 'D',
//                    "Follow build dependencies for installed packages (default if --complete or --everything", true),
//            a_ignore_installed_dependencies(&g_dependency_options, "ignore-installed-dependencies", 'd',
//                    "Ignore (non-build) dependencies for installed packages (default if --lazy)", true),
//
//            g_suggestion_options(this, "Suggestion Options", "Control whether suggestions are taken. Suggestions that are "
//                    "already installed are instead treated as hard dependencies."),
//            a_suggestions(&g_suggestion_options, "suggestions", '\0', "How to treat suggestions and recommendations",
//                    args::EnumArg::EnumArgOptions
//                    ("ignore",                     "Ignore suggestions")
//                    ("display",                    "Display suggestions, but do not take them unless explicitly told to do so")
//                    ("take",                       "Take all suggestions"),
//                    "display"),
//            a_recommendations(&g_suggestion_options, "recommendations", '\0', "How to treat recommendations",
//                    args::EnumArg::EnumArgOptions
//                    ("ignore",                     "Ignore recommendations")
//                    ("display",                    "Display recommendations, but do not take them unless explicitly told to do so")
//                    ("take",                       "Take all recommendations"),
//                    "take"),
//            a_take(&g_suggestion_options, "take", '\0', "Take any suggestion matching the supplied package specification"
//                    " (e.g. --take 'app-vim/securemodelines' or --take 'app-vim/*')"),
//            a_take_from(&g_suggestion_options, "take-from", '\0', "Take all suggestions made by any package matching the "
//                    "supplied package specification"),
//            a_discard(&g_suggestion_options, "discard", '\0', "Discard any suggestion matching the supplied package specification"),
//            a_discard_from(&g_suggestion_options, "discard-from", '\0', "Discard all suggestions made by any package matching the "
//                    "supplied package specification"),
//
//            g_package_options(this, "Package Selection Options", "Control which packages are selected."),
//            a_prefer(&g_package_options, "prefer", '\0', "If there is a choice, prefer the specified package names"),
//            a_avoid(&g_package_options, "avoid", '\0', "If there is a choice, avoid the specified package names"),
//
//            g_ordering_options(this, "Package Ordering Options", "Control the order in which packages are installed"),
//            a_early(&g_ordering_options, "early", '\0', "Try to install the specified package name as early as possible"),
//            a_late(&g_ordering_options, "late", '\0', "Try to install the specified package name as late as possible"),
//
//            g_preset_options(this, "Preset Options", "Preset various constraints."),
//            a_soft_preset(&g_preset_options, "soft-preset", '\0', "Preset a given constraint, but allow the resolver to "
//                    "override it. For example, --soft-preset cat/pkg::installed will tell the resolver to use the installed "
//                    "cat/pkg if possible."),
//            a_fixed_preset(&g_preset_options, "fixed-preset", '\0', "Preset a given constraint, and do not allow the resolver to "
//                    "override it. For example, --fixed-preset cat/pkg::installed will force the resolver to use the installed "
//                    "cat/pkg, generating an error if it cannot."),
//
//            g_destination_options(this, "Destination Options", "Control to which destinations packages are installed. "
//                    "If no options from this group are selected, install only to /. Otherwise, install to all of the "
//                    "specified destinations, and install to / as necessary to satisfy build dependencies."),
//            a_fetch(&g_destination_options, "fetch", 'f', "Only fetch packages, do not install anything", true),
//            a_create_binaries(&g_destination_options, "create-binaries", '\0', "Create binary packages", true),
//            a_install_to_chroot(&g_destination_options, "install-to-chroot", '\0', "Install packages to the environment-configured chroot", true),
//            a_install_to_root(&g_destination_options, "install-to-root", '\0', "Install packages to /", true),

            g_dump_options(this, "Dump Options", "Dump the resolver's state to stdout after completion, or when an "
                    "error occurs. For debugging purposes; produces rather a lot of noise."),
            a_dump(&g_dump_options, "dump", '\0', "Dump debug output", true),
            a_dump_dependencies(&g_dump_options, "dump-dependencies", '\0', "If dumping, also dump the "
                    "sanitised dependencies selected for every package" , true)
        {
            add_usage_line("[ -x|--execute ] [ -z|--lazy or -c|--complete or -e|--everything ] spec ...");
            add_usage_line("[ -x|--execute ] [ -z|--lazy or -c|--complete or -e|--everything ] set");
        }
    };

    struct DisplayCallback
    {
        mutable Mutex mutex;
        mutable int width, metadata, steps;

        DisplayCallback() :
            width(0),
            metadata(0),
            steps(0)
        {
            std::cout << "Resolving: " << std::flush;
        }

        ~DisplayCallback()
        {
            std::cout << std::endl << std::endl;
        }

        void operator() (const NotifierCallbackEvent & event) const
        {
            event.accept(*this);
        }

        void visit(const NotifierCallbackGeneratingMetadataEvent &) const
        {
            Lock lock(mutex);
            ++metadata;
            update();
        }

        void visit(const NotifierCallbackResolverStepEvent &) const
        {
            Lock lock(mutex);
            ++steps;
            update();
        }

        void update() const
        {
            std::string s;
            if (0 != steps)
                s.append("steps: " + stringify(steps));

            if (0 != metadata)
            {
                if (! s.empty())
                    s.append(", ");
                s.append("metadata: " + stringify(metadata));
            }

            std::cout << std::string(width, '\010') << s << std::flush;
            width = s.length();
        }
    };

    void add_resolver_targets(
            const std::tr1::shared_ptr<Environment> & env,
            const std::tr1::shared_ptr<Resolver> & resolver,
            const ResolveCommandLine & cmdline)
    {
        Context context("When adding targets from commandline:");

        if (cmdline.begin_parameters() == cmdline.end_parameters())
            throw args::DoHelp("Must specify at least one target");

        bool seen_sets(false), seen_packages(false);
        for (ResolveCommandLine::ParametersConstIterator p(cmdline.begin_parameters()), p_end(cmdline.end_parameters()) ;
                p != p_end ; ++p)
        {
            try
            {
                resolver->add_target(parse_user_package_dep_spec(*p, env.get(),
                            UserPackageDepSpecOptions() + updso_throw_if_set));
                if (seen_sets)
                    throw args::DoHelp("Cannot specify both set and package targets");
                seen_packages = true;
            }
            catch (const GotASetNotAPackageDepSpec &)
            {
                if (seen_packages)
                    throw args::DoHelp("Cannot specify both set and package targets");
                if (seen_sets)
                    throw args::DoHelp("Cannot specify multiple set targets");

                resolver->add_target(SetName(*p));
                seen_sets = true;
            }
        }
    }

    void display_resolution(
            const std::tr1::shared_ptr<Environment> &,
            const std::tr1::shared_ptr<Resolver> & resolver,
            const ResolveCommandLine &)
    {
        Context context("When displaying chosen resolution:");

        for (Resolver::ConstIterator c(resolver->begin()), c_end(resolver->end()) ;
                c != c_end ; ++c)
        {
            const std::tr1::shared_ptr<const PackageID> id((*c)->decision()->package_id());
            std::cout << "* " << id->canonical_form(idcf_no_version) << " " << id->canonical_form(idcf_version)
                << " to " << *(*c)->destinations() << std::endl;
        }

        std::cout << std::endl;
    }

    void dump_if_requested(
            const std::tr1::shared_ptr<Environment> &,
            const std::tr1::shared_ptr<Resolver> & resolver,
            const ResolveCommandLine & cmdline)
    {
        Context context("When dumping the resolver:");

        if (cmdline.a_dump.specified())
            resolver->dump(std::cerr, cmdline.a_dump_dependencies.specified());
    }

    UseInstalled use_installed_from_cmdline(const args::EnumArg & a, const bool is_set)
    {
        if (a.argument() == "auto")
            return is_set ? ui_if_same : ui_never;
        else if (a.argument() == "always")
            return ui_never;
        else if (a.argument() == "unless-transient")
            return ui_only_if_transient;
        else if (a.argument() == "unless-same")
            return ui_if_same;
        else if (a.argument() == "unless-same-version")
            return ui_if_same_version;
        else if (a.argument() == "if-necessary")
            return ui_if_possible;
        else
            throw args::DoHelp("Don't understand argument '" + a.argument() + "' to '--" + a.long_name() + "'");
    }

    struct UseInstalledVisitor
    {
        const ResolveCommandLine & cmdline;
        const bool from_set;

        UseInstalledVisitor(const ResolveCommandLine & c, const bool f) :
            cmdline(c),
            from_set(f)
        {
        }

        UseInstalled visit(const DependencyReason &) const
        {
            return use_installed_from_cmdline(cmdline.a_reinstall, false);
        }

        UseInstalled visit(const TargetReason &) const
        {
            return use_installed_from_cmdline(cmdline.a_reinstall_targets, from_set);
        }

        UseInstalled visit(const PresetReason &) const
        {
            return ui_if_possible;
        }

        UseInstalled visit(const SetReason & r) const
        {
            UseInstalledVisitor v(cmdline, true);
            return r.reason_for_set()->accept_returning<UseInstalled>(v);
        }
    };

    UseInstalled use_installed_fn(const ResolveCommandLine & cmdline,
            const QPN_S &,
            const PackageDepSpec &,
            const std::tr1::shared_ptr<const Reason> & reason)
    {
        UseInstalledVisitor v(cmdline, false);
        return reason->accept_returning<UseInstalled>(v);
    }

    int reinstall_scm_days(const ResolveCommandLine & cmdline)
    {
        if (cmdline.a_reinstall_scm.argument() == "always")
            return 0;
        else if (cmdline.a_reinstall_scm.argument() == "daily")
            return 1;
        else if (cmdline.a_reinstall_scm.argument() == "weekly")
            return 7;
        else if (cmdline.a_reinstall_scm.argument() == "never")
            return -1;
        else
            throw args::DoHelp("Don't understand argument '" + cmdline.a_reinstall_scm.argument() + "' to '--"
                    + cmdline.a_reinstall_scm.long_name() + "'");
    }

    bool is_scm_name(const QualifiedPackageName & n)
    {
        std::string pkg(stringify(n.package()));
        switch (pkg.length())
        {
            case 0:
            case 1:
            case 2:
            case 3:
                return false;

            default:
                if (0 == pkg.compare(pkg.length() - 6, 6, "-darcs"))
                    return true;

            case 5:
                if (0 == pkg.compare(pkg.length() - 5, 5, "-live"))
                    return true;

            case 4:
                if (0 == pkg.compare(pkg.length() - 4, 4, "-cvs"))
                    return true;
                if (0 == pkg.compare(pkg.length() - 4, 4, "-svn"))
                    return true;
                return false;
        }
    }

    bool is_scm_older_than(const std::tr1::shared_ptr<const PackageID> & id, const int n)
    {
        if (id->version().is_scm() || is_scm_name(id->name()))
        {
            static time_t current_time(time(0)); /* static to avoid weirdness */
            time_t installed_time(current_time);
            if (id->installed_time_key())
                installed_time = id->installed_time_key()->value();

            return (current_time - installed_time) > (24 * 60 * 60 * n);
        }
        else
            return false;
    }

    bool installed_is_scm_older_than(const Environment * const env, const QPN_S & q, const int n)
    {
        Context context("When working out whether '" + stringify(q) + "' has installed SCM packages:");

        const std::tr1::shared_ptr<const PackageIDSequence> ids((*env)[selection::AllVersionsUnsorted(
                    generator::Package(q.package()) |
                    q.make_slot_filter() |
                    filter::SupportsAction<InstalledAction>())]);

        for (PackageIDSequence::ConstIterator i(ids->begin()), i_end(ids->end()) ;
                i != i_end ; ++i)
        {
            if (is_scm_older_than(*i, n))
                return true;
        }

        return false;
    }

    typedef std::map<QPN_S, std::tr1::shared_ptr<Constraints> > InitialConstraints;

    const std::tr1::shared_ptr<Constraints> make_initial_constraints_for(
            const Environment * const env,
            const ResolveCommandLine & cmdline,
            const QPN_S & qpn_s)
    {
        const std::tr1::shared_ptr<Constraints> result(new Constraints);

        int n(reinstall_scm_days(cmdline));
        if ((-1 != n) && installed_is_scm_older_than(env, qpn_s, n))
        {
            result->add(make_shared_ptr(new Constraint(make_named_values<Constraint>(
                                value_for<n::desire_strength>(ds_target),
                                value_for<n::reason>(make_shared_ptr(new PresetReason)),
                                value_for<n::spec>(make_package_dep_spec(PartiallyMadePackageDepSpecOptions()).package(qpn_s.package())),
                                value_for<n::to_destination_slash>(false),
                                value_for<n::use_installed>(ui_only_if_transient)
                                ))));
        }

        return result;
    }

    const std::tr1::shared_ptr<Constraints> initial_constraints_for_fn(
            const Environment * const env,
            const ResolveCommandLine & cmdline,
            const InitialConstraints & initial_constraints,
            const QPN_S & qpn_s)
    {
        InitialConstraints::const_iterator i(initial_constraints.find(qpn_s));
        if (i == initial_constraints.end())
            return make_initial_constraints_for(env, cmdline, qpn_s);
        else
            return i->second;
    }

    struct SlotNameFinder
    {
        std::tr1::shared_ptr<SlotName> visit(const SlotExactRequirement & s)
        {
            return make_shared_ptr(new SlotName(s.slot()));
        }

        std::tr1::shared_ptr<SlotName> visit(const SlotAnyUnlockedRequirement &)
        {
            return make_null_shared_ptr();
        }

        std::tr1::shared_ptr<SlotName> visit(const SlotAnyLockedRequirement &)
        {
            return make_null_shared_ptr();
        }
    };

    const std::tr1::shared_ptr<QPN_S_Sequence>
    get_qpn_s_s_for_fn(const Environment * const env, const ResolveCommandLine &, const PackageDepSpec & spec,
            const std::tr1::shared_ptr<const Reason> &)
    {
        std::tr1::shared_ptr<QPN_S_Sequence> result(new QPN_S_Sequence);

        std::tr1::shared_ptr<SlotName> exact_slot;

        if (spec.slot_requirement_ptr())
        {
            SlotNameFinder f;
            exact_slot = spec.slot_requirement_ptr()->accept_returning<std::tr1::shared_ptr<SlotName> >(f);
        }

        if (exact_slot)
            result->push_back(QPN_S(spec, exact_slot));
        else
        {
            const std::tr1::shared_ptr<const PackageIDSequence> ids((*env)[selection::BestVersionOnly(
                        generator::Matches(spec, MatchPackageOptions() + mpo_ignore_additional_requirements) |
                        filter::SupportsAction<InstallAction>() |
                        filter::NotMasked())]);

            if (! ids->empty())
                result->push_back(QPN_S(*ids->begin()));
            else
            {
                const std::tr1::shared_ptr<const PackageIDSequence> installed_ids((*env)[selection::BestVersionOnly(
                            generator::Matches(spec, MatchPackageOptions() + mpo_ignore_additional_requirements) |
                            filter::SupportsAction<InstalledAction>())]);

                if (! installed_ids->empty())
                    result->push_back(QPN_S(*installed_ids->begin()));
            }
        }

        return result;
    }
}


bool
ResolveCommand::important() const
{
    return true;
}

int
ResolveCommand::run(
        const std::tr1::shared_ptr<Environment> & env,
        const std::tr1::shared_ptr<const Sequence<std::string > > & args
        )
{
    ResolveCommandLine cmdline;
    cmdline.run(args, "CAVE", "CAVE_RESOLVE_OPTIONS", "CAVE_RESOLVE_CMDLINE");

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

    int retcode(0);

    InitialConstraints initial_constraints;

    ResolverFunctions resolver_functions(make_named_values<ResolverFunctions>(
                value_for<n::get_initial_constraints_for_fn>(std::tr1::bind(&initial_constraints_for_fn,
                        env.get(), std::tr1::cref(cmdline), std::tr1::cref(initial_constraints), std::tr1::placeholders::_1)),
                value_for<n::get_qpn_s_s_for_fn>(std::tr1::bind(&get_qpn_s_s_for_fn,
                        env.get(), std::tr1::cref(cmdline), std::tr1::placeholders::_1, std::tr1::placeholders::_2)),
                value_for<n::get_use_installed_fn>(std::tr1::bind(&use_installed_fn,
                        std::tr1::cref(cmdline), std::tr1::placeholders::_1, std::tr1::placeholders::_2, std::tr1::placeholders::_3))
                ));
    std::tr1::shared_ptr<Resolver> resolver(new Resolver(env.get(), resolver_functions));
    try
    {
        while (true)
        {
            try
            {
                DisplayCallback display_callback;
                ScopedNotifierCallback display_callback_holder(env.get(),
                        NotifierCallbackFunction(std::tr1::cref(display_callback)));
                add_resolver_targets(env, resolver, cmdline);
                resolver->resolve();
                break;
            }
            catch (const SuggestRestart & e)
            {
                std::cout << "Restarting: for '" << e.qpn_s() << "' we had chosen '" << *e.previous_decision()
                    << "' but new constraint '" << *e.problematic_constraint() << "' means we now want '"
                    << *e.new_decision() << "' instead" << std::endl;
                initial_constraints.insert(std::make_pair(e.qpn_s(), make_initial_constraints_for(
                                env.get(), cmdline, e.qpn_s()))).first->second->add(
                        e.suggested_preset());
                resolver = make_shared_ptr(new Resolver(env.get(), resolver_functions));
            }
        }

        display_resolution(env, resolver, cmdline);
    }
    catch (...)
    {
        dump_if_requested(env, resolver, cmdline);
        throw;
    }

    dump_if_requested(env, resolver, cmdline);

    return retcode;
}

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