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

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
 *
 * 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 as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * 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 "src/query.hh"
#include "src/colour.hh"
#include <paludis/paludis.hh>
#include <iostream>
#include <iomanip>
#include <string>
#include <functional>

/** \file
 * Handle the --query action for the main paludis program.
 */

namespace p = paludis;

void do_one_query(
        const p::Environment * const env,
        const std::string & q)
{
    p::Context local_context("When handling query '" + q + "':");

    /* we might have a dep atom, but we might just have a simple package name
     * without a category. either should work. */
    p::PackageDepAtom::Pointer atom(std::string::npos == q.find('/') ?
            new p::PackageDepAtom(env->package_database()->fetch_unique_qualified_package_name(
                    p::PackageNamePart(q))) :
            new p::PackageDepAtom(q));

    p::PackageDatabaseEntryCollection::ConstPointer entries(env->package_database()->query(atom));
    if (entries->empty())
        throw p::NoSuchPackageError(q);

    /* match! display it. */
    std::cout << "* " << colour(cl_package_name, entries->begin()->get<p::pde_name>());
    if (atom->version_spec_ptr())
        std::cout << " (" << atom->version_operator() << *atom->version_spec_ptr() << ")";
    std::cout << std::endl;

    /* find all repository names. */
    p::RepositoryNameCollection repo_names;
    p::PackageDatabaseEntryCollection::Iterator e(entries->begin()), e_end(entries->end());
    for ( ; e != e_end ; ++e)
        repo_names.append(e->get<p::pde_repository>());

    /* display versions, by repository. */
    p::RepositoryNameCollection::Iterator r(repo_names.begin()), r_end(repo_names.end());
    for ( ; r != r_end ; ++r)
    {
        std::cout << "    " << std::setw(22) << std::left <<
            (p::stringify(*r) + ":") << std::setw(0) << " ";

        std::string old_slot;
        for (e = entries->begin() ; e != e_end ; ++e)
            if (e->get<p::pde_repository>() == *r)
            {
                p::VersionMetadata::ConstPointer metadata(env->package_database()->fetch_metadata(*e));
                if (CommandLine::get_instance()->a_show_slot.specified())
                {
                    /* show the slot, if we're about to move onto a new slot */
                    std::string slot_name(metadata->get(p::vmk_slot));
                    if (old_slot.empty())
                        old_slot = slot_name;
                    else if (old_slot != slot_name)
                        std::cout << colour(cl_slot, "{:" + old_slot + "} ");
                    old_slot = slot_name;
                }

                const p::MaskReasons masks(env->mask_reasons(*e));

                if (masks.none())
                    std::cout << colour(cl_visible, e->get<p::pde_version>());
                else
                {
                    std::string reasons;
                    if (masks.test(p::mr_keyword))
                        reasons.append("K");
                    if (masks.test(p::mr_user_mask))
                        reasons.append("U");
                    if (masks.test(p::mr_profile_mask))
                        reasons.append("P");
                    if (masks.test(p::mr_repository_mask))
                        reasons.append("R");
                    if (masks.test(p::mr_eapi))
                        reasons.append("E");
                    std::cout << colour(cl_masked, "(" + stringify(e->get<p::pde_version>()) + ")" + reasons);
                }
                /// \todo ^^ text description of masks

                if (e == entries->last())
                    std::cout << "*";
                std::cout << " ";
            }

        /* still need to show the slot for the last item */
        if (CommandLine::get_instance()->a_show_slot.specified())
            std::cout << colour(cl_slot, "{:" + old_slot + "} ");

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

    /* display metadata */
    p::VersionMetadata::ConstPointer metadata(env->package_database()->fetch_metadata(*entries->last()));
    std::cout << "    " << std::setw(22) << std::left << "Homepage:" << std::setw(0) <<
        " " << metadata->get(p::vmk_homepage) << std::endl;
    std::cout << "    " << std::setw(22) << std::left << "Description:" << std::setw(0) <<
        " " << metadata->get(p::vmk_description) << std::endl;
    if (CommandLine::get_instance()->a_show_license.specified())
        std::cout << "    " << std::setw(22) << std::left << "License:" << std::setw(0) <<
            " " << metadata->get(p::vmk_license) << std::endl;

    /* blank line */
    std::cout << std::endl;
}

int do_query()
{
    int return_code(0);

    p::Context context("When performing query action from command line:");
    p::Environment * const env(p::DefaultEnvironment::get_instance());

    CommandLine::ParametersIterator q(CommandLine::get_instance()->begin_parameters()),
        q_end(CommandLine::get_instance()->end_parameters());
    for ( ; q != q_end ; ++q)
    {
        try
        {
            do_one_query(env, *q);
        }
        catch (const p::NameError & e)
        {
            return_code |= 1;
            std::cout << std::endl;
            std::cerr << "Query error:" << std::endl;
            std::cerr << "  * " << e.backtrace("\n  * ") << e.message() << std::endl;
            std::cerr << std::endl;
        }
        catch (const p::PackageDatabaseLookupError & e)
        {
            return_code |= 1;
            std::cout << std::endl;
            std::cerr << "Query error:" << std::endl;
            std::cerr << "  * " << e.backtrace("\n  * ") << e.message() << std::endl;
            std::cerr << std::endl;
        }
    }

    return return_code;
}