aboutsummaryrefslogtreecommitdiff
path: root/python/repository.cc
blob: bbcce3862a8dfa0e70729c03ea25c144fc500a56 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Piotr JaroszyƄski
 *
 * 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 <python/paludis_python.hh>
#include <python/iterable.hh>

#include <paludis/repository.hh>
#include <paludis/repositories/e/e_repository.hh>
#include <paludis/repositories/fake/fake_repository.hh>
#include <paludis/repositories/fake/fake_package_id.hh>
#include <paludis/action.hh>
#include <paludis/package_id.hh>
#include <paludis/environment.hh>
#include <paludis/util/options.hh>
#include <paludis/util/wrapped_forward_iterator.hh>
#include <paludis/util/make_named_values.hh>

using namespace paludis;
using namespace paludis::python;
namespace bp = boost::python;

struct RepositoryWrapper :
    Repository,
    bp::wrapper<Repository>
{
    static RepositoryEnvironmentVariableInterface *
    get_environment_variable_interface(const Repository & self)
    {
        return self.environment_variable_interface();
    }

    static RepositoryProvidesInterface *
    get_provides_interface(const Repository & self)
    {
        return self.provides_interface();
    }

    static RepositoryVirtualsInterface *
    get_virtuals_interface(const Repository & self)
    {
        return self.virtuals_interface();
    }

    static RepositoryDestinationInterface *
    get_destination_interface(const Repository & self)
    {
        return self.destination_interface();
    }

    static PyObject *
    find_metadata(const Repository & self, const std::string & key)
    {
        Repository::MetadataConstIterator i(self.find_metadata(key));
        if (i != self.end_metadata())
            return bp::incref(bp::object(*i).ptr());
        else
            return Py_None;
    }
};

struct FakeRepositoryWrapper
{
    static std::shared_ptr<PackageID>
    add_version(FakeRepository & self, const QualifiedPackageName & qpn, const VersionSpec & vs)
    {
        return self.add_version(qpn, vs);
    }
};

// FIXME
//template <typename I_>
//struct repository_interface_to_python
//{
//    static PyObject *
//    convert(const I_ & i)
//    {
//        return 0;
//        return bp::incref(bp::object(bp::ptr(&i)).ptr());
//    }
//};
//
//template <typename I_>
//void register_repository_interface_to_python()
//{
//    bp::to_python_converter<I_, repository_interface_to_python<I_> >();
//}

namespace
{
    std::shared_ptr<FakeRepository>
    make_fake_repository(const Environment * const env, const RepositoryName & n)
    {
        return std::make_shared<FakeRepository>(make_named_values<FakeRepositoryParams>(
                        n::environment() = env,
                        n::name() = n
                        ));
    }
}

