blib/lib/Gentoo/VDB/Portage.pm untested

filter: Statementstmt 0.0% 0 / 162 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 15 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 273 SCAR 71.9
CC
36
Cov
0%
_package_iterator
210.0
properties
56.0
_category_iterator
42.0
CRAP 1332.0
1 use 5.006; # our
2 use strict;
3 use warnings;
4  
5 package Gentoo::VDB::Portage;
6  
7 our $VERSION = '0.001000';
8  
9 # ABSTRACT: VDB Query Implementation for Portage/Emerge
10  
11 # AUTHORITY
12  
13 use Path::Tiny qw( path );
14  
15 sub new {
16 my ( $class, @args ) = @_;
17 my $config = { ref $args[0] ? %{ $args[0] } : @args };
18 return bless $config, $class;
19 }
20  
21 sub _path {
22 return ( $_[0]->{path} ||= '/var/db/pkg' );
23 }
24  
25 sub _abspath {
26 my $root = path( $_[0]->_path )->absolute->realpath;
27 my $path = path( $_[0]->_path, @_[ 1 .. $#_ ] )->absolute->realpath;
28 die "Illegal path, outside of VDB" unless $root->subsumes($path);
29 return $path->stringify;
30 }
31  
32 sub __dir_iterator {
33 my ($path) = @_;
34 my $handle;
35 ( -d $path and opendir $handle, $path ) or return sub { return undef };
36 return sub {
37 while (1) {
38 my $dir = readdir $handle;
39 return undef unless defined $dir;
40 next if $dir eq '.' or $dir eq '..'; # skip hidden entries
41 return $dir;
42 }
43 };
44 }
45  
46 sub _category_iterator {
47 my ($self) = @_;
48 my $root = $self->_path;
49 return sub { return undef }
50 unless -d $root;
51 my $_cat_iterator = __dir_iterator($root);
52 return sub {
53 while (1) {
54  
55 # Category possible
56 my $category = $_cat_iterator->();
57 return undef if not defined $category;
58  
59 # Skip hidden categories
60 next if $category =~ /\A[.]/x;
61  
62 # Validate category to have at least one package with a file
63 my $_pkg_iterator = __dir_iterator( $self->_abspath($category) );
64 while ( my $package = $_pkg_iterator->() ) {
65 next if $package =~ /\A[.]/x;
66 my $_file_iterator =
67 __dir_iterator( $self->_abspath( $category, $package ) );
68 while ( my $file = $_file_iterator->() ) {
69 next if $file =~ /\A[.]/x;
70 ## Found one package with one file, category is valid
71 return $category;
72 }
73 }
74 }
75 };
76 }
77  
78 sub categories {
79 my ($self) = @_;
80 my $it = $self->_category_iterator;
81 my @cats;
82 while ( my $entry = $it->() ) {
83 push @cats, $entry;
84 }
85 return @cats;
86 }
87  
88 sub _package_iterator {
89 my ( $self, $config ) = @_;
90 my $root = $self->_path;
91 if ( $config->{in} ) {
92 my $catdir = $self->_abspath( $config->{in} );
93 return sub { return undef }
94 unless -d $catdir;
95 my $_pkg_iterator = __dir_iterator($catdir);
96 return sub {
97 while (1) {
98 my $package = $_pkg_iterator->();
99 return undef if not defined $package;
100 next if $package =~ /\A[.]/x;
101 my $_file_iterator =
102 __dir_iterator( $self->_abspath( $config->{in}, $package ) );
103 while ( my $file = $_file_iterator->() ) {
104 next if $file =~ /\A[.]/x;
105 ## Found one package with one file, package is valid
106 return $config->{in} . '/' . $package;
107 }
108 }
109 };
110 }
111  
112 return sub { return undef }
113 unless -d $root;
114  
115 my $_cat_iterator = __dir_iterator($root);
116 my $category = $_cat_iterator->();
117  
118 return sub { return undef }
119 unless defined $category;
120  
121 my $_pkg_iterator = __dir_iterator( $self->_abspath($category) );
122  
123 return sub {
124 while (1) {
125 return undef if not defined $category;
126 my $package = $_pkg_iterator->();
127 if ( not defined $package ) {
128 $category = $_cat_iterator->();
129 return undef if not defined $category;
130 if ( defined $category ) {
131 $_pkg_iterator =
132 __dir_iterator( $self->_abspath($category) );
133 next;
134 }
135 next;
136 }
137 next if $package =~ /\A[.]/x;
138 my $_file_iterator =
139 __dir_iterator( $self->_abspath( $category, $package ) );
140 while ( my $file = $_file_iterator->() ) {
141 next if $file =~ /\A[.]/x;
142 ## Found one package with one file, package is valid
143 return $category . '/' . $package;
144 }
145 }
146 };
147 }
148  
149 sub packages {
150 my ( $self, @args ) = @_;
151 my $config = { ref $args[0] ? %{ $args[0] } : @args };
152 my $iterator = $self->_package_iterator($config);
153 my (@packages);
154 while ( my $result = $iterator->() ) {
155 push @packages, $result;
156 }
157 return @packages;
158 }
159  
160 sub _property_files_iterator {
161 my ( $self, $config ) = @_;
162 return sub { undef }
163 unless $config->{'for'};
164 my $catdir = $self->_abspath( $config->{'for'} );
165 return sub { undef }
166 unless -d $catdir;
167 my $iterator = __dir_iterator($catdir);
168 return sub {
169  
170 while (1) {
171 my $file = $iterator->();
172 return undef if not defined $file;
173 next if $file =~ /\A[.]/x;
174 return $file;
175 }
176 };
177 }
178  
179 my $ENATIVE = {
180 BUILD_TIME => 'timestamp',
181 CATEGORY => 'string',
182 CBUILD => 'string',
183 CC => 'string',
184 CFLAGS => 'string',
185 CHOST => 'string',
186 CONTENTS => 'contents',
187 COUNTER => 'number',
188 CTARGET => 'string',
189 CXX => 'string',
190 CXXFLAGS => 'string',
191 DEBUGBUILD => 'flag-file',
192 DEFINED_PHASES => 'space-separated-list',
193 DEPEND => 'dependencies',
194 DESCRIPTION => 'string',
195 EAPI => 'string',
196 FEATURES => 'use-list',
197 'environment.bz2' => {
198 type => 'file',
199 encoding => 'application/x-bzip2',
200 content => 'text/plain'
201 },
202 HOMEPAGE => 'url-list',
203 INHERITED => 'space-separated-list',
204 IUSE => 'use-list',
205 IUSE_EFFECTIVE => 'use-list',
206 KEYWORDS => 'keywords',
207 LDFLAGS => 'string',
208 LICENSE => 'licenses',
209 NEEDED => 'elf-dependency-map',
210 'NEEDED.ELF.2' => 'arch-elf-dependency-map',
211 PDEPEND => 'dependencies',
212 PF => 'string',
213 PKGUSE => 'use-list',
214 PROVIDES => 'arch-so-map',
215 QA_CONFIGURE_OPTIONS => 'string',
216 QA_PREBUILT => 'space-separated-list',
217 RDEPEND => 'dependencies',
218 repository => 'string',
219 REQUIRES => 'arch-so-map',
220 REQUIRES_EXCLUDE => 'space-separated-list',
221 RESTRICT => 'space-seperated-list',
222 SIZE => 'bytecount',
223 SLOT => 'string',
224 USE => 'use-list',
225 };
226  
227 my @ERULES = (
228 [
229 sub { $_[0] =~ /\.ebuild\z/ },
230 {
231 label => 'special:source_ebuild',
232 type => 'file',
233 content => 'text/plain'
234 }
235 ],
236 );
237  
238 sub properties {
239 my ( $self, @args ) = @_;
240 my $config = { ref $args[0] ? %{ $args[0] } : @args };
241 my (@proplist);
242 my $it = $self->_property_files_iterator($config);
243 while ( my $entry = $it->() ) {
244 my $matched = 0;
245 if ( exists $ENATIVE->{$entry} ) {
246 $matched = 1;
247 push @proplist,
248 {
249 property => $entry,
250 label => $entry,
251 for => $config->{for},
252 (
253 ref $ENATIVE->{$entry}
254 ? %{ $ENATIVE->{$entry} }
255 : ( type => $ENATIVE->{$entry} )
256 ),
257 };
258 }
259 for my $rule (@ERULES) {
260 next unless $rule->[0]->($entry);
261 $matched = 1;
262 push @proplist,
263 {
264 property => $entry,
265 label => $entry,
266 for => $config->{for},
267 (
268 ref $rule->[1]
269 ? %{ $rule->[1] }
270 : ( type => $rule->[1] )
271 ),
272 };
273 }
274 if ( not $matched ) {
275 push @proplist,
276 {
277 property => $entry,
278 label => 'unknown:' . $entry,
279 for => $config->{for},
280 type => 'file',
281 content => 'application/octet-stream',
282 };
283 }
284 }
285 return @proplist;
286 }
287  
288 sub get_property {
289 my ( $self, @args ) = @_;
290 my $config = { ref $args[0] ? %{ $args[0] } : @args };
291 return undef
292 unless exists $config->{for} and exists $config->{property};
293 my $content;
294 open my $fh, '<', $self->_abspath( $config->{for}, $config->{property} )
295 or return undef;
296 {
297 local $/ = undef;
298 $content = <$fh>;
299 }
300 close $fh;
301 chomp $content;
302 return $content;
303 }
304  
305 1;
306  
307 =head1 NAME
308  
309 Gentoo::VDB::Portage - VDB Query Implementation for Portage/Emerge
310  
311 =head1 AUTHOR
312  
313 Kent Fredric <kentnl@cpan.org>
314  
315 =head1 LICENSE
316  
317 This software is copyright (c) 2016 by Kent Fredric.
318  
319 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
320  
321 =cut
322  
323 ## Please see file perltidy.ERR