lib/CPAN/Mini/Visit/Filtered.pm

filter: Statementstmt 85.5% 77 / 90 Branchbran 43.7% 7 / 16 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 93.1% 27 / 29 Podpod n/a totaltotal 78.6% 118 / 150 CC 13 SCAR 15.9
CC
13
Cov
77%
__ANON__
25.8
__ANON__
4.8
distinfo
4.8
CRAP 4.9
↑ Summary
1 package CPAN::Mini::Visit::Filtered;
2  
3 3 our $VERSION = '0.01_06';
4  
5 3 ▶use MooseX::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 CPAN::Mini::Visit::Filtered - visit unpacked distributions in a filtered CPAN::Mini mirror
11  
12 =head1 SYNOPSIS
13  
14 my $visitor = CPAN::Mini::Visit::Filtered->new(
15 action => sub {
16 # XXX put something here
17 },
18 filter => sub { /briang/i },
19 );
20  
21 $visitor->visit_distributions;
22  
23 =head1 DESCRIPTION XXX
24  
25 =head1 RATIONALE XXX
26  
27 =cut
28  
29 3 ▶class CPAN::Mini::Visit::Filtered {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.2
30 3 ▶ use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 3 ▶ use Archive::Extract qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶ use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶ use CPAN::DistnameInfo qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶ use Cwd qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶ use File::Find::Rule qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶ use File::Spec qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶ use File::Temp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 # CPAN::Mini
40  
41 =head1 CONSTRUCTOR
42  
43 new() returns a new CPAN::Mini::Visit::Filtered object. Parameters to new() should be
44 supplied as key=>value pairs. The following attributes are recognised.
45  
46 =head1 ATTRIBUTES
47  
48 Attributes of the CPAN::Mini::Visit::Filtered class are all read-only: they can be set only
49 when constructing an object. They all have getters, however, that can
50 be used at any time, though its doubtful that you'll need to.
51  
52 CPAN::Mini::Visit::Filtered objects have the following attributes:
53  
54 =head2 action
55  
56 Once the archive has been unpacked, the coderef stored in action will
57 be called. The subroutine will be passed a CPAN::DistnameInfo object.
58  
59 This parameter is mandatory.
60  
61 =cut
62  
63 3 has qw(action is ro isa CodeRef required 1);
64  
65 =head2 archive_types
66  
67 This is a regular expression that matches valid archives. The default
68 value matches C<< *.tar.gz >>, C<< *.tgz >>, C<< *.tar.bz2 >> and
69 C<< *.zip >>.
70  
71 =cut
72  
73 has qw(archive_types is ro),
74 3 ▶ default => sub { qr{\.(?:tar\.bz2|tar\.gz|tgz|zip)$} };
Subroutine __ANON__: called CC=1 SCAR=0.0
75  
76 =head2 cpan_base
77  
78 This is the base directory where the CPAN::Mini mirror is stored.
79  
80 This paramater is mandatory
81  
82 =cut
83  
84 has qw(cpan_base is ro isa Str required 1),
85 # default => sub {
86 # require CPAN::Mini;
87  
88 # my $config_file = CPAN::Mini->config_file({});
89 # Carp::croak("CPAN::Mini config file not located: $!")
90 # unless defined $config_file and -e $config_file;
91 # my %config = CPAN::Mini->read_config({quiet=>1});
92 # Carp::croak("You haven't defined 'cpan_base' and no 'local' option was found in $config_file")
93 # unless defined $config{local};
94 # return $config{local}
95 # },
96 7 ▶ trigger => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
97 7 my $self = shift;
98 7 my $base = $self->cpan_base;
99  
100 7 ▶ Carp::croak("Attribute (cpan_base) does not exist: '$base'")
Branch: 1 branch
BranchTrueFalseTotal
unless -e $base 1 6 7
101 unless -e $base;
102 6 ▶ Carp::croak("Attribute (cpan_base) is not a directory: '$base'")
Branch: 1 branch
BranchTrueFalseTotal
unless -d _ 1 5 6
103 unless -d _;
104 3 };
105  
106 =head2 filter
107  
108 This coderef is called before any archive is unpacked. The intention
109 is that this callback is used to filter out distributions you have no
110 interest in.
111  
112 The subroutine will be passed a CPAN::DistnameInfo object and $_ will
113 be set to the full path and filename of the file as stored in the
114 CPAN::Mini mirror. The function should return a true value if you wish
115 this archive to be processed further.
116  
117 By default all archives will be included. (With the possible exception
118 of Acme::*. See L<include_acme>.)
119  
120 =cut
121  
122 has qw(filter is ro isa CodeRef),
123 3 ▶ default => sub { sub {1} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
124  
125 =head2 include_acme
126  
127 Set this parameter to a true value if you wish to process the modules
128 from the Acme::* namespace. Traditionally, these modules are all
129 "jokes", and you may not wish to process them
130  
131 By default, the Acme distributions will not be included.
132  
133 =cut
134  
135 3 has qw(include_acme is ro isa Bool default 0);
136  
137 =head2 unpack_dir
138  
139 The directory where the distributions will be unpacked.
140  
141 By default, a temporary directory (as determined by
142 File::Temp::tempdir) will be allocated for you, and will be deleted
143 when no longer required.
144  
145 =cut
146  
147 3 has qw(unpack_dir is ro isa Str),
148 default => File::Temp::tempdir(
149 File::Spec->catfile(File::Spec->tmpdir, "cmvf-$$-XXXXXXXX"),
150 CLEANUP => 1
151 );
152  
153 # private
154  
155 # cache for CPAN::DI object
156 3 has qw(_distinfo is rw isa CPAN::DistnameInfo writer _set_distinfo);
157 3 has qw(_initial_dir is ro isa Str), default => Cwd::getcwd;
158  
159 =head1 METHODS
160  
161 =head2 distinfo
162  
163 =cut
164  
165 0 ▶ method distinfo(Str $archive) {
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=3 SCAR=15.8
Subroutine distinfo: called CC=3 SCAR=15.8
166 0 ▶ return $self->_distinfo
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_distinfo and $self->_distinfo->pathname eq $archive 0 0 0
if defined $self->_distinfo and $self->_distinfo->pathname eq $archive 0 15 15
Condition33%2/6
defined $self->_distinfo and $self->_distinfo->pathname eq $archive
!ll&&!rl&&r
000
defined $self->_distinfo and $self->_distinfo->pathname eq $archive
!ll&&!rl&&r
3120
MC/DC0%0/2
defined $self->_distinfo and $self->_distinfo->pathname eq $archive
defined $self->_distinfo $self->_distinfo->pathname eq $archive
MC/DC0%0/2
defined $self->_distinfo and $self->_distinfo->pathname eq $archive
defined $self->_distinfo $self->_distinfo->pathname eq $archive
Truth table0%0/3
A and B
A= defined $self->_distinfoB= $self->_distinfo->pathname eq $archive
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $self->_distinfoB= $self->_distinfo->pathname eq $archive
ABresult
0X0
100
111
167 if defined $self->_distinfo
168 && $self->_distinfo->pathname eq $archive;
169  
170 0 return $self->_set_distinfo(CPAN::DistnameInfo->new($archive));
171 3 }
172  
173 =head2 find_archives
174  
175 =cut
176  
177 3 ▶ method find_archives() {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.0
178 my $include_acme = $self->include_acme
179 10 ▶ ? sub { 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
180 3 ▶ : sub { $_[0]->dist !~ /^acme-/i };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->include_acme ? : 2 1 3
181 3 my $filter = $self->filter;
182  
183 return grep {
184 3 my $info = $self->distinfo($_);
185 15 ▶ $include_acme->($info) && $filter->($info)
Condition100%3/3
&$include_acme($info) && &$filter($info)
!ll&&!rl&&r
1311
MC/DC100%2/2
&$include_acme($info) && &$filter($info)
&$include_acme($info) &$filter($info)
Truth table100%3/3
A && B
A= &$include_acme($info)B= &$filter($info)
ABresult
0X0
100
111
186 } File::Find::Rule->file
187 ->name($self->archive_types)
188 ->in( File::Spec->catdir($self->cpan_base, qw{authors id}) );
189 3 }
190  
191 =head2 visit_distributions
192  
193 =cut
194  
195 0 ▶ method visit_distributions() {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=5 SCAR=32.5
196 0 my $dest = $self->unpack_dir;
197  
198 0 for my $archive ($self->find_archives) {
199 0 my $ae = Archive::Extract->new(archive => $archive);
200 0 my $ok = $ae->extract( to => $dest ); # XXX and if it fails???
201  
202 0 my $info = $self->distinfo($archive);
203  
204 0 ▶ chdir $self->unpack_dir or die $!; # XXX
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->unpack_dir or die $! 0 0 0
205 0 ▶ chdir $info->distvname or die $!;
Branch: 1 branch
BranchTrueFalseTotal
chdir $info->distvname or die $! 0 0 0
206 0 ▶ chdir $self->_initial_dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->_initial_dir or die $! 0 0 0
207  
208 0 $self->action->($info);
209 }
210 3 }
211 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 3 1;
214  
215 __END__
↑ Summary