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

/*
 * Copyright (c) 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_print_spec.hh"
#include "exceptions.hh"
#include "format_string.hh"
#include <paludis/args/args.hh>
#include <paludis/args/do_help.hh>
#include <paludis/util/visitor_cast.hh>
#include <paludis/util/set.hh>
#include <paludis/util/iterator_funcs.hh>
#include <paludis/util/options.hh>
#include <paludis/util/map.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/make_null_shared_ptr.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/join.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/environment.hh>
#include <paludis/metadata_key.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/additional_package_dep_spec_requirement.hh>
#include <paludis/partially_made_package_dep_spec.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/version_operator.hh>
#include <paludis/version_requirements.hh>
#include <iostream>
#include <algorithm>

#include "command_command_line.hh"

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

namespace
{
    struct PrintSpecCommandLine :
        CaveCommandCommandLine
    {
        virtual std::string app_name() const
        {
            return "cave print-spec";
        }

        virtual std::string app_synopsis() const
        {
            return "Prints a dependency spec.";
        }

        virtual std::string app_description() const
        {
            return "Parses a dependency spec and prints it out, possibly after applying certain modifications. No "
                "formatting is used, making the output suitable for parsing by scripts.";
        }

        args::ArgsGroup g_modifications;
        args::StringArg a_package;
        args::StringArg a_slot_requirement;
        args::StringArg a_in_repository;
        args::StringArg a_from_repository;
        args::StringArg a_installable_to_repository;
        args::StringArg a_installed_at_path;
        args::StringArg a_installable_to_path;
        args::StringArg a_package_part;
        args::StringArg a_category_part;
        args::StringSetArg a_version_requirement;
        args::EnumArg a_version_requirements_mode;
        args::StringSetArg a_additional_requirement;

        PrintSpecCommandLine() :
            g_modifications(main_options_section(), "Modification Options", "Options for modifying the spec. If an empty string is "
                    "specified for a modification, that requirement is removed."),
            a_package(&g_modifications, "package", '\0', "Specify the cat/pkg requirement."),
            a_slot_requirement(&g_modifications, "slot", '\0', "Specify the slot requirement."),
            a_in_repository(&g_modifications, "in-repository", '\0', "Specify the in-repository requirement."),
            a_from_repository(&g_modifications, "from-repository", '\0', "Specify the from-repository requirement."),
            a_installable_to_repository(&g_modifications, "installable-to-repository", '\0', "Specify the installable-to-repository requirement."),
            a_installed_at_path(&g_modifications, "installed-at-path", '\0', "Specify the installed-at-path requirement."),
            a_installable_to_path(&g_modifications, "installable-to-path", '\0', "Specify the installable-to-path requirement."),
            a_package_part(&g_modifications, "package-part", '\0', "Specify the /pkg requirement."),
            a_category_part(&g_modifications, "category-part", '\0', "Specify the cat/ requirement."),
            a_version_requirement(&g_modifications, "version-requirement", '\0', "Specify a version requirement. May be specified "
                    "multiple times. If specified at all, replaces all version requirements. Use a single empty string to remove "
                    "all version requirements."),
            a_version_requirements_mode(&g_modifications, "version-requirements-mode", '\0', "Specify the mode of version requirements.",
                    args::EnumArg::EnumArgOptions
                    ("default", "Do not change the version requirements mode")
                    ("and",     "And")
                    ("or",      "Or"),
                    "default"),
            a_additional_requirement(&g_modifications, "additional-requirement", '\0', "Specify an additional requirement. May be "
                    "specified multiple times.")
        {
            add_usage_line("spec");
        }
    };

    void do_one_spec(
            const PackageDepSpec & spec,
            const PrintSpecCommandLine & cmdline
            )
    {
        PartiallyMadePackageDepSpec s(spec);

        if (cmdline.a_package.specified())
        {
            if (cmdline.a_package.argument().empty())
                s.clear_package();
            else
                s.package(QualifiedPackageName(cmdline.a_package.argument()));
        }

        if (cmdline.a_slot_requirement.specified())
        {
            if (cmdline.a_slot_requirement.argument().empty())
            {
                s.clear_any_slot();
                s.clear_exact_slot();
            }
            else
            {
                s.clear_any_slot();
                s.exact_slot_constraint(SlotName(cmdline.a_slot_requirement.argument()), false);
            }
        }

        if (cmdline.a_in_repository.specified())
        {
            if (cmdline.a_in_repository.argument().empty())
                s.clear_in_repository();
            else
                s.in_repository(RepositoryName(cmdline.a_in_repository.argument()));
        }

        if (cmdline.a_from_repository.specified())
        {
            if (cmdline.a_from_repository.argument().empty())
                s.clear_from_repository();
            else
                s.from_repository(RepositoryName(cmdline.a_from_repository.argument()));
        }

        if (cmdline.a_installable_to_repository.specified())
        {
            if (cmdline.a_installable_to_repository.argument().empty())
                s.clear_installable_to_repository();
            else
            {
                std::string repo(cmdline.a_installable_to_repository.argument());
                bool include_masked(false);
                if ('?' == repo.at(repo.length() - 1))
                {
                    repo.erase(repo.length() - 1);
                    include_masked = true;
                }

                s.installable_to_repository(RepositoryName(repo), include_masked);
            }
        }

        if (cmdline.a_installed_at_path.specified())
        {
            if (cmdline.a_installed_at_path.argument().empty())
                s.clear_installed_at_path();
            else
                s.installed_at_path(FSPath(cmdline.a_installed_at_path.argument()));
        }

        if (cmdline.a_installable_to_path.specified())
        {
            if (cmdline.a_installable_to_path.argument().empty())
                s.clear_installable_to_path();
            else
            {
                std::string path(cmdline.a_installable_to_path.argument());
                bool include_masked(false);
                if ('?' == path.at(path.length() - 1))
                {
                    path.erase(path.length() - 1);
                    include_masked = true;
                }

                s.installable_to_path(FSPath(path), include_masked);
            }
        }

        if (cmdline.a_package_part.specified())
        {
            if (cmdline.a_package_part.argument().empty())
                s.clear_package_name_part();
            else
                s.package_name_part(PackageNamePart(cmdline.a_package_part.argument()));
        }

        if (cmdline.a_category_part.specified())
        {
            if (cmdline.a_category_part.argument().empty())
                s.clear_category_name_part();
            else
                s.category_name_part(CategoryNamePart(cmdline.a_category_part.argument()));
        }

        if (cmdline.a_version_requirement.specified())
        {
            s.clear_version_requirements();

            for (args::StringSetArg::ConstIterator a(cmdline.a_version_requirement.begin_args()),
                    a_end(cmdline.a_version_requirement.end_args()) ;
                    a != a_end ; ++a)
                if (! a->empty())
                {
                    std::string::size_type p(a->find_first_not_of("=<>~"));
                    if (std::string::npos == p)
                        throw args::DoHelp("--" + cmdline.a_version_requirement.long_name() + " arguments should be in the form =1.23");

                    std::string op(a->substr(0, p)), ver(a->substr(p));

                    s.version_requirement(make_named_values<VersionRequirement>(
                                n::version_operator() = VersionOperator(op),
                                n::version_spec() = VersionSpec(ver, {})
                                ));
                }
        }

        if (cmdline.a_version_requirements_mode.specified())
        {
            if (cmdline.a_version_requirements_mode.argument() == "and")
                s.version_requirements_mode(vr_and);
            else if (cmdline.a_version_requirements_mode.argument() == "or")
                s.version_requirements_mode(vr_or);
            else
                throw args::DoHelp("Argument for --" + cmdline.a_version_requirements_mode.long_name() + " unrecognised");
        }

        if (cmdline.a_additional_requirement.specified())
        {
            s.clear_additional_requirements();

            for (args::StringSetArg::ConstIterator a(cmdline.a_additional_requirement.begin_args()),
                    a_end(cmdline.a_additional_requirement.end_args()) ;
                    a != a_end ; ++a)
                if (! a->empty())
                    s.additional_requirement(std::make_shared<UserKeyRequirement>(*a));
        }

        cout << PackageDepSpec(s) << endl;
    }
}

int
PrintSpecCommand::run(
        const std::shared_ptr<Environment> & env,
        const std::shared_ptr<const Sequence<std::string > > & args
        )
{
    PrintSpecCommandLine cmdline;
    cmdline.run(args, "CAVE", "CAVE_PRINT_SPEC_OPTIONS", "CAVE_PRINT_SPEC_CMDLINE");

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

    if (1 != std::distance(cmdline.begin_parameters(), cmdline.end_parameters()))
        throw args::DoHelp("print-spec takes exactly one parameter");

    PackageDepSpec spec(parse_user_package_dep_spec(*cmdline.begin_parameters(), env.get(), { updso_allow_wildcards }));
    do_one_spec(spec, cmdline);

    return EXIT_SUCCESS;
}

std::shared_ptr<args::ArgsHandler>
PrintSpecCommand::make_doc_cmdline()
{
    return std::make_shared<PrintSpecCommandLine>();
}

CommandImportance
PrintSpecCommand::importance() const
{
    return ci_scripting;
}