aboutsummaryrefslogtreecommitdiff
path: root/ruby/environment.cc
blob: 23edba070a4ee45c76acc199badc77651a4ff8e5 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.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 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/environment/default/default_environment.hh>
#include <paludis/environment/default/default_config.hh>
#include <paludis/environment/no_config/no_config_environment.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x)

namespace
{
    static VALUE c_environment;
    static VALUE c_default_environment;
    static VALUE c_default_config;
    static VALUE c_no_config_environment;

    /*
     * call-seq:
     *     query_use(use_flag)
     *     query_use(use_flag, package_database_entry)
     *
     * Does the user want the specified USE flag set, either globally or for a PackageDatabaseEntry.
     */
    VALUE
    environment_query_use(int argc, VALUE * argv, VALUE self)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        try
        {
            if (1 == argc)
                return env_data->env_ptr->query_use(UseFlagName(StringValuePtr(argv[0])), 0) ? Qtrue : Qfalse;
            else if (2 == argc)
            {
                PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
                return env_data->env_ptr->query_use(UseFlagName(StringValuePtr(argv[0])), &pde) ? Qtrue : Qfalse;
            }
            else
                rb_raise(rb_eArgError, "Environment.query_use expects one or two arguments, but got %d", argc);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     accept_keyword(keyword)
     *     accept_keyword(keyword, package_database_entry)
     *
     * Is the specified KEYWORD set, either globally or for a PackageDatabaseEntry.
     */
    VALUE
    environment_accept_keyword(int argc, VALUE * argv, VALUE self)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        try
        {
            if (1 == argc)
                return env_data->env_ptr->accept_keyword(KeywordName(StringValuePtr(argv[0])), 0) ? Qtrue : Qfalse;
            else if (2 == argc)
            {
                PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
                return env_data->env_ptr->accept_keyword(KeywordName(StringValuePtr(argv[0])), &pde) ? Qtrue : Qfalse;
            }
            else
                rb_raise(rb_eArgError, "Environment.accept_keyword expects one or two arguments, but got %d", argc);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     accept_license(license)
     *     accept_license(license, package_database_entry)
     *
     * Is the specified LICENSE set, either globally or for a PackageDatabaseEntry.
     */
    VALUE
    environment_accept_license(int argc, VALUE * argv, VALUE self)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        try
        {
            if (1 == argc)
                return env_data->env_ptr->accept_license(std::string(StringValuePtr(argv[0])), 0) ? Qtrue : Qfalse;
            else if (2 == argc)
            {
                PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
                return env_data->env_ptr->accept_license(std::string(StringValuePtr(argv[0])), &pde) ? Qtrue : Qfalse;
            }
            else
                rb_raise(rb_eArgError, "Environment.accept_license expects one or two arguments, but got %d", argc);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     mask_reasons(package_database_entry)
     *
     * Fetch the MaskReasons for a PackageDatabaseEntry.
     */
    VALUE
    environment_mask_reasons(VALUE self, VALUE pde_value)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        PackageDatabaseEntry pde = value_to_package_database_entry(pde_value);
        try
        {
            MaskReasons r(env_data->env_ptr->mask_reasons(pde));
            return mask_reasons_to_value(r);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     query_user_masks(package_database_entry)
     *
     * Are there any user masks on a PackageDatabaseEntry?
     */
    VALUE
    environment_query_user_masks(VALUE self, VALUE pde_value)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        PackageDatabaseEntry pde = value_to_package_database_entry(pde_value);
        try
        {
             return env_data->env_ptr->query_user_masks(pde) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Fetch our PackageDatabase.
     */
    VALUE
    environment_package_database(VALUE self)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        try
        {
            return package_database_to_value(env_data->env_ptr->package_database());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     package_set(set_name)
     *
     * Fetch a named package set as a DepAtom.
     */
    VALUE
    environment_package_set(VALUE self, VALUE set_name)
    {
        EnvironmentData * env_data;
        Data_Get_Struct(self, EnvironmentData, env_data);

        try
        {
            return dep_atom_to_value(env_data->env_ptr->package_set(SetName(StringValuePtr(set_name))));
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Gets the config suffix.
     */
    VALUE
    default_config_config_suffix(VALUE)
    {
        try
        {
            return rb_str_new2(DefaultConfig::config_suffix().c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: root
     *
     * The ROOT.
     */
    /*
     * Document-method: bashrc_files
     *
     * Our bashrc files.
     */
    /*
     * Document-method: config_dir
     *
     * The config directory.
     */
    template <std::string (DefaultConfig::* m_) () const>
    struct ConfigStruct
    {
        static VALUE
        fetch(VALUE)
        {
            return rb_str_new2((((DefaultConfig::get_instance())->*m_)()).c_str());
        }
    };

    /*
     * call-seq:
     *     config_suffix=
     *
     * Set config suffig.
     */
    VALUE
    default_config_config_suffix_set(VALUE klass, VALUE str)
    {
        try
        {
            DefaultConfig::set_config_suffix(stringify(StringValuePtr(str)));
            return klass;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    no_config_environment_init(VALUE self, VALUE)
    {
        return self;
    }

    /*
     * call-seq:
     *     NoConfigEnvironment.new(dir) -> NoConfigEnvironment
     *
     * Create a new NoConfigEnvironment form the specified directory.
     */
    VALUE
    no_config_environment_new(VALUE self, VALUE s)
    {
        std::string path;
        try
        {
            if (T_STRING == TYPE(s))
                path = StringValuePtr(s);
            else if (rb_obj_is_kind_of(s, rb_cDir))
            {
                VALUE v = rb_funcall(s, rb_intern("path"), 0);
                path = StringValuePtr(v);
            }
            else
                rb_raise(rb_eTypeError, "NoConfigEnvironment.new expects a string or Dir");

            NoConfigEnvironment * e(new NoConfigEnvironment(NoConfigEnvironmentParams::create()
                        .repository_dir(FSEntry(path))
                        .write_cache(FSEntry("/var/empty"))
                        .accept_unstable(false)
                        .repository_type(ncer_auto)));
            EnvironmentData * ptr(new EnvironmentData(e, e));
            VALUE tdata(Data_Wrap_Struct(self, 0, &Common<EnvironmentData>::free, ptr));
            rb_obj_call_init(tdata, 1, &s);
            return tdata;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    VALUE
    default_environment_new(VALUE self)
    {
        try
        {
            EnvironmentData * ptr(new EnvironmentData(DefaultEnvironment::get_instance()));
            return Data_Wrap_Struct(self, 0, &Common<EnvironmentData>::free, ptr);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     portage_repository -> PortageRepository
     *
     * Return the PortageRepository in this environment
     */
    VALUE
    no_config_environment_portage_repository(VALUE self)
    {
        try
        {
            return portage_repository_to_value(value_to_no_config_environment(self)->portage_repository());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    void do_register_environment()
    {
        rb_require("singleton");
        /*
         * Document-class: Paludis::Environment
         *
         * Represents a working environment, which contains an available packages database and provides 
         * various methods for querying package visibility and options.
         */
        c_environment = environment_class();
        rb_funcall(c_environment, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_environment, "query_use", RUBY_FUNC_CAST(&environment_query_use), -1);
        rb_define_method(c_environment, "accept_keyword", RUBY_FUNC_CAST(&environment_accept_keyword), -1);
        rb_define_method(c_environment, "accept_license", RUBY_FUNC_CAST(&environment_accept_license), -1);
        rb_define_method(c_environment, "mask_reasons", RUBY_FUNC_CAST(&environment_mask_reasons), 1);
        rb_define_method(c_environment, "query_user_masks", RUBY_FUNC_CAST(&environment_query_user_masks), 1);
        rb_define_method(c_environment, "package_database", RUBY_FUNC_CAST(&environment_package_database), 0);
        rb_define_method(c_environment, "package_set", RUBY_FUNC_CAST(&environment_package_set), 1);

        /*
         * Document-class: Paludis::DefaultEnvironment
         *
         * The DefaultEnvironment is an Environment that correspons to the normal operating environment
         *
         */
        c_default_environment = rb_define_class_under(paludis_module(), "DefaultEnvironment", c_environment);
        rb_define_singleton_method(c_default_environment, "new", RUBY_FUNC_CAST(&default_environment_new), 0);
        rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")), rb_intern("included"), 1, c_default_environment);


        /*
         * Document-class: Paludis::DefaultConfig
         *
         * DefaultConfig is used by DefaultEnvironment to access the user's configuration settings from on-disk configuration files.
         */
        c_default_config = rb_define_class_under(paludis_module(), "DefaultConfig", rb_cObject);
        rb_funcall(c_default_config, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")), rb_intern("included"), 1, c_default_config);
        rb_define_singleton_method(c_default_config, "config_suffix",
                RUBY_FUNC_CAST(&default_config_config_suffix), 0);
        rb_define_singleton_method(c_default_config, "config_suffix=",
                RUBY_FUNC_CAST(&default_config_config_suffix_set), 1);
        rb_define_method(c_default_config, "config_dir", RUBY_FUNC_CAST((&ConfigStruct<&DefaultConfig::config_dir>::fetch)), 0);
        rb_define_method(c_default_config, "root", RUBY_FUNC_CAST((&ConfigStruct<&DefaultConfig::root>::fetch)), 0);
        rb_define_method(c_default_config, "bashrc_files", RUBY_FUNC_CAST((&ConfigStruct<&DefaultConfig::bashrc_files>::fetch)), 0);

        /*
         * Document-class: Paludis::NoConfigEnvironment
         *
         * An environment that uses a single repository, with no user configuration.
         */
        c_no_config_environment = no_config_environment_class();
        rb_define_singleton_method(c_no_config_environment, "new", RUBY_FUNC_CAST(&no_config_environment_new), 1);
        rb_define_method(c_no_config_environment, "initialize", RUBY_FUNC_CAST(&no_config_environment_init), 1);
        rb_define_method(c_no_config_environment, "portage_repository", RUBY_FUNC_CAST(&no_config_environment_portage_repository), 0);
    }
}

EnvironmentData *
paludis::ruby::value_to_environment_data(VALUE v)
{
    if (rb_obj_is_kind_of(v, c_environment))
    {
        EnvironmentData * v_ptr;
        Data_Get_Struct(v, EnvironmentData, v_ptr);
        return v_ptr;
    }
    else
    {
        rb_raise(rb_eTypeError, "Can't convert %s into EnvironmentData", rb_obj_classname(v));
    }
}

NoConfigEnvironment *
paludis::ruby::value_to_no_config_environment(VALUE v)
{
    if (rb_obj_is_kind_of(v, c_no_config_environment))
    {
        return static_cast<NoConfigEnvironment *>(value_to_environment_data(v)->env_ptr);
    }
    else
    {
        rb_raise(rb_eTypeError, "Can't convert %s into NoConfigEnvironment", rb_obj_classname(v));
    }
}

RegisterRubyClass::Register paludis_ruby_register_environment PALUDIS_ATTRIBUTE((used))
    (&do_register_environment);