aboutsummaryrefslogtreecommitdiff
path: root/ruby/repository.cc
blob: 53a592aa8c5491b915c543c791d1e27dce458ede (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 * Copyright (c) 2006, 2007 Richard Brown <mynamewasgone@gmail.com>
 *
 * 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/repository.hh>
#include <paludis/repositories/gentoo/portage_repository.hh>
#include <paludis/util/stringify.hh>
#include <ruby.h>

using namespace paludis;
using namespace paludis::ruby;

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

namespace
{
    static VALUE c_repository;
    static VALUE c_repository_info;
    static VALUE c_repository_info_section;
    static VALUE c_portage_repository;
    static VALUE c_portage_repository_profiles_desc_line;

    /*
     * call-seq:
     *     name -> String
     *
     * Returns our name.
     */
    VALUE
    repository_name(VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return rb_str_new2(stringify((*self_ptr)->name()).c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     format -> String
     *
     * Returns our format.
     */
    VALUE
    repository_format(VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return rb_str_new2(stringify((*self_ptr)->format()).c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     has_category_named?(category_name) -> true or false
     *
     * Do we have a category with the given name?
     */
    VALUE
    repository_has_category_named(VALUE self, VALUE cat)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return (*self_ptr)->has_category_named(CategoryNamePart(StringValuePtr(cat))) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    VALUE

    //TODO why do we need Document-method here?
    /*
     * Document-method: has_package_named?
     *
     * call-seq:
     *     has_package_named?(package_name) -> true or false
     *
     * Do we have a package with the given name?
     */
    repository_has_package_named(VALUE self, VALUE name)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return (*self_ptr)->has_package_named(value_to_qualified_package_name(name)) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     has_version?(package_name, version) -> true or false
     *
     * Do we have a version spec?
     */
    VALUE
    repository_has_version(VALUE self, VALUE name, VALUE version)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return (*self_ptr)->has_version(value_to_qualified_package_name(name),
                    value_to_version_spec(version)) ? Qtrue : Qfalse;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     *  call-seq:
     *      category_names -> Array
     *      category_names {|category_name| block } -> Nil
     *
     * Returns the names of all categories, either as an Array or as the parameters to a block.
     */
    VALUE
    repository_category_names(VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            if (rb_block_given_p())
            {
                std::tr1::shared_ptr<const CategoryNamePartCollection> c((*self_ptr)->category_names());
                for (CategoryNamePartCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                    rb_yield(rb_str_new2(stringify(*i).c_str()));
                return Qnil;
            }
            VALUE result(rb_ary_new());
            std::tr1::shared_ptr<const CategoryNamePartCollection> c((*self_ptr)->category_names());
            for (CategoryNamePartCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                rb_ary_push(result, rb_str_new2(stringify(*i).c_str()));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     *  call-seq:
     *      category_names_containing_package(package) -> Array
     *      category_names_containing_package(package) {|category_name| block } -> Nil
     *
     * Returns the names of all categories containing the given package, either as an Array
     * or as the parameters to a block.
     */
    VALUE
    repository_category_names_containing_package(VALUE self, VALUE pkg)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            PackageNamePart package(StringValuePtr(pkg));

            if (rb_block_given_p())
            {
                std::tr1::shared_ptr<const CategoryNamePartCollection> c((*self_ptr)->category_names_containing_package(package));
                for (CategoryNamePartCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                    rb_yield(rb_str_new2(stringify(*i).c_str()));
                return Qnil;
            }
            VALUE result(rb_ary_new());
            std::tr1::shared_ptr<const CategoryNamePartCollection> c((*self_ptr)->category_names_containing_package(package));
            for (CategoryNamePartCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                rb_ary_push(result, rb_str_new2(stringify(*i).c_str()));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     *  call-seq:
     *      package_names(category) -> Array
     *      package_names(category) {|qualified_package_name| block } -> Nil
     *
     * Returns the names of all packages within the given category, either as an Array,
     * or as the parameters to a block.
     */
    VALUE
    repository_package_names(VALUE self, VALUE cat)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            CategoryNamePart category(StringValuePtr(cat));

            if (rb_block_given_p())
            {
                std::tr1::shared_ptr<const QualifiedPackageNameCollection> c((*self_ptr)->package_names(category));
                for (QualifiedPackageNameCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                    rb_yield(qualified_package_name_to_value(*i));
                return Qnil;
            }
            VALUE result(rb_ary_new());
            std::tr1::shared_ptr<const QualifiedPackageNameCollection> c((*self_ptr)->package_names(category));
            for (QualifiedPackageNameCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                rb_ary_push(result, qualified_package_name_to_value(*i));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     version_specs(qualified_package_name) -> Array
     *     version_specs(qualified_package_name) {|version_spec| block } -> Qnil
     *
     * Returns the versions for the given package, either as an Array, or as the parameters
     * to a block.
     */
    VALUE
    repository_version_specs(VALUE self, VALUE qpn)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            QualifiedPackageName q = value_to_qualified_package_name(qpn);

            if (rb_block_given_p())
            {
                std::tr1::shared_ptr<const VersionSpecCollection> c((*self_ptr)->version_specs(q));
                for (VersionSpecCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                    rb_yield(version_spec_to_value(*i));
                return Qnil;
            }
            VALUE result(rb_ary_new());
            std::tr1::shared_ptr<const VersionSpecCollection> c((*self_ptr)->version_specs(q));
            for (VersionSpecCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i)
                rb_ary_push(result, version_spec_to_value(*i));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     version_metadata(qualified_package_name, version_spec) -> VersionMetadata
     *
     * Fetch metadata.
     */
    VALUE
    repository_version_metadata(VALUE self, VALUE name, VALUE version)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return version_metadata_to_value((*self_ptr)->version_metadata(value_to_qualified_package_name(name),
                        value_to_version_spec(version)));
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: installable_interface
     *
     * call-seq:
     *     installable_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: installed_interface
     *
     * call-seq:
     *     installed_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: mask_interface
     *
     * call-seq:
     *     mask_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: news_interface
     *
     * call-seq:
     *     news_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: sets_interface
     *
     * call-seq:
     *     sets_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: uninstallable_interface
     *
     * call-seq:
     *     uninstallable_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: use_interface
     *
     * call-seq:
     *     use_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: world_interface
     *
     * call-seq:
     *     world_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: mirrors_interface
     *
     * call-seq:
     *     mirrors_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: environment_variable_interface
     *
     * call-seq:
     *     environment_variable_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: provides_interface
     *
     * call-seq:
     *     provides_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    /*
     * Document-method: virtuals_interface
     *
     * call-seq:
     *     virtuals_interface -> self or Nil
     *
     * Returns self if the repository supports the interface, otherwise Nil.
     */
    template <typename T_, T_ * RepositoryCapabilities::* m_>
    struct Interface
    {
        static VALUE fetch(VALUE self)
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            return ((**self_ptr).*m_) ? self : Qnil;
        }
    };

    /*
     * call-seq:
     *      repository_info(verbose) -> RepositoryInfo
     *
     * Fetch our RepositoryInfo
     */
    VALUE
    repository_info(VALUE self, VALUE verbose)
    {
        std::tr1::shared_ptr<const Repository> * self_ptr;
        Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);

        std::tr1::shared_ptr<const RepositoryInfo> * p = new std::tr1::shared_ptr<const RepositoryInfo>((*self_ptr)->info(Qfalse == verbose));
        return Data_Wrap_Struct(c_repository_info, 0, &Common<std::tr1::shared_ptr<const RepositoryInfo> >::free, p);
    }

    /*
     * call-seq:
     *     info_sections -> Array
     *
     * Fetch an array of our sections.
     */
    VALUE
    repository_info_sections(VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const RepositoryInfo> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const RepositoryInfo>, self_ptr);

            VALUE result(rb_ary_new());
            for (RepositoryInfo::SectionIterator i((*self_ptr)->begin_sections()),
                    i_end((*self_ptr)->end_sections()) ; i != i_end ; ++i)
            {
                std::tr1::shared_ptr<const RepositoryInfoSection> * s(new std::tr1::shared_ptr<const RepositoryInfoSection>(*i));
                rb_ary_push(result, Data_Wrap_Struct(c_repository_info_section, 0, &Common<std::tr1::shared_ptr<const RepositoryInfo> >::free, s));
            }
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     kvs -> Hash
     *
     * Returns the key/value pairs within the section.
     */
    VALUE
    repository_info_section_kvs(VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const RepositoryInfoSection> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const RepositoryInfoSection>, self_ptr);

            VALUE result(rb_hash_new());
            for (RepositoryInfoSection::KeyValueIterator i((*self_ptr)->begin_kvs()),
                    i_end((*self_ptr)->end_kvs()) ; i != i_end ; ++i)
                rb_hash_aset(result, rb_str_new2(i->first.c_str()), rb_str_new2(i->second.c_str()));
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     header -> String
     *
     * Our heading
     */
    VALUE
    repository_info_section_header(VALUE self)
    {
        std::tr1::shared_ptr<const RepositoryInfoSection> * self_ptr;
        Data_Get_Struct(self, std::tr1::shared_ptr<const RepositoryInfoSection>, self_ptr);
        return rb_str_new2((*self_ptr)->heading().c_str());
    }

    /*
     * call-seq:
     *     contents(qualified_package_name, version_spec) -> Contents or Nil
     *
     * Fetches the package contents, if the Repository includes the installed_interface
     */
    VALUE
    repository_contents(VALUE self, VALUE qpn, VALUE vs)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            const RepositoryContentsInterface * const contents_interface ((**self_ptr).contents_interface);
            if (contents_interface)
            {
                return contents_to_value(
                        contents_interface->contents(
                            value_to_qualified_package_name(qpn),
                            value_to_version_spec(vs)
                            )
                        );
            }
            else
            {
                return Qnil;
            }
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     installed_time(qualified_package_name, version_spec) -> Array or Nil
     *
     * Fetches the package install time, if the Repository includes the installed_interface
     */
    VALUE
    repository_installed_time(VALUE self, VALUE qpn, VALUE vs)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            const RepositoryInstalledInterface * const installed_interface ((**self_ptr).installed_interface);
            if (installed_interface)
            {
                return rb_time_new(installed_interface->installed_time(
                            value_to_qualified_package_name(qpn),
                            value_to_version_spec(vs)
                            ), 0);
            }
            else
            {
                return Qnil;
            }
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     profiles -> Array
     *
     * Fetch an array of our profiles, as PortageRepositoryProfilesDescLine.
     */
    VALUE
    portage_repository_profiles(VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const PortageRepository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const PortageRepository>, self_ptr);

            VALUE result(rb_ary_new());
            for (PortageRepository::ProfilesIterator i((*self_ptr)->begin_profiles()),
                    i_end((*self_ptr)->end_profiles()) ; i != i_end ; ++i)
            {
                rb_ary_push(result, portage_repository_profiles_desc_line_to_value(*i));
            }
            return result;
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     find_profile(profile_location) -> PortageRepositoryProfilesDescLine
     *
     * Fetches the named profile.
     */
    VALUE
    portage_repository_find_profile(VALUE self, VALUE profile)
    {
        try
        {
            std::tr1::shared_ptr<const PortageRepository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const PortageRepository>, self_ptr);

            PortageRepository::ProfilesIterator p((*self_ptr)->find_profile(FSEntry(StringValuePtr(profile))));

            if (p == (*self_ptr)->end_profiles())
                return Qnil;

            return portage_repository_profiles_desc_line_to_value(*p);
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * call-seq:
     *     profile_variable(variable) -> String
     *
     * Fetches the named variable.
     */
    VALUE
    portage_repository_profile_variable(VALUE self, VALUE var)
    {
        try
        {
            std::tr1::shared_ptr<const PortageRepository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const PortageRepository>, self_ptr);
            return rb_str_new2(((*self_ptr)->profile_variable(StringValuePtr(var))).c_str());
        }
        catch (const std::exception & e)
        {
            exception_to_ruby_exception(e);
        }
    }

    /*
     * Document-method: arch
     *
     * call-seq:
     *     arch -> String
     *
     * Fetch arch for this PortageRepositoryProfilesDescLine.
     */
    /*
     * Document-method: status
     *
     * call-seq:
     *     status -> String
     *
     * Fetch status for this PortageRepositoryProfilesDescLine.
     */
    /*
     * Document-method: path
     *
     * call-seq:
     *     path -> String
     *
     * Fetch path to this PortageRepositoryProfilesDescLine.
     */
    template <typename T_, T_ PortageRepositoryProfilesDescLine::* m_>
    struct DescLineValue
    {
        static VALUE
        fetch(VALUE self)
        {
            PortageRepositoryProfilesDescLine * ptr;
            Data_Get_Struct(self, PortageRepositoryProfilesDescLine, ptr);
            return rb_str_new2(stringify((*ptr).*m_).c_str());
        }
    };

    /*
     * Document-method: query_use
     *
     * call-seq:
     *     query_use(use_flag) -> true or false or nil
     *     query_use(use_flag, package_database_entry) -> true or false or nil
     *
     * Query the state of the specified use flag: true if set, false
     * if unset, nil if unspecified.  nil if the repository doesn't
     * implement use_interface.
     */
    /*
     * Document-method: query_use_mask
     *
     * call-seq:
     *     query_use_mask(use_flag) -> true or false or nil
     *     query_use_mask(use_flag, package_database_entry) -> true or false or nil
     *
     * Query whether the specified use flag is masked.  nil if the
     * repository doesn't implement use_interface.
     */
    /*
     * Document-method: query_use_force
     *
     * call-seq:
     *     query_use_force(use_flag) -> true or false or nil
     *     query_use_force(use_flag, package_database_entry) -> true or false or nil
     *
     * Query whether the specified use flag is forced.  nil if the
     * repository doesn't implement use_interface.
     */

    template <typename T_, T_ (RepositoryUseInterface::* m_) (const UseFlagName &, const PackageDatabaseEntry *) const> struct QueryUseMessage;

    template <typename T_, T_ trueval_, T_ falseval_, T_ (RepositoryUseInterface::* m_) (const UseFlagName &, const PackageDatabaseEntry *) const>
    struct QueryUse
    {
        static VALUE
        query(int argc, VALUE * argv, VALUE self)
        {
            try
            {
                std::tr1::shared_ptr<const Repository> * self_ptr;
                Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
                const RepositoryUseInterface * const use_interface ((**self_ptr).use_interface);

                if (use_interface)
                {
                    if (1 != argc && 2 != argc) {
                        rb_raise(rb_eArgError, QueryUseMessage<T_, m_>::message, argc);
                    }

                    T_ status;

                    if (1 == argc)
                        status = ((*use_interface).*m_)(UseFlagName(StringValuePtr(argv[0])), 0);
                    else
                    {
                        PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
                        status = ((*use_interface).*m_)(UseFlagName(StringValuePtr(argv[0])), &pde);
                    }

                    return status == trueval_ ? Qtrue : status == falseval_ ? Qfalse : Qnil;
                }
                else
                {
                    return Qnil;
                }
            }
            catch (const std::exception & e)
            {
                exception_to_ruby_exception(e);
            }
        }
    };

    template<>
    struct QueryUseMessage<UseFlagState, &RepositoryUseInterface::query_use>
    {
        static const char * message;
    };
    const char * QueryUseMessage<UseFlagState, &RepositoryUseInterface::query_use>::message = "Repository.query_use expects one or two arguments, but got %d";

    template<>
    struct QueryUseMessage<bool, &RepositoryUseInterface::query_use_mask>
    {
        static const char * message;
    };
    const char * QueryUseMessage<bool, &RepositoryUseInterface::query_use_mask>::message = "Repository.query_use_mask expects one or two arguments, but got %d";

    template<>
    struct QueryUseMessage<bool, &RepositoryUseInterface::query_use_force>
    {
        static const char * message;
    };
    const char * QueryUseMessage<bool, &RepositoryUseInterface::query_use_force>::message = "Repository.query_use_force expects one or two arguments, but got %d";

    /*
     * Document-method: query_repository_masks
     *
     * call-seq:
     *     query_repository_masks(qualified_package_name, version_spec) -> true or false or nil
     *
     * Query repository masks.  nil if the repository doesn't implement mask_interface.
     */
    /*
     * Document-method: query_profile_masks
     *
     * call-seq:
     *     query_profile_masks(qualified_package_name, version_spec) -> true or false or nil
     *
     * Query profile masks.  nil if the repository doesn't implement mask_interface.
     */

    template <bool (RepositoryMaskInterface::* m_) (const QualifiedPackageName &, const VersionSpec &) const>
    struct QueryMasks
    {
        static VALUE
        query(VALUE self, VALUE qpn, VALUE ver)
        {
            try
            {
                std::tr1::shared_ptr<const Repository> * self_ptr;
                Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
                const RepositoryMaskInterface * const mask_interface ((**self_ptr).mask_interface);

                if (mask_interface)
                {
                    QualifiedPackageName q = value_to_qualified_package_name(qpn);
                    VersionSpec v = value_to_version_spec(ver);

                    return ((*mask_interface).*m_)(q, v) ? Qtrue : Qfalse;
                }
                else
                {
                    return Qnil;
                }
            }
            catch (const std::exception & e)
            {
                exception_to_ruby_exception(e);
            }
        }
    };

    /*
     * call-seq:
     *     describe_use_flag(flag_name) -> String or Nil
     *     describe_use_flag(flag_name, package_database_entry) -> String or Nil
     *
     * Returns the description for a use flag name, or nil if the repository does not include 
     * the use_flag_interface.
     */
    VALUE
    repository_describe_use_flag(int argc, VALUE * argv, VALUE self)
    {
        try
        {
            std::tr1::shared_ptr<const Repository> * self_ptr;
            Data_Get_Struct(self, std::tr1::shared_ptr<const Repository>, self_ptr);
            if ((*self_ptr)->use_interface) {
                if (1 == argc || 2 ==argc)
                {
                    UseFlagName ufn = UseFlagName(StringValuePtr(argv[0]));
                    PackageDatabaseEntry * pde(0);
                    if (2 == argc)
                    {
                        PackageDatabaseEntry pde2 = value_to_package_database_entry(argv[1]);
                        pde = &pde2;
                    }

                    return rb_str_new2(((*self_ptr)->use_interface->describe_use_flag(ufn, pde).c_str()));

                }
                else
                {
                    rb_raise(rb_eArgError, "describe_use_flag expects one or two arguments, but got %d", argc);
                }
            }
            else
            {
                return Qnil;
            }

        }
            catch (const std::exception & e)
            {
                exception_to_ruby_exception(e);
            }
    }

    void do_register_repository()
    {
        /*
         * Document-class: Paludis::Repository
         *
         * A Repository provides a representation of a physical repository to a PackageDatabase.
         */
        c_repository = rb_define_class_under(paludis_module(), "Repository", rb_cObject);
        rb_funcall(c_repository, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_repository, "name", RUBY_FUNC_CAST(&repository_name), 0);
        rb_define_method(c_repository, "format", RUBY_FUNC_CAST(&repository_format), 0);

        rb_define_method(c_repository, "has_category_named?", RUBY_FUNC_CAST(&repository_has_category_named), 1);
        rb_define_method(c_repository, "has_package_named?", RUBY_FUNC_CAST(&repository_has_package_named), 1);
        rb_define_method(c_repository, "has_version?", RUBY_FUNC_CAST(&repository_has_version), 2);

        rb_define_method(c_repository, "category_names", RUBY_FUNC_CAST(&repository_category_names), 0);
        rb_define_method(c_repository, "category_names_containing_package",
                RUBY_FUNC_CAST(&repository_category_names_containing_package), 1);
        rb_define_method(c_repository, "package_names", RUBY_FUNC_CAST(&repository_package_names), 1);
        rb_define_method(c_repository, "version_specs", RUBY_FUNC_CAST(&repository_version_specs), 1);

        rb_define_method(c_repository, "version_metadata", RUBY_FUNC_CAST(&repository_version_metadata), 2);

        rb_define_method(c_repository, "installable_interface", RUBY_FUNC_CAST((&Interface<RepositoryInstallableInterface,
                        &Repository::installable_interface>::fetch)), 0);
        rb_define_method(c_repository, "installed_interface", RUBY_FUNC_CAST((&Interface<RepositoryInstalledInterface,
                        &Repository::installed_interface>::fetch)), 0);
        rb_define_method(c_repository, "mask_interface", RUBY_FUNC_CAST((&Interface<RepositoryMaskInterface,
                        &Repository::mask_interface>::fetch)), 0);
        rb_define_method(c_repository, "news_interface", RUBY_FUNC_CAST((&Interface<RepositoryNewsInterface,
                        &Repository::news_interface>::fetch)), 0);
        rb_define_method(c_repository, "sets_interface", RUBY_FUNC_CAST((&Interface<RepositorySetsInterface,
                        &Repository::sets_interface>::fetch)), 0);
        rb_define_method(c_repository, "syncable_interface", RUBY_FUNC_CAST((&Interface<RepositorySyncableInterface,
                        &Repository::syncable_interface>::fetch)), 0);
        rb_define_method(c_repository, "uninstallable_interface", RUBY_FUNC_CAST((&Interface<RepositoryUninstallableInterface,
                        &Repository::uninstallable_interface>::fetch)), 0);
        rb_define_method(c_repository, "use_interface", RUBY_FUNC_CAST((&Interface<RepositoryUseInterface,
                        &Repository::use_interface>::fetch)), 0);
        rb_define_method(c_repository, "world_interface", RUBY_FUNC_CAST((&Interface<RepositoryWorldInterface,
                        &Repository::world_interface>::fetch)), 0);
        rb_define_method(c_repository, "mirrors_interface", RUBY_FUNC_CAST((&Interface<RepositoryMirrorsInterface,
                        &Repository::mirrors_interface>::fetch)), 0);
        rb_define_method(c_repository, "environment_variable_interface", RUBY_FUNC_CAST((&Interface<RepositoryEnvironmentVariableInterface,
                        &Repository::environment_variable_interface>::fetch)), 0);
        rb_define_method(c_repository, "provides_interface", RUBY_FUNC_CAST((&Interface<RepositoryProvidesInterface,
                        &Repository::provides_interface>::fetch)), 0);
        rb_define_method(c_repository, "virtuals_interface", RUBY_FUNC_CAST((&Interface<RepositoryVirtualsInterface,
                        &Repository::virtuals_interface>::fetch)), 0);
        rb_define_method(c_repository, "contents_interface", RUBY_FUNC_CAST((&Interface<RepositoryContentsInterface,
                        &Repository::contents_interface>::fetch)), 0);

        rb_define_method(c_repository, "info", RUBY_FUNC_CAST(&repository_info), 1);
        rb_define_method(c_repository, "contents", RUBY_FUNC_CAST(&repository_contents), 2);
        rb_define_method(c_repository, "installed_time", RUBY_FUNC_CAST(&repository_installed_time), 2);

        rb_define_method(c_repository, "query_use", RUBY_FUNC_CAST((&QueryUse<UseFlagState, use_enabled, use_disabled, &RepositoryUseInterface::query_use>::query)), -1);
        rb_define_method(c_repository, "query_use_mask", RUBY_FUNC_CAST((&QueryUse<bool, true, false, &RepositoryUseInterface::query_use_mask>::query)), -1);
        rb_define_method(c_repository, "query_use_force", RUBY_FUNC_CAST((&QueryUse<bool, true, false, &RepositoryUseInterface::query_use_force>::query)), -1);

        rb_define_method(c_repository, "query_repository_masks", RUBY_FUNC_CAST(&QueryMasks<&RepositoryMaskInterface::query_repository_masks>::query), 2);
        rb_define_method(c_repository, "query_profile_masks", RUBY_FUNC_CAST(&QueryMasks<&RepositoryMaskInterface::query_profile_masks>::query), 2);

        rb_define_method(c_repository, "describe_use_flag", RUBY_FUNC_CAST(&repository_describe_use_flag),-1);

        /*
         * Document-class: Paludis::RepositoryInfo
         *
         * Information about a Repository, for the end user.
         */
        c_repository_info = rb_define_class_under(paludis_module(), "RepositoryInfo", rb_cObject);
        rb_funcall(c_repository_info, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_repository_info, "sections", RUBY_FUNC_CAST(&repository_info_sections), 0);

        /*
         * Document-class: Paludis::RepositoryInfoSection
         *
         * A section of information about a Repository.
         */
        c_repository_info_section = rb_define_class_under(paludis_module(), "RepositoryInfoSection", rb_cObject);
        rb_funcall(c_repository_info_section, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_repository_info_section, "kvs", RUBY_FUNC_CAST(&repository_info_section_kvs), 0);
        rb_define_method(c_repository_info_section, "header", RUBY_FUNC_CAST(&repository_info_section_header), 0);

        /*
         * Document-class: Paludis::PortageRepository
         *
         * A PortageRepository is a Repository that handles the layout used by Portage for the main Gentoo tree.
         */
        c_portage_repository = rb_define_class_under(paludis_module(), "PortageRepository", c_repository);
        rb_define_method(c_portage_repository, "profiles", RUBY_FUNC_CAST(&portage_repository_profiles), 0);
        rb_define_method(c_portage_repository, "find_profile", RUBY_FUNC_CAST(&portage_repository_find_profile), 1);
        rb_define_method(c_portage_repository, "profile_variable", RUBY_FUNC_CAST(&portage_repository_profile_variable), 1);

        /*
         * Document-class: Paludis::PortageRepositoryProfilesDescLine
         *
         *
         */
        c_portage_repository_profiles_desc_line = rb_define_class_under(paludis_module(), "PortageRepositoryProfilesDescLine", rb_cObject);
        rb_funcall(c_repository_info, rb_intern("private_class_method"), 1, rb_str_new2("new"));
        rb_define_method(c_portage_repository_profiles_desc_line, "path",
                RUBY_FUNC_CAST((&DescLineValue<FSEntry,&PortageRepositoryProfilesDescLine::path>::fetch)), 0);
        rb_define_method(c_portage_repository_profiles_desc_line, "arch",
                RUBY_FUNC_CAST((&DescLineValue<std::string,&PortageRepositoryProfilesDescLine::arch>::fetch)), 0);
        rb_define_method(c_portage_repository_profiles_desc_line, "status",
                RUBY_FUNC_CAST((&DescLineValue<std::string,&PortageRepositoryProfilesDescLine::status>::fetch)), 0);
    }
}

template <typename T_>
VALUE repo_to_value(T_ m, VALUE * klass)
{
    T_ * m_ptr(0);
    try
    {
        m_ptr = new T_(m);
        return Data_Wrap_Struct(*klass, 0, &Common<T_>::free, m_ptr);
    }
    catch (const std::exception & e)
    {
        delete m_ptr;
        exception_to_ruby_exception(e);
    }
}

VALUE
paludis::ruby::repository_to_value(std::tr1::shared_ptr<const Repository> m)
{
    if (0 != dynamic_cast<const PortageRepository *>(m.get()))
        return repo_to_value<std::tr1::shared_ptr<const PortageRepository> >(
                std::tr1::static_pointer_cast<const PortageRepository>(m), &c_portage_repository);
    else
        return repo_to_value<std::tr1::shared_ptr<const Repository> >(m, &c_repository);
}

VALUE
paludis::ruby::portage_repository_profiles_desc_line_to_value(const PortageRepositoryProfilesDescLine & v)
{
    PortageRepositoryProfilesDescLine * vv(new PortageRepositoryProfilesDescLine(v));
    return Data_Wrap_Struct(c_portage_repository_profiles_desc_line, 0, &Common<PortageRepositoryProfilesDescLine>::free, vv);
}

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

RegisterRubyClass::Register paludis_ruby_register_repository PALUDIS_ATTRIBUTE((used))
    (&do_register_repository);