blib/lib/Dist/Metadata/Dist.pm

filter: Statementstmt 93.1% 136 / 146 Branchbran 82.1% 46 / 56 Conditioncond 75.0% 15 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 82.8% 29 / 35 Podpod 100% 23 / 23 totaltotal 87.2% 254 / 291 CC 40 SCAR 14.1
CC
40
Cov
89%
parse_name_and_version
10.3
new
7.1
remove_root_dir
5.1
CRAP 4.1
« blib/lib/Dist/Metadata.pm ↑ Summary blib/lib/Dist/Metadata/Archive.pm »
1 #
2 # This file is part of Dist-Metadata
3 #
4 # This software is copyright (c) 2011 by Randy Stauner.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Dist::Metadata::Dist;
13 17 our $AUTHORITY = 'cpan:RWSTAUNER';
14 # ABSTRACT: Base class for format-specific implementations
15 17 $Dist::Metadata::Dist::VERSION = '0.927';
16 17 ▶use Carp qw(croak carp); # core
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use CPAN::DistnameInfo 0.12 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 ▶use Path::Class 0.24 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 17 ▶use Try::Tiny 0.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 90 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
23 90 my $class = shift;
24 my $self = {
25 0 ▶ @_ == 1 ? %{ $_[0] } : @_
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 90 90
26 };
27  
28 90 bless $self, $class;
29  
30 90 my $req = $class->required_attribute;
31 croak qq['$req' parameter required]
32 90 ▶ if $req && !$self->{$req};
Branch: 1 branch
BranchTrueFalseTotal
if $req and not $self->{$req} 5 85 90
Condition66%2/3
$req and not $self->{$req}
!ll&&!rl&&r
0855
MC/DC50%1/2
$req and not $self->{$req}
$req not $self->{$req}
Truth table66%2/3
A and not(B)
A= $reqB= not $self->{$req}
ABresult
0X0
100
111
33  
34 85 ▶ if ( exists $self->{file_spec} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"file_spec"}) 12 73 85
35 # we just want the OS name ('Unix' or '')
36 $self->{file_spec} =~ s/^File::Spec(::)?//
37 12 ▶ if $self->{file_spec};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file_spec"} 10 2 12
38 # blank is no good, use "Native" hack
39 $self->{file_spec} = 'Native'
40 12 ▶ if !$self->{file_spec};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file_spec"} 3 9 12
41 }
42  
43 85 return $self;
44 }
45  
46  
47 9 ▶sub default_file_spec { 'Native' }
Subroutine default_file_spec: called CC=1 SCAR=0.0
Pod documented
48  
49  
50 71 ▶sub determine_name_and_version {
Subroutine determine_name_and_version: called CC=1 SCAR=0.0
Pod documented
51 71 my ($self) = @_;
52 71 $self->set_name_and_version( $self->parse_name_and_version( $self->root ) );
53 71 return;
54 }
55  
56  
57 42 ▶sub determine_packages {
Subroutine determine_packages: called CC=1 SCAR=0.2
Pod documented
58 42 my ($self, @files) = @_;
59  
60 42 ▶ my $determined = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 42 my @dir_and_files = $self->physical_directory(@files);
62  
63 # return
64 42 $self->packages_from_directory(@dir_and_files);
65 }
66 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 carp("Error determining packages: $_[0]");
68 0 +{}; # return
69 42 };
70  
71 42 return $determined;
72 }
73  
74  
75 37 ▶sub extract_into {
Subroutine extract_into: called CC=5 SCAR=16.1
Pod documented
76 37 my ($self, $dir, @files) = @_;
77  
78 37 ▶ @files = $self->list_files
Branch: 1 branch
BranchTrueFalseTotal
unless @files 2 35 37
79 unless @files;
80  
81 37 require File::Basename;
82  
83 37 my @disk_files;
84 37 foreach my $file (@files) {
85 57 my $ff = $self->path_class_file->new_foreign( $self->file_spec, $file );
86 # Translate dist format (relative path) to disk/OS format and prepend $dir.
87 # This dir_list + basename hack is probably ok because the paths in a dist
88 # should always be relative (if there *was* a volume we wouldn't want it).
89 57 my $path = $self->path_class_file
90 ->new( $dir, $ff->dir->dir_list, $ff->basename );
91  
92 57 $path->dir->mkpath(0, oct(700));
93  
94 57 my $full_path = $path->stringify;
95 57 ▶ open(my $fh, '>', $full_path)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $full_path or croak("Failed to open '${full_path}' for writing: $!") 57 0 57
96 or croak "Failed to open '$full_path' for writing: $!";
97 57 print $fh $self->file_content($file);
98  
99 # do we really want full path or do we want relative?
100 57 push(@disk_files, $full_path);
101 }
102  
103 37 ▶ return (wantarray ? ($dir, @disk_files) : $dir);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 36 1 37
104 }
105  
106  
107 0 ▶sub file_content {
Subroutine file_content: not called CC=1 SCAR=6.9
Pod documented
108 0 croak q[Method 'file_content' not defined];
109 }
110  
111  
112 22 ▶sub file_checksum {
Subroutine file_checksum: called CC=2 SCAR=7.0
Pod documented
113 22 my ($self, $file, $type) = @_;
114 22 ▶ $type ||= 'md5';
Condition50%1/2
$type ||= "md5"
l!l
220
MC/DC0%0/1
$type ||= "md5"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
115  
116 22 require Digest; # core
117  
118 # md5 => MD5, sha256 => SHA-256
119 22 (my $impl = uc $type) =~ s/^(SHA|CRC)([0-9]+)$/$1-$2/;
120  
121 22 my $digest = Digest->new($impl);
122  
123 22 $digest->add( $self->file_content($file) );
124 22 return $digest->hexdigest;
125 }
126  
127  
128 0 ▶sub find_files {
Subroutine find_files: not called CC=1 SCAR=6.9
Pod documented
129 0 croak q[Method 'find_files' not defined];
130 }
131  
132  
133 219 ▶sub file_spec {
Subroutine file_spec: called CC=2 SCAR=6.9
Pod documented
134 219 my ($self) = @_;
135  
136 $self->{file_spec} = $self->default_file_spec
137 219 ▶ if !exists $self->{file_spec};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"file_spec"} 44 175 219
138  
139 219 return $self->{file_spec};
140 }
141  
142  
143 102 ▶sub full_path {
Subroutine full_path: called CC=3 SCAR=11.0
Pod documented
144 102 my ($self, $file) = @_;
145  
146 102 ▶ return $file
Branch: 1 branch
BranchTrueFalseTotal
unless my $root = $self->root 38 64 102
147 unless my $root = $self->root;
148  
149 # don't re-add the root if it's already there
150 64 ▶ return $file
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[^\Q$root\E[\\/]] 0 64 64
151 # FIXME: this regexp is probably not cross-platform...
152 # FIXME: is there a way to do this with File::Spec?
153 if $file =~ m@^\Q${root}\E[\\/]@;
154  
155 # FIXME: does this foreign_file work w/ Dir ?
156 64 return $self->path_class_file
157 ->new_foreign($self->file_spec, $root, $file)->stringify;
158 }
159  
160  
161 105 ▶sub list_files {
Subroutine list_files: called CC=2 SCAR=6.9
Pod documented
162 105 my ($self) = @_;
163  
164 $self->{_list_files} = do {
165 59 my @files = sort $self->find_files;
166 59 my ($root, @rel) = $self->remove_root_dir(@files);
167 59 $self->{root} = $root;
168 59 \@rel; # return
169 }
170 105 ▶ unless $self->{_list_files};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_list_files"} 59 46 105
171  
172 105 return @{ $self->{_list_files} };
173 }
174  
175  
176 {
177 17 ▶ no strict 'refs'; ## no critic (NoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
178 17 foreach my $method ( qw(
179 name
180 version
181 ) ){
182 128 ▶ *$method = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
183 128 my ($self) = @_;
184  
185 $self->determine_name_and_version
186 128 ▶ if !exists $self->{ $method };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$method} 68 60 128
187  
188 128 return $self->{ $method };
189 34 };
190 }
191 }
192  
193  
194 42 ▶sub packages_from_directory {
Subroutine packages_from_directory: called CC=4 SCAR=14.4
Pod documented
195 42 my ($self, $dir, @files) = @_;
196  
197 42 my @pvfd = ($dir);
198 # M::M::p_v_f_d expects full paths for \@files
199 push @pvfd, [map {
200 42 ▶ $self->path_class_file->new($_)->is_absolute
Branch: 2 branches
BranchTrueFalseTotal
if @files 42 0 42
$self->path_class_file->new($_)->is_absolute ? : 65 0 65
201 ? $_ : $self->path_class_file->new($dir, $_)->stringify
202 } @files]
203 if @files;
204  
205 42 require Module::Metadata;
206  
207 42 ▶ my $provides = try {
Subroutine __ANON__: called CC=2 SCAR=6.9
208 42 my $packages = Module::Metadata->package_versions_from_directory(@pvfd);
209 42 ▶ while ( my ($pack, $pv) = each %$packages ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$packages) 77 42 119
210 # M::M::p_v_f_d returns files in native OS format (obviously);
211 # CPAN::Meta expects file paths in Unix format
212 $pv->{file} = $self->path_class_file
213 77 ->new($pv->{file})->as_foreign('Unix')->stringify;
214 }
215 42 $packages; # return
216 }
217 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 carp("Failed to determine packages: $_[0]");
219 0 +{}; # return
220 42 };
221 42 ▶ return $provides || {};
Condition50%1/2
$provides || {}
l!l
420
MC/DC0%0/1
$provides || {}
$provides
Truth table50%1/2
A
A= $provides
Aresult
11
00
222 }
223  
224  
225 115 ▶sub parse_name_and_version {
Subroutine parse_name_and_version: called CC=10 SCAR=23.3
Pod documented
226 115 my ($self, $path) = @_;
227 115 my ( $name, $version );
228 115 ▶ if ( $path ){
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 76 39 115
229 # try a simple regexp first
230 76 ▶ $path =~ m!
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[ ([^\\/]+) # name (anything below final directory) - # separator (v?[0-9._]+) # version (?: # possible file extensions \.t(?:ar\.)?gz )? $ ]x and () = ($1, $2) 54 22 76
231 ([^\\/]+) # name (anything below final directory)
232 - # separator
233 (v?[0-9._]+) # version
234 (?: # possible file extensions
235 \.t(?:ar\.)?gz
236 )?
237 $
238 !x and
239 ( $name, $version ) = ( $1, $2 );
240  
241 # attempt to improve data with CPAN::DistnameInfo (but ignore any errors)
242 # TODO: also grab maturity and cpanid ?
243 # release_status = $dist->maturity eq 'released' ? 'stable' : 'unstable';
244 # -(TRIAL|RC) => 'testing', '_' => 'unstable'
245 76 eval {
246 # DistnameInfo expects any directories in unix format (thanks jeroenl)
247 76 my $dnifile = $self->path_class_file
248 ->new($path)->as_foreign('Unix')->stringify;
249 # if it doesn't appear to have an extension fake one to help DistnameInfo
250 76 ▶ $dnifile .= '.tar.gz' unless $dnifile =~ /\.[a-z]\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $dnifile =~ /\.[a-z]\w+$/ 46 30 76
251  
252 76 my $dni = CPAN::DistnameInfo->new($dnifile);
253 76 my $dni_name = $dni->dist;
254 76 my $dni_version = $dni->version;
255 # if dni matched both name and version, or previous regexp didn't match
256 76 ▶ if ( $dni_name && $dni_version || !$name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dni_name and $dni_version or not $name) 76 0 76
Condition66%4/6
$dni_name and $dni_version or not $name
l!l&&r!l&&!r
63130
$dni_name and $dni_version
!ll&&!rl&&r
01363
MC/DC0%0/3
$dni_name and $dni_version or not $name
$dni_name $dni_version not $name
Truth table40%2/5
A and B or not(C)
A= $dni_nameB= $dni_versionC= not $name
ABCresult
11X1
0X11
1011
0X00
1000
257 76 ▶ $name = $dni_name if $dni_name;
Branch: 1 branch
BranchTrueFalseTotal
if $dni_name 76 0 76
258 76 ▶ $version = $dni_version if $dni_version;
Branch: 1 branch
BranchTrueFalseTotal
if $dni_version 63 13 76
259 }
260 };
261 76 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 76 76
262 }
263 115 return ($name, $version);
264 }
265  
266  
267 23 ▶sub path_class_dir { $_[0]->{path_class_dir} ||= 'Path::Class::Dir' }
Subroutine path_class_dir: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"path_class_dir"} ||= "Path::Class::Dir"
l!l
1112
MC/DC100%1/1
$_[0]{"path_class_dir"} ||= "Path::Class::Dir"
$_[0]{"path_class_dir"}
Truth table100%2/2
A
A= $_[0]{"path_class_dir"}
Aresult
11
00
268 533 ▶sub path_class_file { $_[0]->{path_class_file} ||= 'Path::Class::File' }
Subroutine path_class_file: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"path_class_file"} ||= "Path::Class::File"
l!l
46865
MC/DC100%1/1
$_[0]{"path_class_file"} ||= "Path::Class::File"
$_[0]{"path_class_file"}
Truth table100%2/2
A
A= $_[0]{"path_class_file"}
Aresult
11
00
269  
270  
271 0 ▶sub path_classify_dir {
Subroutine path_classify_dir: not called CC=1 SCAR=6.9
Pod documented
272 0 my ($self, $dir) = @_;
273 0 $self->path_class_dir->new_foreign($self->file_spec, $dir)
274 }
275  
276 88 ▶sub path_classify_file {
Subroutine path_classify_file: called CC=1 SCAR=0.0
Pod documented
277 88 my ($self, $file) = @_;
278 88 $self->path_class_file->new_foreign($self->file_spec, $file)
279 }
280  
281  
282 43 ▶sub perl_files {
Subroutine perl_files: called CC=1 SCAR=0.0
Pod documented
283 return
284 43 grep { /\.pm$/ }
285 $_[0]->list_files;
286 }
287  
288  
289 37 ▶sub physical_directory {
Subroutine physical_directory: called CC=1 SCAR=0.0
Pod documented
290 37 my ($self, @files) = @_;
291  
292 37 require File::Temp;
293 # dir will be removed when return value goes out of scope (in caller)
294 37 my $dir = File::Temp->newdir();
295  
296 37 return $self->extract_into($dir, @files);
297 }
298  
299  
300 59 ▶sub remove_root_dir {
Subroutine remove_root_dir: called CC=5 SCAR=16.2
Pod documented
301 59 my ($self, @files) = @_;
302 59 ▶ return unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 59 59
303  
304 # FIXME: can we use File::Spec for these regexp's instead of [\\/] ?
305  
306 # grab the root dir from the first file
307 59 ▶ $files[0] =~ m{^([^\\/]+)[\\/]}
Branch: 1 branch
BranchTrueFalseTotal
$files[0] =~ m[^([^\\/]+)[\\/]] or return undef, @files 32 27 59
308 # if not matched quit now
309 or return (undef, @files);
310  
311 32 my $dir = $1;
312 32 my @rel;
313  
314 # strip $dir from each file
315 32 for (@files) {
316  
317 98 ▶ m{^\Q$dir\E[\\/](.+)$}
Branch: 1 branch
BranchTrueFalseTotal
m[^\Q$dir\E[\\/](.+)$] or return undef, @files 98 0 98
318 # if the match failed they're not all under the same root so just return now
319 or return (undef, @files);
320  
321 98 push @rel, $1;
322 }
323  
324 32 return ($dir, @rel);
325  
326 }
327  
328  
329 0 ▶sub required_attribute { return }
Subroutine required_attribute: not called CC=1 SCAR=6.9
Pod documented
330  
331  
332 193 ▶sub root {
Subroutine root: called CC=2 SCAR=6.9
Pod documented
333 193 my ($self) = @_;
334  
335 # call list_files instead of find_files so that it caches the result
336 $self->list_files
337 193 ▶ unless exists $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"root"} 12 181 193
338  
339 193 return $self->{root};
340 }
341  
342  
343 115 ▶sub set_name_and_version {
Subroutine set_name_and_version: called CC=4 SCAR=13.9
Pod documented
344 115 my ($self, @values) = @_;
345 115 my @fields = qw( name version );
346  
347 115 foreach my $i ( 0 .. $#fields ){
348 $self->{ $fields[$i] } = $values[$i]
349 230 ▶ if !exists $self->{ $fields[$i] } && defined $values[$i];
Branch: 1 branch
BranchTrueFalseTotal
if not exists $$self{$fields[$i]} and defined $values[$i] 87 143 230
Condition100%3/3
not exists $$self{$fields[$i]} and defined $values[$i]
!ll&&!rl&&r
588587
MC/DC100%2/2
not exists $$self{$fields[$i]} and defined $values[$i]
not exists $$self{$fields[$i]} defined $values[$i]
Truth table100%3/3
not(A) and B
A= not exists $$self{$fields[$i]}B= defined $values[$i]
ABresult
0X0
100
111
350 }
351 115 return;
352 }
353  
354  
355 # version() defined with name()
356  
357 17 1;
358  
359 __END__
« blib/lib/Dist/Metadata.pm ↑ Summary blib/lib/Dist/Metadata/Archive.pm »