DEP12.pm

filter: Statementstmt 75.6% 124 / 164 Branchbran 62.9% 39 / 62 Conditioncond 60.0% 9 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 95.0% 19 / 20 Podpod 100% 7 / 7 totaltotal 72.5% 201 / 277 CC 48 SCAR 40.0
CC
48
Cov
71%
validate
108.0
new
19.3
_to_BibTeX
3.0
CRAP 54.5
↑ Summary DEP12/ValidationWarning.pm »
1 package Debian::DEP12;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: interface to Debian DEP 12 format
7 5 our $VERSION = '0.2.0'; # VERSION
8  
9 # As DEP 12 is in draft state and the development is happening in its
10 # wiki page (https://wiki.debian.org/UpstreamMetadata), this variable
11 # holds the UTC timestamp of DEP 12 wiki page.
12 5 our $DEP12_VERSION = '2021-02-21 11:52:54';
13  
14 5 ▶use Data::Validate::Email qw( is_email_rfc822 );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Data::Validate::URI qw( is_uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Debian::DEP12::ValidationWarning;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Text::BibTeX::Validate qw( validate_BibTeX );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Preventing YAML::XS from doing undesired things:
24 5 $YAML::XS::DumpCode = 0;
25 5 $YAML::XS::LoadBlessed = 0;
26 5 $YAML::XS::UseCode = 0;
27  
28 5 my @fields = qw(
29 Archive
30 ASCL-Id
31 Bug-Database
32 Bug-Submit
33 Cite-As
34 Changelog
35 CPE
36 Documentation
37 Donation
38 FAQ
39 Funding
40 Gallery
41 Other-References
42 Reference
43 Registration
44 Registry
45 Repository
46 Repository-Browse
47 Screenshots
48 Security-Contact
49 Webservice
50 );
51  
52 5 my @list_fields = qw(
53 Funding
54 Reference
55 Registry
56 Screenshots
57 );
58  
59 5 my @reserved_fields = qw(
60 ping
61 YAML-ALL
62 YAML-URL
63 YAML-REFRESH-DATE
64 );
65  
66 =head1 NAME
67  
68 Debian::DEP12 - interface to Debian DEP 12 format
69  
70 =head1 SYNOPSIS
71  
72 use Debian::DEP12;
73  
74 my $meta = Debian::DEP12->new;
75 $meta->set( 'Bug-Database',
76 'https://github.com/merkys/Debian-DEP12/issues' );
77  
78 $meta->validate;
79  
80 =head1 DESCRIPTION
81  
82 Debian::DEP12 is an object-oriented interface for Debian DEP 12 format,
83 also known as debian/upstream/metadata. Primary focus of the initial
84 development is the validation and fixing of DEP 12 data.
85  
86 DEP 12 is in draft state and the development is happening in its wiki
87 page (L<https://wiki.debian.org/UpstreamMetadata>). Thus Debian::DEP12
88 attempts to keep up with the DEP 12 specification. To keep track, the
89 UTC timestamp of DEP 12 wiki page is stored in C<$DEP12_VERSION> class
90 variable.
91  
92 Contributions welcome!
93  
94 =head1 METHODS
95  
96 =head2 new( $what )
97  
98 Creates a new Debian::DEP12 instance from either YAML,
99 L<Text::BibTeX::Entry|Text::BibTeX::Entry> or
100 L<Text::BibTeX::File|Text::BibTeX::File> instances, or plain Perl hash
101 reference with DEP 12 fields and their values.
102  
103 =cut
104  
105 sub new
106 10 ▶{
Subroutine new: called CC=14 SCAR=29.6
Pod documented
107 10 my( $class, $what ) = @_;
108  
109 10 my $self;
110 10 ▶ if( !defined $what ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $what) { } 1 9 10
elsif (builtin::blessed($what) and $what->isa('Text::BibTeX::Entry') || $what->isa('Text::BibTeX::File')) { } 2 7 9
elsif (ref $what eq '') { } 5 2 7
elsif (ref $what eq 'HASH') { } 2 0 2
111 1 $self = {};
112 ▶ } elsif( blessed $what &&
Condition66%4/6
builtin::blessed($what) and $what->isa("Text::BibTeX::Entry") || $what->isa("Text::BibTeX::File")
!ll&&!rl&&r
702
$what->isa("Text::BibTeX::Entry") or $what->isa("Text::BibTeX::File")
l!l&&r!l&&!r
110
MC/DC33%1/3
builtin::blessed($what) and $what->isa("Text::BibTeX::Entry") || $what->isa("Text::BibTeX::File")
builtin::blessed($what) $what->isa("Text::BibTeX::Entry") $what->isa("Text::BibTeX::File")
Truth table75%3/4
A and B or C
A= builtin::blessed($what)B= $what->isa("Text::BibTeX::Entry")C= $what->isa("Text::BibTeX::File")
ABCresult
0XX0
1000
11X1
1011
113 ( $what->isa( 'Text::BibTeX::Entry' ) ||
114 $what->isa( 'Text::BibTeX::File' ) ) ) {
115  
116 2 my @entries;
117 2 ▶ if( $what->isa( 'Text::BibTeX::Entry' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what->isa('Text::BibTeX::Entry')) { } 1 1 2
118 1 push @entries, $what;
119 } else {
120 1 require Text::BibTeX::Entry;
121 1 ▶ while( my $entry = Text::BibTeX::Entry->new( $what ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = "Text::BibTeX::Entry"->new($what)) 1 1 2
122 1 push @entries, $entry;
123 }
124 }
125  
126 2 my @references;
127 2 for my $entry (@entries) {
128 # FIXME: Filter only supported keys (?)
129 push @references,
130 2 { map { _canonical_BibTeX_key( $_ ) =>
131 decode( 'UTF-8', $entry->get( $_ ) ) }
132 2 grep { defined $entry->get( $_ ) }
133 $entry->fieldlist };
134  
135 0 for ('number', 'pages', 'volume', 'year') {
136 0 ▶ next if !exists $references[-1]->{ucfirst $_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $references[-1]{ucfirst $_} 0 0 0
137 0 ▶ next if $references[-1]->{ucfirst $_} !~ /^[1-9][0-9]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $references[-1]{ucfirst $_} =~ /^[1-9][0-9]*$/ 0 0 0
138 $references[-1]->{ucfirst $_} =
139 0 int $references[-1]->{ucfirst $_};
140 }
141 }
142  
143 0 return $class->new( { Reference => \@references } );
144 } elsif( ref $what eq '' ) {
145 # Text in YAML format
146 5 ▶ if( version->parse($YAML::XS::VERSION) < version->parse('0.69') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($YAML::XS::VERSION) < "version"->parse("0.69")) 0 5 5
147 0 die 'YAML::XS < 0.69 is insecure' . "\n";
148 }
149  
150 5 $self = Load $what;
151 } elsif( ref $what eq 'HASH' ) {
152 2 $self = $what;
153 } else {
154 0 die 'cannot create Debian::DEP12 from ' . ref( $what ) . "\n";
155 }
156  
157 8 return bless $self, $class;
158 }
159  
160 sub _canonical_BibTeX_key
161 4 ▶{
Subroutine _canonical_BibTeX_key: called CC=2 SCAR=6.9
162 4 my( $key ) = @_;
163 4 ▶ return uc $key if $key =~ /^(doi|isbn|issn|pmid|url)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(doi|isbn|issn|pmid|url)$/ 2 2 4
164 2 return ucfirst $key;
165 }
166  
167 =head2 fields()
168  
169 Returns an array of fields defined in the instance in any order.
170  
171 =cut
172  
173 sub fields
174 7 ▶{
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
175 7 return keys %{$_[0]};
176 }
177  
178 =head2 get( $field )
179  
180 Returns value of a field.
181  
182 =cut
183  
184 sub get
185 52 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
186 52 my( $self, $field ) = @_;
187 52 return $self->{$field};
188 }
189  
190 =head2 set( $field, $value )
191  
192 Sets a new value for a field. Returns the old value.
193  
194 =cut
195  
196 sub set
197 1 ▶{
Subroutine set: called CC=1 SCAR=0.0
Pod documented
198 1 my( $self, $field, $value ) = @_;
199 1 ( my $old_value, $self->{$field} ) = ( $self->{$field}, $value );
200 1 return $old_value;
201 }
202  
203 =head2 delete( $field )
204  
205 Unsets value for a field. Returns the old value.
206  
207 =cut
208  
209 sub delete
210 0 ▶{
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
211 0 my( $self, $field ) = @_;
212  
213 0 my $old_value = $self->{$field};
214 0 delete $self->{$field};
215  
216 0 return $old_value;
217 }
218  
219 sub _to_BibTeX
220 7 ▶{
Subroutine _to_BibTeX: called CC=3 SCAR=11.0
221 7 my( $self ) = @_;
222  
223 7 my $reference = $self->get( 'Reference' );
224 7 ▶ if( ref $reference eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $reference eq "HASH") 1 6 7
225 1 $reference = [ $reference ];
226 }
227  
228 7 my @BibTeX;
229 7 for my $reference (@$reference) {
230 push @BibTeX,
231 3 { map { lc( $_ ) => $reference->{$_} } keys %$reference };
232 }
233 7 return @BibTeX;
234 }
235  
236 =head2 to_YAML()
237  
238 Returns a string with YAML representation.
239  
240 =cut
241  
242 sub to_YAML
243 1 ▶{
Subroutine to_YAML: called CC=1 SCAR=0.0
Pod documented
244 1 my( $self ) = @_;
245 1 my $yaml = Dump $self;
246  
247 # HACK: no better way to serialize plain data?
248 1 $yaml =~ s/^---[^\n]*\n//m;
249 1 return $yaml;
250 }
251  
252 =head2 validate()
253  
254 Performs checks of DEP 12 data in the instance and returns an array of
255 validation messages as instances of
256 L<Debian::DEP12::ValidationWarning|Debian::DEP12::ValidationWarning>.
257  
258 =cut
259  
260 sub validate
261 7 ▶{
Subroutine validate: called CC=31 SCAR=46.8
Pod documented
262 7 my( $self ) = @_;
263  
264 7 my @warnings;
265  
266 # TODO: validate other fields
267  
268 7 for my $key (sort $self->fields) {
269 9 ▶ if( !grep { $_ eq $key } @fields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $key;} @fields) 0 9 9
270 0 push @warnings,
271 _warn_value( 'unknown field', $key, $self->get( $key ) );
272 }
273  
274 9 ▶ if( grep { $_ eq $key } @reserved_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $key;} @reserved_fields) 0 9 9
275 0 push @warnings,
276 _warn_value( 'reserved field', $key, $self->get( $key ) );
277 }
278  
279 9 ▶ if( ref $self->get( $key ) && !grep { $_ eq $key } @list_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->get($key) and not grep({$_ eq $key;} @list_fields)) 1 8 9
Condition100%3/3
ref $self->get($key) and not grep({$_ eq $key;} @list_fields)
!ll&&!rl&&r
621
MC/DC100%2/2
ref $self->get($key) and not grep({$_ eq $key;} @list_fields)
ref $self->get($key) not grep({$_ eq $key;} @list_fields)
Truth table100%3/3
A and not(B)
A= ref $self->get($key)B= not grep({$_ eq $key;} @list_fields)
ABresult
0X0
100
111
280 1 push @warnings,
281 _warn_value( 'scalar value expected',
282 $key,
283 $self->get( $key ) );
284 }
285 }
286  
287 7 for my $key ('Bug-Database', 'Bug-Submit', 'Changelog',
288 'Documentation', 'Donation', 'FAQ', 'Gallery',
289 'Other-References', 'Registration', 'Repository',
290 'Repository-Browse', 'Screenshots', 'Webservice') {
291 91 ▶ next if !exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 84 7 91
292  
293 7 ▶ if( !defined $self->get( $key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get($key)) 1 6 7
294 1 push @warnings, _warn_value( 'undefined value', $key );
295 1 next;
296 }
297  
298 6 my @values;
299 6 ▶ if( ref $self->get( $key ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->get($key) eq 'ARRAY') { } 1 5 6
300 1 @values = @{$self->get( $key )};
301 } else {
302 5 @values = ( $self->get( $key ) );
303 }
304  
305 6 for my $i (0..$#values) {
306 7 ▶ my $yamlpath = $key .
Branch: 1 branch
BranchTrueFalseTotal
ref $self->get($key) eq 'ARRAY' ? : 2 5 7
307 (ref $self->get( $key ) eq 'ARRAY' ? "[$i]" : '');
308 7 $_ = $values[$i];
309  
310 7 ▶ if( ref $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 7 7
311 0 push @warnings,
312 _warn_value( 'non-scalar value',
313 $yamlpath,
314 $_ );
315 0 next;
316 }
317  
318 7 ▶ next if defined is_uri $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined is_uri($_) 2 5 7
319  
320 5 ▶ if( /^(.*)\n$/ && defined is_uri $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.*)\n$/ and defined is_uri($1)) 0 5 5
Condition33%1/3
/^(.*)\n$/ and defined is_uri($1)
!ll&&!rl&&r
500
MC/DC0%0/2
/^(.*)\n$/ and defined is_uri($1)
/^(.*)\n$/ defined is_uri($1)
Truth table33%1/3
A and B
A= /^(.*)\n$/B= defined is_uri($1)
ABresult
0X0
100
111
321 0 push @warnings,
322 _warn_value( 'URL has trailing newline character',
323 $yamlpath,
324 $_,
325 { suggestion => $1 } );
326 0 next;
327 }
328  
329 5 ▶ if( is_email_rfc822( $_ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_email_rfc822($_)) 2 3 5
330 2 push @warnings,
331 _warn_value( 'value \'%(value)s\' is better written as \'%(suggestion)s\'',
332 $yamlpath,
333 $_,
334 { suggestion => 'mailto:' . $_ } );
335 2 next;
336 }
337  
338 3 push @warnings,
339 _warn_value( 'value \'%(value)s\' does not look like valid URL',
340 $yamlpath,
341 $_ );
342 }
343 }
344  
345 7 ▶ if( defined $self->get( 'Registry' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get("Registry") and ref $self->get("Registry") eq "ARRAY") 0 7 7
Condition33%1/3
defined $self->get("Registry") and ref $self->get("Registry") eq "ARRAY"
!ll&&!rl&&r
700
MC/DC0%0/2
defined $self->get("Registry") and ref $self->get("Registry") eq "ARRAY"
defined $self->get("Registry") ref $self->get("Registry") eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->get("Registry")B= ref $self->get("Registry") eq "ARRAY"
ABresult
0X0
100
111
346 ref $self->get( 'Registry' ) eq 'ARRAY' ) {
347 # TODO: report non-arrays
348  
349 0 my @registry = @{$self->get( 'Registry' )};
350 0 for my $i (0..$#registry) {
351 0 my @unknown_fields = grep { $_ !~ /^(Name|Entry)$/ }
352 0 sort keys %{$registry[$i]};
353 0 for (@unknown_fields) {
354 push @warnings,
355 _warn_value( 'unknown field',
356 "Registry[$i].$_",
357 0 $registry[$i]->{$_} );
358 }
359  
360 0 for ('Name', 'Entry') {
361 0 ▶ if( !exists $registry[$i]->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $registry[$i]{$_}) 0 0 0
362 0 push @warnings,
363 _warn_value( 'missing mandatory field',
364 "Registry[$i].$_" );
365 0 next;
366 }
367  
368 0 ▶ if( !defined $registry[$i]->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $registry[$i]{$_}) 0 0 0
369 0 push @warnings,
370 _warn_value( 'undefined value',
371 "Registry[$i].$_" );
372 0 next;
373 }
374  
375 0 ▶ if( ref $registry[$i]->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $registry[$i]{$_}) 0 0 0
376 push @warnings,
377 _warn_value( 'non-scalar value',
378 "Registry[$i].$_",
379 0 $registry[$i]->{$_} );
380 0 next;
381 }
382  
383 0 ▶ if( $_ eq 'Name' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'Name') { } 0 0 0
384 0 ▶ if( $registry[$i]->{$_} !~
Branch: 1 branch
BranchTrueFalseTotal
unless ($registry[$i]{$_} =~ /^(bio\.tools|biii|OMICtools|SciCrunch|conda:.+|opam|PyPI)$/) 0 0 0
385 /^(bio\.tools|biii|OMICtools|SciCrunch|conda:.+|opam|PyPI)$/ ) {
386 push @warnings,
387 _warn_value( 'unknown registry \'%(value)s\'',
388 "Registry[$i].$_",
389 0 $registry[$i]->{$_} );
390 }
391 } else { # Entry
392 0 ▶ if( defined is_uri $registry[$i]->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined is_uri($registry[$i]{$_})) 0 0 0
393 push @warnings,
394 _warn_value( 'should not be URI',
395 "Registry[$i].$_",
396 0 $registry[$i]->{$_} );
397 }
398 }
399 }
400 }
401 }
402  
403 7 my @BibTeX = $self->_to_BibTeX;
404 7 for my $i (0..$#BibTeX) {
405 3 my $BibTeX = $BibTeX[$i];
406 3 my @BibTeX_warnings = validate_BibTeX( $BibTeX );
407 3 for (@BibTeX_warnings) {
408 # For everything under Reference outputting YAML paths like
409 # https://github.com/wwkimball/yamlpath/wiki/Segments-of-a-YAML-Path
410 2 ▶ $_->set( 'field',
Branch: 1 branch
BranchTrueFalseTotal
ref $self->get('Reference') eq 'ARRAY' ? : 1 1 2
411 "Reference" .
412 (ref $self->get( 'Reference' ) eq 'ARRAY' ? "[$i]" : '') . '.' .
413 _canonical_BibTeX_key( $_->get( 'field' ) ) );
414 2 bless $_, Debian::DEP12::ValidationWarning::;
415 }
416 3 push @warnings, @BibTeX_warnings;
417 }
418  
419 7 return @warnings;
420 }
421  
422 sub _warn_value
423 7 ▶{
Subroutine _warn_value: called CC=2 SCAR=6.9
424 7 my( $message, $field, $value, $extra ) = @_;
425 7 ▶ $extra = {} unless $extra;
Branch: 1 branch
BranchTrueFalseTotal
unless $extra 5 2 7
426 7 return Debian::DEP12::ValidationWarning->new(
427 $message,
428 { field => $field,
429 value => $value,
430 %$extra } );
431 }
432  
433 =head1 SEE ALSO
434  
435 For the description of DEP 12 refer to
436 L<https://dep-team.pages.debian.net/deps/dep12/>.
437  
438 =head1 AUTHORS
439  
440 Andrius Merkys, E<lt>merkys@cpan.orgE<gt>
441  
442 =cut
443  
444 5 1;
↑ Summary DEP12/ValidationWarning.pm »