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

/*
 * Copyright (c) 2009, 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_import.hh"
#include "exceptions.hh"
#include "resolve_common.hh"
#include <paludis/args/args.hh>
#include <paludis/args/do_help.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/join.hh>
#include <paludis/util/map.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/environment.hh>
#include <paludis/version_spec.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/generator.hh>
#include <paludis/selection.hh>
#include <paludis/filtered_generator.hh>
#include <paludis/filter.hh>
#include <paludis/package_id.hh>
#include <paludis/repository.hh>
#include <paludis/metadata_key.hh>
#include <paludis/stringify_formatter.hh>
#include <paludis/repository_factory.hh>
#include <paludis/package_database.hh>

#include <iostream>
#include <cstdlib>

#include "command_command_line.hh"

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

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

        virtual std::string app_synopsis() const
        {
            return "Import a package from a directory containing its image.";
        }

        virtual std::string app_description() const
        {
            return "Import a package from a directory containing its image. A named directory "
                "is treated as holding the contents to install, and a dummy package name is "
                "provided on the command line. Safe merge, unmerge, upgrade and replace support "
                "is provided, as is content tracking for installed files.";
        }

        args::ArgsGroup g_execution_options;
        args::SwitchArg a_execute;

        args::ArgsGroup g_contents_options;
        args::StringArg a_location;
        args::StringArg a_install_under;
        args::IntegerArg a_rewrite_ids_over_to_root;
        args::EnumArg a_strip;
        args::EnumArg a_preserve_work;

        args::ArgsGroup g_metadata_options;
        args::StringArg a_description;
        args::StringSetArg a_build_dependency;
        args::StringSetArg a_run_dependency;
        args::SwitchArg a_preserve_metadata;

        ImportCommandLine() :
            g_execution_options(main_options_section(), "Execution Options", "Control execution."),
            a_execute(&g_execution_options, "execute", 'x', "Execute the suggested actions", true),

            g_contents_options(main_options_section(), "Contents Options",
                    "Options controlling the content to install"),
            a_location(&g_contents_options, "location", 'l',
                    "Specify the directory containing the image to install (default: current directory)"),
            a_install_under(&g_contents_options, "install-under", 'u',
                    "Install under the specified directory, rather than /"),
            a_rewrite_ids_over_to_root(&g_contents_options, "rewrite-ids-over-to-root", 'r',
                    "Change any UID or GID over this value to 0 (-1 disables, default)"),
            a_strip(&g_contents_options, "strip", 's',
                    "Specify whether to strip and split objects being installed.",
                    args::EnumArg::EnumArgOptions
                    ("config",       'c',   "Use build_options: split and build_options: strip from user configuration")
                    ("always",       'a',   "Split and strip, regardless of user configuration")
                    ("never",        'n',   "Do not split or strip, regardless of user configuration"),
                    "config"),
            a_preserve_work(&g_contents_options, "preserve-work", 'p',
                    "Specify whether to avoid carrying out destructive merge actions (e.g. using rename() rather than copying)",
                    args::EnumArg::EnumArgOptions
                    ("config",       'c',   "Use build_options: preserve_work from user configuration")
                    ("always",       'a',   "Do not merge destructively, regardless of user configuration")
                    ("never",        'n',   "Allow desructive merges, regardless of user configuration"),
                    "config"),

            g_metadata_options(main_options_section(), "Metadata Options",
                    "Options specifying metadata for the package being installed"),
            a_description(&g_metadata_options, "description", 'D',
                    "Specify a description for the package"),
            a_build_dependency(&g_metadata_options, "build-dependency", 'B',
                    "Specify a build dependency. May be specified multiple times."),
            a_run_dependency(&g_metadata_options, "run-dependency", 'R',
                    "Specify a run dependency. May be specified multiple times."),
            a_preserve_metadata(&g_metadata_options, "preserve-metadata", 'P',
                    "If replacing a package previously installed using this command, copy its description "
                    "and dependencies", true)
        {
            add_usage_line("[ --location blah ] cat/pkg [ version ] [ slot ] "
                    "[ --execute ] [ -- options for 'cave resolve' ]");

            add_note("This command uses the same underlying logic as 'cave resolve'. Any option that is "
                    "valid for 'cave resolve' may be passed as a parameter following a '--'. For example, "
                    "'cave import cat/pkg --build-dependency some/dep -- --lazy' may be useful. As a "
                    "special case, '--execute' does not require a '--'.");
        }
    };

    struct OptionsForResolve :
        args::ArgsHandler
    {
        ResolveCommandLineResolutionOptions resolution_options;
        ResolveCommandLineExecutionOptions execution_options;
        ResolveCommandLineDisplayOptions display_options;
        ResolveCommandLineGraphJobsOptions graph_jobs_options;
        ResolveCommandLineProgramOptions program_options;

        OptionsForResolve() :
            resolution_options(this),
            execution_options(this),
            display_options(this),
            graph_jobs_options(this),
            program_options(this)
        {
        }

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

        std::string app_synopsis() const
        {
            return "";
        }

        std::string app_description() const
        {
            return "";
        }
    };

    std::string from_keys(const std::shared_ptr<const Map<std::string, std::string> > & m,
            const std::string & k)
    {
        Map<std::string, std::string>::ConstIterator mm(m->find(k));
        if (m->end() == mm)
            return "";
        else
            return mm->second;
    }
}

int
ImportCommand::run(
        const std::shared_ptr<Environment> & env,
        const std::shared_ptr<const Sequence<std::string > > & args
        )
{
    ImportCommandLine cmdline;
    OptionsForResolve resolve_cmdline;

    cmdline.run(args, "CAVE", "CAVE_IMPORT_OPTIONS", "CAVE_IMPORT_CMDLINE",
            args::ArgsHandlerOptions() + args::aho_separate_after_dashes);
    resolve_cmdline.run(cmdline.separate_after_dashes_args(),
            "CAVE", "CAVE_RESOLVE_OPTIONS", "CAVE_RESOLVE_CMDLINE");

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

    if (std::distance(cmdline.begin_parameters(), cmdline.end_parameters()) < 1 ||
            std::distance(cmdline.begin_parameters(), cmdline.end_parameters()) > 3)
        throw args::DoHelp("import takes between one and three parameters");

    QualifiedPackageName package(*cmdline.begin_parameters());
    VersionSpec version((next(cmdline.begin_parameters()) != cmdline.end_parameters()) ?
            *next(cmdline.begin_parameters()) : "0", user_version_spec_options());
    SlotName slot(
            (next(cmdline.begin_parameters()) != cmdline.end_parameters() &&
             next(cmdline.begin_parameters(), 2) != cmdline.end_parameters()) ?
            *next(cmdline.begin_parameters(), 2) : "0");

    std::string build_dependencies, run_dependencies, description;

    if (cmdline.a_preserve_metadata.specified())
    {
        std::shared_ptr<const PackageIDSequence> old_ids((*env)[selection::AllVersionsSorted(generator::Package(package))]);
        std::shared_ptr<const PackageID> old_id;
        for (PackageIDSequence::ConstIterator i(old_ids->begin()), i_end(old_ids->end()) ;
                i != i_end ; ++i)
        {
            if (! (*i)->repository()->format_key())
                continue;
            if ((*i)->repository()->format_key()->value() != "installed_unpackaged")
                continue;
            old_id = *i;
            break;
        }

        if (! old_id)
            throw args::DoHelp("--" + cmdline.a_preserve_metadata.long_name() + " specified but "
                    "no old ID available");

        StringifyFormatter f;
        if (old_id->short_description_key())
            description = old_id->short_description_key()->value();
        if (old_id->build_dependencies_key())
            build_dependencies = old_id->build_dependencies_key()->pretty_print_flat(f);
        if (old_id->run_dependencies_key())
            run_dependencies = old_id->run_dependencies_key()->pretty_print_flat(f);
    }

    if (cmdline.a_description.specified())
        description = cmdline.a_description.argument();
    if (cmdline.a_build_dependency.specified())
        build_dependencies = join(
                cmdline.a_build_dependency.begin_args(),
                cmdline.a_build_dependency.end_args(), ", ");
    if (cmdline.a_run_dependency.specified())
        run_dependencies = join(
                cmdline.a_run_dependency.begin_args(),
                cmdline.a_run_dependency.end_args(), ", ");

    std::string strip;
    if (cmdline.a_strip.argument() == "never")
        strip = "false";
    else if (cmdline.a_strip.argument() == "always")
        strip = "true";

    std::string preserve_work;
    if (cmdline.a_preserve_work.argument() == "never")
        preserve_work = "false";
    else if (cmdline.a_preserve_work.argument() == "always")
        preserve_work = "true";

    std::shared_ptr<Map<std::string, std::string> > keys(std::make_shared<Map<std::string, std::string>>());
    keys->insert("location", stringify(
                cmdline.a_location.specified() ?
                FSPath(cmdline.a_location.argument()) :
                FSPath::cwd()));
    keys->insert("install_under", stringify(
                cmdline.a_install_under.specified() ?
                FSPath(cmdline.a_install_under.argument()) :
                FSPath("/")));
    keys->insert("rewrite_ids_over_to_root", stringify(
                cmdline.a_rewrite_ids_over_to_root.specified() ?
                cmdline.a_rewrite_ids_over_to_root.argument() : -1));
    keys->insert("format", "unpackaged");
    keys->insert("name", stringify(package));
    keys->insert("version", stringify(version));
    keys->insert("slot", stringify(slot));
    keys->insert("description", description);
    keys->insert("build_dependencies", build_dependencies);
    keys->insert("run_dependencies", run_dependencies);
    keys->insert("strip", strip);
    keys->insert("preserve_work", preserve_work);
    std::shared_ptr<Repository> repo(RepositoryFactory::get_instance()->create(env.get(),
                std::bind(from_keys, keys, std::placeholders::_1)));
    env->package_database()->add_repository(10, repo);
    std::shared_ptr<const PackageIDSequence> ids(repo->package_ids(package));
    if (1 != std::distance(ids->begin(), ids->end()))
        throw InternalError(PALUDIS_HERE, "ids is '" + join(indirect_iterator(ids->begin()), indirect_iterator(
                        ids->end()), " ") + "'");

    resolve_cmdline.resolution_options.apply_shortcuts();
    resolve_cmdline.resolution_options.verify(env);

    if (cmdline.a_execute.specified())
        resolve_cmdline.resolution_options.a_execute.set_specified(true);

    std::shared_ptr<Sequence<std::pair<std::string, std::string> > > targets(std::make_shared<Sequence<std::pair<std::string, std::string> >>());
    targets->push_back(std::make_pair(stringify((*ids->begin())->name()), ""));

    std::shared_ptr<Sequence<std::string> > world_specs(std::make_shared<Sequence<std::string>>());
    world_specs->push_back(stringify((*ids->begin())->name()));

    return resolve_common(env,
            resolve_cmdline.resolution_options,
            resolve_cmdline.execution_options,
            resolve_cmdline.display_options,
            resolve_cmdline.graph_jobs_options,
            resolve_cmdline.program_options,
            keys, targets, world_specs, false);
}

std::shared_ptr<args::ArgsHandler>
ImportCommand::make_doc_cmdline()
{
    return std::make_shared<ImportCommandLine>();
}