aboutsummaryrefslogtreecommitdiff
path: root/ruby/paludis_ruby.cc
blob: 7ce44f23bcd8b260ad22575caea1994ed4b25e35 (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011 Ciaran McCreesh
 * Copyright (c) 2006, 2007, 2008 Richard Brown
 *
 * 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 <paludis_ruby.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/singleton-impl.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/match_package.hh>
#include <paludis/version_spec.hh>
#include <paludis/version_operator.hh>
#include <paludis/about.hh>
#include <paludis/user_dep_spec.hh>
#include <paludis/action.hh>
#include <ruby.h>
#include <list>
#include <ctype.h>

using namespace paludis;
using namespace paludis::ruby;

template class Singleton<RegisterRubyClass>;

namespace paludis
{
    template<>
    struct Imp<RegisterRubyClass>
    {
        std::list<void (*)()> funcs;
    };
}

namespace
{
    static VALUE c_paludis_module;
    static VALUE c_name_error;
    static VALUE c_set_name_error;
    static VALUE c_category_name_part_error;
    static VALUE c_package_name_part_error;
    static VALUE c_bad_version_spec_error;
    static VALUE c_package_dep_spec_error;
    static VALUE c_ambiguous_package_name_error;
    static VALUE c_no_such_package_error;
    static VALUE c_no_such_repository_error;
    static VALUE c_configuration_error;
    static VALUE c_config_file_error;
    static VALUE c_action_failed_error;
    static VALUE c_action_aborted_error;
    static VALUE c_bad_version_operator_error;
    static VALUE c_got_a_set_not_a_package_dep_spec;

    /*
     * Document-method: match_package
     *
     * call-seq:
     *     match_package(environment, package_dep_spec, package_id, from_package_id, options) -> true or false
     *
     * Return whether the specified PackageID matches the specified PackageDepSpec.
     *
     */
    VALUE paludis_match_package(VALUE, VALUE en, VALUE a, VALUE t, VALUE f, VALUE o)
    {
        try
        {
            std::shared_ptr<Environment> env = value_to_environment(en);
            std::shared_ptr<const PackageDepSpec> spec = value_to_package_dep_spec(a);
            std::shared_ptr<const PackageID> target = value_to_package_id(t);
            std::shared_ptr<const PackageID> from_package_id = value_to_package_id(f, true);
            MatchPackageOptions options(value_to_match_package_options(o));
            return match_package(*env, *spec, target, from_package_id, options) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }

    }

    /*
     * Document-method: match_package_in_set
     *
     * call-seq:
     *     match_package_in_set(environment, set_spec_tree, package_id, options) -> true or false
     *
     * Return whether the specified PackageID matches the specified set.
     *
     */
    VALUE paludis_match_package_in_set(VALUE, VALUE en, VALUE a, VALUE t, VALUE o)
    {
        try
        {
            std::shared_ptr<Environment> env = value_to_environment(en);
            std::shared_ptr<const SetSpecTree> spec = value_to_dep_tree<SetSpecTree>(a);
            std::shared_ptr<const PackageID> target = value_to_package_id(t);
            MatchPackageOptions options(value_to_match_package_options(o));
            return match_package_in_set(*env, *spec, target, options) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }

    }

    /*
     * Document-method: version_spec_comparator
     *
     * call-seq:
     *     version_spec_comparator(operator, left_version_spec, right_version_spec) -> true of false
     *
     * Applies operator to left_version_spec and right_version_spec
     */
    VALUE paludis_version_spec_comparator(VALUE, VALUE op, VALUE left, VALUE right)
    {
        try
        {
            const VersionOperator vo = VersionOperator(StringValuePtr(op));
            const VersionSpec l = value_to_version_spec(left);
            const VersionSpec r = value_to_version_spec(right);
            if (vo.as_version_spec_comparator()(l, r))
                return true;
            return false;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

}

RegisterRubyClass::RegisterRubyClass() :
    _imp()
{
}

RegisterRubyClass::~RegisterRubyClass()
{
}

void
RegisterRubyClass::execute() const
{
    for (std::list<void (*)()>::const_iterator f(_imp->funcs.begin()), f_end(_imp->funcs.end()) ;
            f != f_end ; ++f)
        (*f)();
}

RegisterRubyClass::Register::Register(void (* f)())
{
    RegisterRubyClass::get_instance()->_imp->funcs.push_back(f);
}

void paludis::ruby::exception_to_ruby_exception(const std::exception & ee)
{
    if (0 != dynamic_cast<const paludis::InternalError *>(&ee))
        rb_raise(rb_eRuntimeError, "Unexpected paludis::InternalError: %s (%s)",
                dynamic_cast<const paludis::InternalError *>(&ee)->message().c_str(), ee.what());
    else if (0 != dynamic_cast<const paludis::GotASetNotAPackageDepSpec *>(&ee))
        rb_raise(c_got_a_set_not_a_package_dep_spec, dynamic_cast<const paludis::GotASetNotAPackageDepSpec *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::BadVersionSpecError *>(&ee))
        rb_raise(c_bad_version_spec_error, dynamic_cast<const paludis::BadVersionSpecError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::SetNameError *>(&ee))
        rb_raise(c_set_name_error, dynamic_cast<const paludis::SetNameError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::PackageNamePartError *>(&ee))
        rb_raise(c_package_name_part_error, dynamic_cast<const paludis::PackageNamePartError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::CategoryNamePartError *>(&ee))
        rb_raise(c_category_name_part_error, dynamic_cast<const paludis::CategoryNamePartError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::NameError *>(&ee))
        rb_raise(c_name_error, dynamic_cast<const paludis::NameError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::PackageDepSpecError *>(&ee))
        rb_raise(c_package_dep_spec_error, dynamic_cast<const paludis::PackageDepSpecError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee))
    {
        VALUE ex_args[2];
        ex_args[0] = rb_str_new2(dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee)->message().c_str());
        ex_args[1] = rb_ary_new();
        const AmbiguousPackageNameError * e = dynamic_cast<const paludis::AmbiguousPackageNameError *>(&ee);
        for (AmbiguousPackageNameError::OptionsConstIterator f(e->begin_options()), f_end(e->end_options()) ;
                f != f_end ; ++f)
            rb_ary_unshift(ex_args[1], rb_str_new2(stringify(*f).c_str()));
        rb_exc_raise(rb_class_new_instance(2, ex_args, c_ambiguous_package_name_error));
    }
    else if (0 != dynamic_cast<const paludis::NoSuchPackageError *>(&ee))
        rb_raise(c_no_such_package_error, dynamic_cast<const paludis::NoSuchPackageError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::NoSuchRepositoryError *>(&ee))
        rb_raise(c_no_such_repository_error, dynamic_cast<const paludis::NoSuchRepositoryError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::ConfigFileError *>(&ee))
        rb_raise(c_config_file_error, dynamic_cast<const paludis::ConfigFileError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::ConfigurationError *>(&ee))
        rb_raise(c_configuration_error, dynamic_cast<const paludis::ConfigurationError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::ActionFailedError *>(&ee))
        rb_raise(c_action_failed_error, dynamic_cast<const paludis::ActionFailedError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::ActionAbortedError *>(&ee))
        rb_raise(c_action_aborted_error, dynamic_cast<const paludis::ActionAbortedError *>(&ee)->message().c_str());
    else if (0 != dynamic_cast<const paludis::BadVersionOperatorError *>(&ee))
        rb_raise(c_bad_version_operator_error, dynamic_cast<const paludis::BadVersionOperatorError *>(&ee)->message().c_str());

    else if (0 != dynamic_cast<const paludis::Exception *>(&ee))
        rb_raise(rb_eRuntimeError, "Caught paludis::Exception: %s (%s)",
                dynamic_cast<const paludis::Exception *>(&ee)->message().c_str(), ee.what());
    else
        rb_raise(rb_eRuntimeError, "Unexpected std::exception: (%s)", ee.what());
}

std::string
paludis::ruby::value_case_to_RubyCase(const std::string & s)
{
    if (s.empty())
        return s;

    bool upper_next(true);
    std::string result;
    for (std::string::size_type p(0), p_end(s.length()) ; p != p_end ; ++p)
    {
        if ('_' == s[p] || ' ' == s[p])
            upper_next = true;
        else if (upper_next)
        {
            result.append(std::string(1, toupper(s[p])));
            upper_next = false;
        }
        else
            result.append(std::string(1, s[p]));
    }

    return result;
}

VALUE
paludis::ruby::paludis_module()
{
    return c_paludis_module;
}

/*
 * call-seq:
 *     query -> String or Nil
 *
 * Our query.
 */
VALUE
has_query_property_error_query(VALUE self)
{
    return rb_attr_get(self, rb_intern("query"));
}

/*
 * call-seq:
 *     failures -> Array
 *
 * Our failures
 */
VALUE
fetch_action_error_failures(VALUE self)
{
    return rb_attr_get(self, rb_intern("failures"));
}

static VALUE
ambiguous_package_name_error_init(int argc, VALUE* argv, VALUE self)
{
    VALUE options;

    options = (argc > 1) ? argv[--argc] : Qnil;
    rb_call_super(argc, argv);
    rb_iv_set(self, "options", options);
    return self;
}

/*
 * call-seq:
 *     options -> Array
 *
 * Our options
 */
VALUE
ambiguous_package_name_error_failures(VALUE self)
{
    return rb_attr_get(self, rb_intern("options"));
}

void PALUDIS_VISIBLE paludis::ruby::init()
{
    /*
     * Defined in create_ruby_doc.rb
     */
    c_paludis_module = rb_define_module("Paludis");
    c_name_error = rb_define_class_under(c_paludis_module, "NameError", rb_eRuntimeError);
    c_set_name_error = rb_define_class_under(c_paludis_module, "SetNameError", c_name_error);
    c_category_name_part_error = rb_define_class_under(c_paludis_module, "CategoryNamePartError", c_name_error);
    c_package_name_part_error = rb_define_class_under(c_paludis_module, "PackageNamePartError", c_name_error);
    c_bad_version_spec_error = rb_define_class_under(c_paludis_module, "BadVersionSpecError", c_name_error);
    c_package_dep_spec_error = rb_define_class_under(c_paludis_module, "PackageDepSpecError", rb_eRuntimeError);
    c_no_such_package_error = rb_define_class_under(c_paludis_module, "NoSuchPackageError", rb_eRuntimeError);
    c_no_such_repository_error = rb_define_class_under(c_paludis_module, "NoSuchRepositoryError", rb_eRuntimeError);
    c_configuration_error = rb_define_class_under(c_paludis_module, "ConfigurationError", rb_eRuntimeError);
    c_config_file_error = rb_define_class_under(c_paludis_module, "ConfigFileError", c_configuration_error);

    /*
     * Document-class: Paludis::ActionFailedError
     */
    c_action_failed_error = rb_define_class_under(c_paludis_module, "ActionFailedError", rb_eRuntimeError);

    /*
     * Document-class: Paludis::ActionAbortedError
     */
    c_action_aborted_error = rb_define_class_under(c_paludis_module, "ActionAbortedError", rb_eRuntimeError);

    /*
     * Document-class: Paludis::AmbiguousPackageNameError
     *
     * Thrown if an Environment query results in more than one matching Package.
     */
    c_ambiguous_package_name_error = rb_define_class_under(c_paludis_module, "AmbiguousPackageNameError", rb_eRuntimeError);
    rb_define_method(c_ambiguous_package_name_error, "initialize", RUBY_FUNC_CAST(&ambiguous_package_name_error_init), -1);
    rb_define_method(c_ambiguous_package_name_error, "options", RUBY_FUNC_CAST(&ambiguous_package_name_error_failures), 0);

    /*
     * Document-class: Paludis::GotASetNotAPackageDepSpec
     *
     * Thrown if parse_user_package_dep_spec gets a set.
     */
    c_got_a_set_not_a_package_dep_spec = rb_define_class_under(c_paludis_module, "GotASetNotAPackageDepSpec",
            rb_eRuntimeError);

    /*
     * Document-class: Paludis::BadVersionOperatorError
     *
     * Thrown if a bad version operator is encountered
     */
    c_bad_version_operator_error = rb_define_class_under(c_paludis_module, "BadVersionOperatorError", rb_eRuntimeError);

    rb_define_module_function(c_paludis_module, "match_package", RUBY_FUNC_CAST(&paludis_match_package), 5);
    rb_define_module_function(c_paludis_module, "match_package_in_set", RUBY_FUNC_CAST(&paludis_match_package_in_set), 4);
    rb_define_module_function(c_paludis_module, "version_spec_comparator", RUBY_FUNC_CAST(&paludis_version_spec_comparator), 3);

    rb_define_const(c_paludis_module, "Version", INT2FIX(PALUDIS_VERSION));
    rb_define_const(c_paludis_module, "VersionMajor", INT2FIX(PALUDIS_VERSION_MAJOR));
    rb_define_const(c_paludis_module, "VersionMinor", INT2FIX(PALUDIS_VERSION_MINOR));
    rb_define_const(c_paludis_module, "VersionMicro", INT2FIX(PALUDIS_VERSION_MICRO));
    rb_define_const(c_paludis_module, "VersionSuffix",
            rb_str_new2(stringify(PALUDIS_VERSION_SUFFIX).c_str()));
    rb_define_const(c_paludis_module, "GitHead",
            rb_str_new2(stringify(PALUDIS_GIT_HEAD).c_str()));
    RegisterRubyClass::get_instance()->execute();
}

bool
paludis::ruby::value_to_bool(VALUE v)
{
    if (Qfalse == v || Qnil == v)
        return false;
    return true;
}

VALUE
paludis::ruby::bool_to_value(bool b)
{
    return b ? Qtrue : Qfalse;
}

MatchPackageOptions
paludis::ruby::value_to_match_package_options(VALUE v)
{
    MatchPackageOptions o;
    for (int i(0) ; i < RARRAY_LEN(v) ; ++i)
    {
        VALUE entry(rb_ary_entry(v, i));
        Check_Type(entry, T_SYMBOL);
        if (SYM2ID(entry) == rb_intern("ignore_choice_constraints"))
            o += mpo_ignore_choice_constraints;
        else
            rb_raise(rb_eArgError, "Unknown MatchPackageOptions option '%s'", rb_obj_as_string(entry));
    }
    return o;
}

VALUE
paludis::ruby::match_package_options_to_value(const MatchPackageOptions & o)
{
    VALUE a(rb_ary_new());
    if (o[mpo_ignore_choice_constraints])
        rb_ary_push(a, ID2SYM(rb_intern("ignore_choice_constraints")));
    return a;
}

#ifdef ENABLE_RUBY_QA
paludis::ruby::RubyQAReporter::RubyQAReporter(VALUE* ruby_reporter)
{
    this->reporter = ruby_reporter;
}

void
paludis::ruby::RubyQAReporter::message(const QAMessage & msg)
{
    try
    {
        ID message_id = rb_intern("message");
        VALUE msg_val = qa_message_to_value(msg);
        rb_funcall(*(this->reporter), message_id, 1, msg_val);
    }
    catch (const std::exception & e)
    {
        exception_to_ruby_exception(e);
    }
}

void
paludis::ruby::RubyQAReporter::status(const std::string &)
{
}
#endif