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

/*
 * Copyright (c) 2009, 2010, 2011 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_graph_jobs.hh"
#include "resolve_cmdline.hh"
#include "exceptions.hh"
#include "command_command_line.hh"
#include <paludis/args/do_help.hh>
#include <paludis/util/safe_ifstream.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/make_shared_copy.hh>
#include <paludis/util/system.hh>
#include <paludis/util/destringify.hh>
#include <paludis/util/safe_ofstream.hh>
#include <paludis/util/fs_path.hh>
#include <paludis/util/join.hh>
#include <paludis/util/process.hh>
#include <paludis/resolver/job_list.hh>
#include <paludis/resolver/job_lists.hh>
#include <paludis/resolver/job.hh>
#include <paludis/resolver/job_requirements.hh>
#include <paludis/serialise-impl.hh>
#include <paludis/dep_spec.hh>
#include <paludis/name.hh>

#include <iostream>
#include <cstdlib>

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

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

namespace
{
    struct GraphJobsCommandLine :
        CaveCommandCommandLine
    {
        ResolveCommandLineGraphJobsOptions graph_jobs_options;
        ResolveCommandLineImportOptions import_options;
        ResolveCommandLineProgramOptions program_options;

        GraphJobsCommandLine() :
            graph_jobs_options(this),
            import_options(this),
            program_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 graph-jobs";
        }

        virtual std::string app_synopsis() const
        {
            return "Creates a Graphviz graph for jobs in a resolution created using 'cave resolve'.";
        }

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

    std::string short_spec(const PackageDepSpec & p, bool full)
    {
        if (full || ! p.package_ptr())
            return stringify(p);
        else
        {
            std::string result(stringify(p.package_ptr()->package()));
            if (p.slot_requirement_ptr())
                result = result + stringify(*p.slot_requirement_ptr());
            if (p.in_repository_ptr())
                result = result + "::" + stringify(*p.in_repository_ptr());
            return result;
        }
    }

    struct ShowOneJobAttrs
    {
        std::ostream & output_stream;
        bool full;

        void visit(const FetchJob & job) const
        {
            output_stream << "label=\"fetch " << short_spec(job.origin_id_spec(), full) << "\", ";
            output_stream << "shape=ellipse, ";
            output_stream << "fillcolor=cadetblue, ";
            output_stream << "style=filled";
        }

        void visit(const InstallJob & job) const
        {
            output_stream << "label=\"" << short_spec(job.origin_id_spec(), full) << " -> " << job.destination_repository_name() << "\"";
            switch (job.destination_type())
            {
                case dt_install_to_slash:
                    output_stream << "shape=box, ";
                    output_stream << "fillcolor=royalblue, ";
                    break;

                case dt_create_binary:
                    output_stream << "shape=octagon, ";
                    output_stream << "fillcolor=aquamarine3, ";
                    break;

                case dt_install_to_chroot:
                    output_stream << "shape=septagon, ";
                    output_stream << "fillcolor=steelblue, ";
                    break;

                case last_dt:
                    break;
            }

            output_stream << "style=filled";
        }

        void visit(const UninstallJob & job) const
        {
            output_stream << "label=\"uninstall " << join(job.ids_to_remove_specs()->begin(), job.ids_to_remove_specs()->end(), ", ",
                    std::bind(&short_spec, std::placeholders::_1, full)) << "\"";
            output_stream << "shape=hexagon, ";
            output_stream << "fillcolor=slateblue, ";
            output_stream << "style=filled";
        }
    };

    void graph_jobs(
            const std::shared_ptr<Environment> &,
            const GraphJobsCommandLine & cmdline,
            const std::shared_ptr<const JobList<ExecuteJob> > & execute_job_list,
            std::ostream & output_stream)
    {
        output_stream << "digraph Jobs {" << endl;
        output_stream << "    graph [ splines=true ]" << endl;
        output_stream << "    node [ fontsize=8, fontname=sans, height=0, width=0 ]" << endl;

        for (auto j(execute_job_list->begin()), j_end(execute_job_list->end()) ;
                j != j_end ; ++j)
        {
            output_stream << "    job" << execute_job_list->number(j) << " [ ";
            (*j)->accept(ShowOneJobAttrs{output_stream, cmdline.graph_jobs_options.a_graph_jobs_full_names.specified()});
            output_stream << " ]" << endl;

            for (auto r((*j)->requirements()->begin()), r_end((*j)->requirements()->end()) ;
                     r != r_end ; ++r)
            {
                if (! cmdline.graph_jobs_options.a_graph_jobs_all_arrows.specified())
                    if (! (r->required_if() - jri_require_for_independent).any())
                        continue;

                output_stream << "    job" << execute_job_list->number(j) << " -> job" << r->job_number() << " [ ";
                if (r->required_if()[jri_require_always])
                    output_stream << " color=crimson";
                else if (r->required_if()[jri_require_for_satisfied])
                    output_stream << " color=indianred";
                else if (r->required_if()[jri_require_for_independent])
                    output_stream << " color=lightpink";

                if (! r->required_if()[jri_fetching])
                    output_stream << " style=dotted";

                output_stream << " ]" << endl;
            }

            output_stream << endl;
        }

        output_stream << "}" << endl;
    }

    int create_graph(
            const std::shared_ptr<Environment> &,
            const GraphJobsCommandLine & cmdline,
            const FSPath & src,
            const FSPath & dst)
    {
        Process process(ProcessCommand({ cmdline.program_options.a_graph_program.argument(),
                    "-T", cmdline.graph_jobs_options.a_graph_jobs_format.argument(),
                    stringify(src), "-o", stringify(dst)
                    }));

        return process.run().wait();
    }
}

int
GraphJobsCommand::run(
        const std::shared_ptr<Environment> & env,
        const std::shared_ptr<const Sequence<std::string > > & args,
        const std::shared_ptr<const Resolved> & maybe_resolved
        )
{
    GraphJobsCommandLine cmdline;
    cmdline.run(args, "CAVE", "CAVE_GRAPH_JOBS_OPTIONS", "CAVE_GRAPH_JOBS_CMDLINE");

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

    cmdline.import_options.apply(env);

    std::shared_ptr<const Resolved> resolved(maybe_resolved);
    if (! resolved)
    {
        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);
        Deserialiser deserialiser(env.get(), deser_stream);
        Deserialisation deserialisation("Resolved", deserialiser);
        resolved = make_shared_copy(Resolved::deserialise(deserialisation));
        close(fd);
    }

    std::shared_ptr<SafeOFStream> stream_if_file;
    if (! cmdline.graph_jobs_options.a_graph_jobs_basename.argument().empty())
        stream_if_file = std::make_shared<SafeOFStream>(FSPath(cmdline.graph_jobs_options.a_graph_jobs_basename.argument() + ".graphviz"), -1, true);

    int retcode(0);

    graph_jobs(env, cmdline, resolved->job_lists()->execute_job_list(), stream_if_file ? *stream_if_file : cout);

    if (stream_if_file && ! cmdline.graph_jobs_options.a_graph_jobs_format.argument().empty())
        retcode = create_graph(env, cmdline,
                FSPath(cmdline.graph_jobs_options.a_graph_jobs_basename.argument() + ".graphviz"),
                FSPath(cmdline.graph_jobs_options.a_graph_jobs_basename.argument() + "." + cmdline.graph_jobs_options.a_graph_jobs_format.argument()));

    return retcode;
}

int
GraphJobsCommand::run(
        const std::shared_ptr<Environment> & env,
        const std::shared_ptr<const Sequence<std::string > > & args)
{
    return run(env, args, make_null_shared_ptr());
}

std::shared_ptr<args::ArgsHandler>
GraphJobsCommand::make_doc_cmdline()
{
    return std::make_shared<GraphJobsCommandLine>();
}

CommandImportance
GraphJobsCommand::importance() const
{
    return ci_internal;
}