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

/*
 * Copyright (c) 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 "cmd_resume.hh"
#include "cmd_execute_resolution.hh"
#include "resolve_cmdline.hh"
#include "exceptions.hh"
#include "command_command_line.hh"
#include "colours.hh"
#include "resume_data.hh"
#include <paludis/args/do_help.hh>
#include <paludis/args/escape.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/system.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/join.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/make_shared_copy.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/wrapped_output_iterator.hh>
#include <paludis/util/string_list_stream.hh>
#include <paludis/util/process.hh>
#include <paludis/util/fs_stat.hh>
#include <paludis/resolver/job_lists.hh>
#include <paludis/resolver/job.hh>
#include <paludis/resolver/job_list.hh>
#include <paludis/resolver/job_state.hh>
#include <paludis/environment.hh>
#include <paludis/serialise.hh>

#include <iostream>
#include <cstdlib>
#include <algorithm>

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

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

namespace
{
    struct ResumeCommandLine :
        CaveCommandCommandLine
    {
        args::ArgsGroup g_retry_options;
        args::SwitchArg a_retry_failed;
        args::SwitchArg a_retry_skipped;
        args::SwitchArg a_skip_failed;

        ResolveCommandLineExecutionOptions execution_options;
        ResolveCommandLineProgramOptions program_options;

        ResumeCommandLine() :
            g_retry_options(main_options_section(), "Retry Options", "Retry options. By default, 'cave resume' will "
                    "treat packages that were already skipped or already failed as skipped or failed, respectively."),
            a_retry_failed(&g_retry_options, "retry-failed", 'r', "Retry any job that has already failed", true),
            a_retry_skipped(&g_retry_options, "retry-skipped", 'R', "Retry any job that has already been skipped. Note that "
                    "the job will just be skipped again unless circumstances have changed.", true),
            a_skip_failed(&g_retry_options, "skip-failed", 's', "Skip any job that has already failed", true),
            execution_options(this),
            program_options(this)
        {
            execution_options.a_preserve_world.remove();
            add_usage_line("--resume-file state [ --retry-failed ] [ --retry-skipped ]");
        }

        virtual std::string app_name() const
        {
            return "cave resume";
        }

        virtual std::string app_synopsis() const
        {
            return "Resume a failed resolution from 'cave resolve'";
        }

        virtual std::string app_description() const
        {
            return "Resumes a failed resultion from 'cave resolve'. To enable resumes, use "
                "'cave resolve --resume-file state --execute', and then if errors occur, use "
                "'cave resume --resume-file state [ --retry-failed ] [ --retry-skipped ]' to "
                "try again.";
        }
    };

    int perform_resolution(
            const std::shared_ptr<Environment> & env,
            const std::shared_ptr<ResumeData> & data,
            const ResolveCommandLineExecutionOptions & execution_options,
            const ResolveCommandLineProgramOptions & program_options)
    {
        Context context("When performing chosen resolution:");

        std::shared_ptr<Sequence<std::string> > args(std::make_shared<Sequence<std::string>>());

        for (args::ArgsSection::GroupsConstIterator g(program_options.begin()), g_end(program_options.end()) ;
                g != g_end ; ++g)
        {
            for (args::ArgsGroup::ConstIterator o(g->begin()), o_end(g->end()) ;
                    o != o_end ; ++o)
                if ((*o)->specified())
                {
                    const std::shared_ptr<const Sequence<std::string> > f((*o)->forwardable_args());
                    std::copy(f->begin(), f->end(), args->back_inserter());
                }
        }

        for (args::ArgsSection::GroupsConstIterator g(execution_options.begin()), g_end(execution_options.end()) ;
                g != g_end ; ++g)
        {
            for (args::ArgsGroup::ConstIterator o(g->begin()), o_end(g->end()) ;
                    o != o_end ; ++o)
                if ((*o)->specified())
                {
                    const std::shared_ptr<const Sequence<std::string> > f((*o)->forwardable_args());
                    std::copy(f->begin(), f->end(), args->back_inserter());
                }
        }

        for (Sequence<std::string>::ConstIterator p(data->targets()->begin()),
                p_end(data->targets()->end()) ;
                p != p_end ; ++p)
            args->push_back(*p);

        for (Sequence<std::string>::ConstIterator p(data->world_specs()->begin()),
                p_end(data->world_specs()->end()) ;
                p != p_end ; ++p)
        {
            args->push_back("--world-specs");
            args->push_back(*p);
        }

        if (data->preserve_world())
            args->push_back("--preserve-world");

        if (data->target_set())
            args->push_back("--set");

        if (program_options.a_execute_resolution_program.specified())
        {
            StringListStream ser_stream;
            Serialiser ser(ser_stream);
            data->job_lists()->serialise(ser);
            ser_stream.nothing_more_to_write();

            std::string command;
            if (program_options.a_execute_resolution_program.specified())
                command = program_options.a_execute_resolution_program.argument();
            else
                command = "$CAVE execute-resolution";

            for (Sequence<std::string>::ConstIterator a(args->begin()), a_end(args->end()) ;
                    a != a_end ; ++a)
                command = command + " " + args::escape(*a);

            Process process((ProcessCommand(command)));
            process
                .send_input_to_fd(ser_stream, -1, "PALUDIS_SERIALISED_RESOLUTION_FD")
                .as_main_process();

            int retcode(process.run().wait());
            _exit(retcode);
        }
        else
            return ExecuteResolutionCommand().run(env, args, data->job_lists());
    }

    struct StateVisitor
    {
        bool is_active;
        bool is_failed;
        bool is_skipped;

        StateVisitor() :
            is_active(false),
            is_failed(false),
            is_skipped(false)
        {
        }

        void visit(const JobPendingState &)
        {
        }

        void visit(const JobSkippedState &)
        {
            is_skipped = true;
        }

        void visit(const JobFailedState &)
        {
            is_failed = true;
        }

        void visit(const JobActiveState &)
        {
            is_active = true;
        }

        void visit(const JobSucceededState &)
        {
        }
    };

    void fix_lists(
            const std::shared_ptr<Environment> &,
            const ResumeCommandLine & cmdline,
            const std::shared_ptr<JobLists> & lists)
    {
        for (JobList<ExecuteJob>::ConstIterator c(lists->execute_job_list()->begin()),
                c_end(lists->execute_job_list()->end()) ;
                c != c_end ; ++c)
        {
            StateVisitor s;
            if ((*c)->state())
                (*c)->state()->accept(s);

            if (s.is_active)
                (*c)->set_state(std::make_shared<JobPendingState>());
            else if (cmdline.a_retry_failed.specified() && s.is_failed)
                (*c)->set_state(std::make_shared<JobPendingState>());
            else if (cmdline.a_skip_failed.specified() && s.is_failed)
                (*c)->set_state(std::make_shared<JobSkippedState>());
            else if (cmdline.a_retry_skipped.specified() && s.is_skipped)
                (*c)->set_state(std::make_shared<JobPendingState>());
        }
    }
}

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

int
ResumeCommand::run(
        const std::shared_ptr<Environment> & env,
        const std::shared_ptr<const Sequence<std::string > > & args
        )
{
    ResumeCommandLine cmdline;
    cmdline.run(args, "CAVE", "CAVE_RESUME_OPTIONS", "CAVE_RESUME_CMDLINE");

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

    if (! cmdline.execution_options.a_resume_file.specified())
        throw args::DoHelp("--" + cmdline.execution_options.a_resume_file.long_name() + " must be specified");

    std::shared_ptr<ResumeData> data;
    {
        FSPath f(cmdline.execution_options.a_resume_file.argument());
        if (! f.stat().exists())
            throw args::DoHelp("Resume file '" + stringify(f) + "' does not exist");
        SafeIFStream deser_stream(f);
        Deserialiser deserialiser(env.get(), deser_stream);
        Deserialisation deserialisation("ResumeData", deserialiser);
        data = ResumeData::deserialise(deserialisation);
    }

    fix_lists(env, cmdline, data->job_lists());
    return perform_resolution(env, data, cmdline.execution_options, cmdline.program_options);
}

std::shared_ptr<args::ArgsHandler>
ResumeCommand::make_doc_cmdline()
{
    return std::make_shared<ResumeCommandLine>();
}