void expose_repository()
{
    /**
     * Repository
     */
    register_shared_ptrs_to_python<Repository>(rsp_const);
    bp::class_<RepositoryWrapper, std::shared_ptr<Repository>, boost::noncopyable>
        (
         "Repository",
         "A Repository provides a representation of a physical repository to a PackageDatabase.",
         bp::no_init
        )

        .add_property("name", &Repository::name,
                "[ro] RepositoryName\n"
                "Our name."
                )

        .def("has_category_named", &Repository::has_category_named,
                "has_category_named(CategoryNamePart) -> bool\n"
                "Do we have a category with the given name?"
            )

        .def("has_package_named", &Repository::has_package_named,
                "has_package_named(QualifiedPackageName) -> bool\n"
                "Do we have a package in the given category with the given name?"
            )

        .add_property("category_names", &Repository::category_names,
                "[ro] CategoryNamePartIterable\n"
                "Our category names."
                )

        .def("category_names_containing_package", &Repository::category_names_containing_package,
                "category_names_containing_package(PackageNamePart) -> CategoryNamePartIterable\n"
                "Fetch categories that contain a named package."
            )

        .def("package_names", &Repository::package_names,
                "package_names(CategoryNamePart) -> QualifiedPackageNameIterable\n"
                "Fetch our package names."
            )

        .def("package_ids", &Repository::package_ids, bp::with_custodian_and_ward_postcall<0, 1>(),
                "package_ids(QualifiedPackageName) -> PackageIDIterable\n"
                "Fetch our versions."
            )

        .def("some_ids_might_support_action", &Repository::some_ids_might_support_action,
                "some_ids_might_support_action(SupportsActionTestBase) -> bool\n"
                "Might some of our IDs support a particular action?\n\n"

                "Used to optimise PackageDatabase::query. If a repository doesn't\n"
                "support, say, InstallAction, a query can skip searching it\n"
                "entirely when looking for installable packages."
            )

        .add_property("environment_variable_interface",
                bp::make_function(&RepositoryWrapper::get_environment_variable_interface,
                    bp::return_internal_reference<>()),
                "[ro] RepositoryEnvironmentInterface"
                )

        .add_property("virtuals_interface", bp::make_function(&RepositoryWrapper::get_virtuals_interface,
                    bp::return_internal_reference<>()),
                "[ro] RepositoryVirtualsInterface"
                )

        .add_property("provides_interface", bp::make_function(&RepositoryWrapper::get_provides_interface,
                    bp::return_internal_reference<>()),
                "[ro] RepositoryProvidesInterface"
                )

        .add_property("destination_interface", bp::make_function(&RepositoryWrapper::get_destination_interface,
                    bp::return_internal_reference<>()),
                "[ro] RepositoryDestinationInterface"
                )

        .def("format_key", &Repository::format_key,
                "The format_key, if not None, holds our repository's format"
            )

        .def("location_key", &Repository::location_key,
                "The location_key, if not None, holds a file or directory containing "
                "our repository's data."
            )

        .def("installed_root_key", &Repository::installed_root_key,
                "The installed_root_key, if not None, specifies that we contain installed "
                "packages at the specified root."
            )

        .add_property("metadata", bp::range(&Repository::begin_metadata, &Repository::end_metadata),
                "[ro] Iterable of MetadataKey\n"
                "NEED_DOC"
                )

        .def("find_metadata", &RepositoryWrapper::find_metadata,
                "find_metadata(string) -> MetadataKey\n"
                "NEED_DOC"
            )
        ;

    /**
     * RepositoryEnvironmentVariableInterface
     */
    bp::class_<RepositoryEnvironmentVariableInterface, boost::noncopyable>
        (
         "RepositoryEnvironmentVariableInterface",
         "Interface for environment variable querying for repositories.",
         bp::no_init
        );

    /**
     * RepositoryVirtualsInterface
     */
    bp::class_<RepositoryVirtualsInterface, boost::noncopyable>
        (
         "RepositoryVirtualsInterface",
         "Interface for repositories that define virtuals.",
         bp::no_init
        );

    /**
     * RepositoryProvidesInterface
     */
    bp::class_<RepositoryProvidesInterface, boost::noncopyable>
        (
         "RepositoryProvidesInterface",
         "Interface for repositories that provide packages.",
         bp::no_init
        );

    /**
     * RepositoryDestinationInterface
     */
    bp::class_<RepositoryDestinationInterface, boost::noncopyable>
        (
         "RepositoryDestinationInterface",
         "Interface for repositories that can be used as an install destination.",
         bp::no_init
        );

    /**
     * FakeRepository
     */
    bp::implicitly_convertible<std::shared_ptr<FakeRepository>, std::shared_ptr<Repository> >();

    bp::class_<FakeRepository, std::shared_ptr<FakeRepository>, bp::bases<Repository>, boost::noncopyable>
        (
         "FakeRepository",
         "Fake repository for use in test cases.",
         bp::no_init
        )

        .def("__init__",
                bp::make_constructor(&make_fake_repository),
                "__init__(Environment, RepositoryName)"
            )

        .def("add_category", &FakeRepository::add_category)

        .def("add_package", &FakeRepository::add_package)

        .def("add_version", &FakeRepositoryWrapper::add_version)
        ;
}