blib/lib/Gentoo/Overlay.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 209 CC 9 SCAR 14.7
CC
9
Cov
0%
_build___categories_scan
12.0
_build_name
6.0
_build__profile_dir
6.0
CRAP 4.4
1 use 5.006;
2 use strict;
3 use warnings;
4  
5 package Gentoo::Overlay;
6  
7 our $VERSION = '2.001001';
8  
9 # ABSTRACT: Tools for working with Gentoo Overlays
10  
11 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 use Moo 1.006000 qw( has );
14 use MooX::HandlesVia;
15 use MooseX::Has::Sugar qw( ro coerce lazy_build lazy );
16 use Types::Standard qw( HashRef CodeRef );
17 use Types::Path::Tiny qw( File Dir );
18 use MooX::ClassAttribute qw( class_has );
19 use Carp qw();
20 use Gentoo::Overlay::Category;
21 use Gentoo::Overlay::Types qw( Gentoo__Overlay_RepositoryName Gentoo__Overlay_Category );
22 use Gentoo::Overlay::Exceptions qw( exception warning );
23 use namespace::clean -except => 'meta';
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35 has 'path' => (
36 ro, coerce,
37 isa => Dir,
38 default => sub {
39 exception(
40 ident => 'path parameter required',
41 message => '%{package}s requires the \'path\' attribute passed during construction',
42 payload => { package => __PACKAGE__ },
43 );
44 },
45 );
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59 has 'name' => ( isa => Gentoo__Overlay_RepositoryName, ro, lazy, builder => 1, );
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72 sub _build_name {
73 my ($self) = shift;
74 my $f = $self->default_path( repo_name => );
75 if ( not $f->exists or $f->is_dir ) {
76 exception(
77 ident => 'no repo_name',
78 message => <<'EOF',
79 No repo_name file for overlay at: %{overlay_path}s
80 Expects:%{expected_path}s
81 EOF
82 payload => {
83 overlay_path => $self->path->stringify,
84 expected_path => $f->stringify,
85 },
86 );
87 }
88 return [ $f->lines_raw( { chomp => 1 } ) ]->[0];
89  
90 }
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104 has _profile_dir => ( isa => Dir, ro, lazy, builder => 1 );
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116 sub _build__profile_dir {
117 my ($self) = shift;
118 my $pd = $self->default_path( profiles => );
119 if ( not $pd->exists or not $pd->is_dir ) {
120 exception(
121 ident => 'no profile directory',
122 message => <<'EOF',
123 No profile directory for overlay at: %{overlay_path}s
124 Expects:%{expected_path}s
125 EOF
126 payload => {
127 overlay_path => $self->path->stringify,
128 expected_path => $pd->stringify,
129 },
130 );
131 }
132 return $pd->absolute;
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 has _categories => (
199 lazy,
200 builder => 1,
201 ro,
202 isa => HashRef [Gentoo__Overlay_Category],
203 handles_via => 'Hash',
204 handles => {
205 _has_category => exists =>,
206 category_names => keys =>,
207 categories => elements =>,
208 get_category => get =>,
209 },
210 );
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227 sub _build__categories {
228 my ($self) = @_;
229 my $cf = $self->default_path('catfile');
230 if ( not $cf->exists or $cf->is_dir ) {
231 warning(
232 ident => 'no category file',
233 message => <<'EOF',
234 No category file for overlay %{name}s, expected: %{category_file}s.
235 Falling back to scanning
236 EOF
237 payload => {
238 name => $self->name,
239 category_file => $cf->stringify,
240 },
241 );
242 goto $self->can('_build___categories_scan');
243 }
244 goto $self->can('_build___categories_file');
245 }
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256 class_has _default_paths => (
257 ro, lazy,
258 isa => HashRef [CodeRef],
259 default => sub {
260 return {
261 'profiles' => sub { shift->path->child('profiles') },
262 'repo_name' => sub { shift->_profile_dir->child('repo_name') },
263 'catfile' => sub { shift->_profile_dir->child('categories') },
264 'category' => sub { shift->path->child(shift) },
265 'package' => sub { shift->default_path( 'category', shift )->child(shift) },
266 'ebuild' => sub { shift->default_path( 'package', shift, shift )->child(shift) },
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 sub default_path {
299 my ( $self, $name, @args ) = @_;
300 if ( !exists $self->_default_paths->{$name} ) {
301 exception(
302 ident => 'no default path',
303 message => q[No default path '%{name}s'],
304 payload => { path => $name },
305 );
306 }
307 return $self->_default_paths->{$name}->( $self, @args );
308 }
309  
310  
311  
312  
313  
314  
315  
316  
317  
318 sub _build___categories_file {
319 my ($self) = shift;
320 my %out;
321 for my $cat ( $self->default_path('catfile')->lines_raw( { chomp => 1 } ) ) {
322 my $category = Gentoo::Overlay::Category->new(
323 name => $cat,
324 overlay => $self,
325 );
326 if ( !$category->exists ) {
327 exception(
328 ident => 'missing category',
329 message => <<'EOF',
330 category %{category_name}s is not an existing directory (%{expected_path}s) for overlay %{overlay_name}s
331 EOF
332 payload => {
333 category_name => $category->name,
334 expected_path => $category->path->stringify,
335 overlay_name => $self->name,
336 },
337 );
338 next;
339 }
340 $out{$cat} = $category;
341 }
342 return \%out;
343 }
344  
345  
346  
347  
348  
349  
350  
351  
352  
353  
354 sub _build___categories_scan {
355 my ($self) = shift;
356 my %out;
357 my $it = $self->path->absolute->iterator();
358 while ( my $entry = $it->() ) {
359 my $cat = $entry->basename;
360 next if Gentoo::Overlay::Category->is_blacklisted($cat);
361  
362 my $category = Gentoo::Overlay::Category->new(
363 overlay => $self,
364 name => $cat,
365 );
366 next unless $category->exists();
367 $out{$cat} = $category;
368 }
369 return \%out;
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 sub iterate {
435 my ( $self, $what, $callback ) = @_; ## no critic (Variables::ProhibitUnusedVarsStricter)
436  
437 my %method_map = (
438 categories => _iterate_categories =>,
439 packages => _iterate_packages =>,
440 ebuilds => _iterate_ebuilds =>,
441 );
442 if ( exists $method_map{$what} ) {
443 goto $self->can( $method_map{$what} );
444 }
445 return exception(
446 ident => 'bad iteration method',
447 message => 'The iteration method %{what_method}s is not a known way to iterate.',
448 payload => { what_method => $what, },
449 );
450 }
451  
452  
453  
454  
455  
456  
457  
458  
459  
460  
461  
462 # ebuilds = { /categories/packages/ebuilds }
463 sub _iterate_ebuilds {
464 my ( $self, undef, $callback ) = @_;
465  
466 my $real_callback = sub {
467 my (%cconfig) = %{ $_[1] };
468 my $inner_callback = sub {
469 my %pconfig = %{ $_[1] };
470 $self->$callback( { ( %cconfig, %pconfig ) } );
471 };
472 $cconfig{package}->_iterate_ebuilds( 'ebuilds' => $inner_callback );
473 };
474  
475 $self->_iterate_packages( 'packages' => $real_callback );
476 return;
477  
478 }
479  
480  
481  
482  
483  
484  
485  
486  
487  
488  
489  
490 # categories = { /categories }
491 sub _iterate_categories {
492 my ( $self, undef, $callback ) = @_;
493 my %categories = $self->categories();
494 my $num_categories = scalar keys %categories;
495 my $last_category = $num_categories - 1;
496 my $offset = 0;
497 for my $cname ( sort keys %categories ) {
498 local $_ = $categories{$cname};
499 $self->$callback(
500 {
501 category_name => $cname,
502 category => $categories{$cname},
503 num_categories => $num_categories,
504 last_category => $last_category,
505 category_num => $offset,
506 }
507 );
508 $offset++;
509 }
510 return;
511 }
512  
513  
514  
515  
516  
517  
518  
519  
520  
521  
522  
523 # packages = { /categories/packages }
524 sub _iterate_packages {
525 my ( $self, undef, $callback ) = @_;
526  
527 my $real_callback = sub {
528 my (%cconfig) = %{ $_[1] };
529 my $inner_callback = sub {
530 my %pconfig = %{ $_[1] };
531 $self->$callback( { ( %cconfig, %pconfig ) } );
532 };
533 $cconfig{category}->_iterate_packages( 'packages' => $inner_callback );
534 };
535 $self->_iterate_categories( 'categories' => $real_callback );
536 return;
537 }
538 no Moo;
539 1;
540  
541 __END__
542  
543 =pod
544  
545 =encoding UTF-8
546  
547 =head1 NAME
548  
549 Gentoo::Overlay - Tools for working with Gentoo Overlays
550  
551 =head1 VERSION
552  
553 version 2.001001
554  
555 =head1 SYNOPSIS
556  
557 my $overlay = Gentoo::Overlay->new( path => '/usr/portage' );
558  
559 my $name = $overlay->name();
560 my %categories = $overlay->categories();
561  
562 print "Overlay $name 's categories:\n";
563 for( sort keys %categories ){
564 printf "%30s : %s", $_, $categories{$_};
565 }
566  
567 # Overlay gentoo 's categories:
568 # .....
569 # dev-lang : /usr/portage/dev-lang
570 # .....
571  
572 There will be more features eventually, this is just a first release.
573  
574 =head1 METHODS
575  
576 =head2 default_path
577  
578 Useful function to easily wrap the class-wide method with a per-object sugar.
579  
580 $overlay->default_path('profiles');
581 ->
582 ::Overlay->_default_paths->{'profiles'}->($overlay);
583 ->
584 $overlay->path->subdir('profiles')
585  
586  
587 $overlay->default_path('category','foo');
588 ->
589 ::Overlay->_default_path('category')->( $overlay, 'foo' );
590 ->
591 $overlay->path->subdir('foo')
592  
593 $overlay->default_path('repo_name');
594 ->
595 ::Overlay->_default_path('repo_name')->( $overlay );
596 ->
597 $overlay->_profile_dir->file('repo_name')
598  
599 They're class wide functions, but they need individual instances to work.
600  
601 =head2 iterate
602  
603 $overlay->iterate( $what, sub {
604 my ( $context_information ) = shift;
605  
606 } );
607  
608 The iterate method provides a handy way to do walking across the whole tree stopping at each of a given type.
609  
610 =over 4
611  
612 =item * C<$what = 'categories'>
613  
614 $overlay->iterate( categories => sub {
615 my ( $self, $c ) = shift;
616 # $c->{category_name} # String
617 # $c->{category} # Category Object
618 # $c->{num_categories} # How many categories are there to iterate
619 # $c->{last_category} # Index ID of the last category.
620 # $c->{category_num} # Index ID of the current category.
621 } );
622  
623 =item * C<$what = 'packages'>
624  
625 $overlay->iterate( packages => sub {
626 my ( $self, $c ) = shift;
627 # $c->{category_name} # String
628 # $c->{category} # Category Object
629 # $c->{num_categories} # How many categories are there to iterate
630 # $c->{last_category} # Index ID of the last category.
631 # $c->{category_num} # Index ID of the current category.
632 #
633 # $c->{package_name} # String
634 # See ::Category for the rest of the fields provided by the package Iterator.
635 # Very similar though.
636 } );
637  
638 =item * C<$what = 'ebuilds'>
639  
640 $overlay->iterate( ebuilds => sub {
641 my ( $self, $c ) = shift;
642 # $c->{category_name} # String
643 # $c->{category} # Category Object
644 # $c->{num_categories} # How many categories are there to iterate
645 # $c->{last_category} # Index ID of the last category.
646 # $c->{category_num} # Index ID of the current category.
647 #
648 # $c->{package_name} # String
649 # See ::Category for the rest of the fields provided by the package Iterator.
650 # Very similar though.
651 #
652 # $c->{ebuild_name} # String
653 # See ::Package for the rest of the fields provided by the ebuild Iterator.
654 # Very similar though.
655 } );
656  
657 =back
658  
659 =head1 ATTRIBUTES
660  
661 =head2 path
662  
663 Path to repository.
664  
665 isa => File, ro, required, coerce
666  
667 L<Types::Path::Tiny/File>
668  
669 =head2 name
670  
671 Repository name.
672  
673 isa => Gentoo__Overlay_RepositoryName, ro, lazy_build
674  
675 L<< C<RepositoryName>|Gentoo::Overlay::Types/Gentoo__Overlay_RepositoryName >>
676  
677 L</_build_name>
678  
679 =head1 ATTRIBUTE ACCESSORS
680  
681 =head2 category_names
682  
683 Returns a list of the names of all the categories.
684  
685 my @list = sort $overlay->category_names();
686  
687 L</_categories>
688  
689 =head2 categories
690  
691 Returns a hash of L<< C<Category>|Gentoo::Overlay::Category >> objects.
692  
693 my %hash = $overlay->categories;
694 print $hash{dev-perl}->pretty_name; # dev-perl/::gentoo
695  
696 L</_categories>
697  
698 =head2 get_category
699  
700 Returns a Category Object for a given category name
701  
702 my $cat = $overlay->get_category('dev-perl');
703  
704 L</_categories>
705  
706 =head1 PRIVATE ATTRIBUTES
707  
708 =head2 _profile_dir
709  
710 Path to the profile sub-directory.
711  
712 isa => Dir, ro, lazy_build
713  
714 L<MooseX::Types::Path::Tiny/Dir>
715  
716 L</_build__profile_dir>
717  
718 =head2 _categories
719  
720 The auto-generating category hash backing
721  
722 isa => HashRef[ Gentoo__Overlay_Category ], ro, lazy_build
723  
724 L</_build__categories>
725  
726 L</_has_category>
727  
728 L</category_names>
729  
730 L</categories>
731  
732 L</get_category>
733  
734 L<Gentoo::Overlay::Types/Gentoo__Overlay_Category>
735  
736 L<< C<MooseX::Types::Moose>|MooseX::Types::Moose >>
737  
738 =head1 PRIVATE ATTRIBUTE ACCESSORS
739  
740 =head2 _has_category
741  
742 Returns if a named category exists
743  
744 $overlay->_has_category("dev-perl");
745  
746 L</_categories>
747  
748 =head1 PRIVATE CLASS ATTRIBUTES
749  
750 =head2 _default_paths
751  
752 Class-wide list of path generators.
753  
754 isa => HashRef[ CodeRef ], ro, lazy_build
755  
756 L</_build__default_paths>
757  
758 =head1 PRIVATE METHODS
759  
760 =head2 _build_name
761  
762 Extracts the repository name out of the file 'C<repo_name>'
763 in C<$OVERLAY/profiles/repo_name>
764  
765 $overlay->_build_name
766  
767 L</name>
768  
769 =head2 _build__profile_dir
770  
771 Verifies the existence of the profile directory, and returns the path to it.
772  
773 $overlay->_build__profile_dir
774  
775 L</_profile_dir>
776  
777 =head2 _build__categories
778  
779 Generates the Category Hash-Table, either by reading the categories index ( new, preferred )
780 or by traversing the directory ( old, discouraged )
781  
782 $category->_build_categories;
783  
784 L</_categories>
785  
786 L</_build___categories_scan>
787  
788 L</_build___categories_file>
789  
790 =head2 _build___categories_file
791  
792 Builds the category map using the 'categories' file found in the overlays profile directory.
793  
794 $overlay->_build___categories_file
795  
796 =head2 _build___categories_scan
797  
798 Builds the category map the hard way by scanning the directory and then skipping things
799 that are files and/or blacklisted.
800  
801 $overlay->_build___categories_scan
802  
803 =head2 _iterate_ebuilds
804  
805 $object->_iterate_ebuilds( ignored_value => sub { } );
806  
807 Handles dispatch call for
808  
809 $object->iterate( ebuilds => sub { } );
810  
811 =head2 _iterate_categories
812  
813 $object->_iterate_categories( ignored_value => sub { } );
814  
815 Handles dispatch call for
816  
817 $object->iterate( categories => sub { } );
818  
819 =head2 _iterate_packages
820  
821 $object->_iterate_packages( ignored_value => sub { } );
822  
823 Handles dispatch call for
824  
825 $object->iterate( packages => sub { } );
826  
827 =head1 AUTHOR
828  
829 Kent Fredric <kentnl@cpan.org>
830  
831 =head1 COPYRIGHT AND LICENSE
832  
833 This software is copyright (c) 2014 by Kent Fredric <kentnl@cpan.org>.
834  
835 This is free software; you can redistribute it and/or modify it under
836 the same terms as the Perl 5 programming language system itself.
837  
838 =cut