| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | CPAN::Kwalify - Interface between CPAN.pm and Kwalify.pm | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | use CPAN::Kwalify; | ||||||||||||||||||||||||||||
| 8 | validate($schema_name, $data, $file, $doc); | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =over | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =item _validate($schema_name, $data, $file, $doc) | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $schema_name is the name of a supported schema. Currently only | ||||||||||||||||||||||||||||
| 17 | C<distroprefs> is supported. $data is the data to be validated. $file | ||||||||||||||||||||||||||||
| 18 | is the absolute path to the file the data are coming from. $doc is the | ||||||||||||||||||||||||||||
| 19 | index of the document within $doc that is to be validated. The last | ||||||||||||||||||||||||||||
| 20 | two arguments are only there for better error reporting. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Relies on being called from within CPAN.pm. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Dies if something fails. Does not return anything useful. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =item yaml($schema_name) | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Returns the YAML text of that schema. Dies if something fails. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =back | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Andreas Koenig C<< <andk@cpan.org> >> | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 39 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | package CPAN::Kwalify; | ||||||||||||||||||||||||||||
| 51 | 3 | ▶ | use vars qw($VERSION $VAR1); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 3 | $VERSION = "5.50"; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 3 | my %vcache = (); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | my $schema_loaded = {}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub _validate { | ||||||||||||||||||||||||||
|
Subroutine
_validate: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 61 | 0 | my($schema_name,$data,$abs,$y) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | my $yaml_module = CPAN->_yaml_module; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$CPAN::META->has_inst($yaml_module) and $CPAN::META->has_inst("Kwalify")
MC/DC0%0/2
$CPAN::META->has_inst($yaml_module) and $CPAN::META->has_inst("Kwalify")
$CPAN::META->has_inst($yaml_module)
$CPAN::META->has_inst("Kwalify")
Truth table0%0/3
A and B
A= $CPAN::META->has_inst($yaml_module)B= $CPAN::META->has_inst("Kwalify")
| |||||||||||||||||||||||||||||
| 64 | $CPAN::META->has_inst($yaml_module) | ||||||||||||||||||||||||||||
| 65 | && | ||||||||||||||||||||||||||||
| 66 | $CPAN::META->has_inst("Kwalify") | ||||||||||||||||||||||||||||
| 67 | ) { | ||||||||||||||||||||||||||||
| 68 | 0 | my $load = UNIVERSAL::can($yaml_module,"Load"); | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | unless ($schema_loaded->{$schema_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | eval { | |||||||||||||||||||||||||||
| 71 | 0 | my $schema_yaml = yaml($schema_name); | |||||||||||||||||||||||||||
| 72 | 0 | $schema_loaded->{$schema_name} = $load->($schema_yaml); | |||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | # we know that YAML.pm 0.62 cannot parse the schema, | ||||||||||||||||||||||||||||
| 76 | # so we try a fallback | ||||||||||||||||||||||||||||
| 77 | 0 | my $content = do { | |||||||||||||||||||||||||||
| 78 | 0 | my $path = __FILE__; | |||||||||||||||||||||||||||
| 79 | 0 | $path =~ s/\.pm$//; | |||||||||||||||||||||||||||
| 80 | 0 | $path = File::Spec->catfile($path, "$schema_name.dd"); | |||||||||||||||||||||||||||
| 81 | 0 | local *FH; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | open FH, $path or die "Could not open '$path': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | local $/; | |||||||||||||||||||||||||||
| 84 | 0 | <FH>; | |||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | 0 | $VAR1 = undef; | |||||||||||||||||||||||||||
| 87 | 0 | eval $content; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | die "parsing of '$schema_name.dd' failed: $err"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | $schema_loaded->{$schema_name} = $VAR1; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (my $schema = $schema_loaded->{$schema_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | my $mtime = (stat $abs)[9]; | |||||||||||||||||||||||||||
| 97 | 0 | for my $k (keys %{$vcache{$abs}}) { | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | delete $vcache{$abs}{$k} unless $k eq $mtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return if $vcache{$abs}{$mtime}{$y}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | eval { Kwalify::validate($schema, $data) }; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | my $info = {}; yaml($schema_name, info => $info); | |||||||||||||||||||||||||||
| 104 | 0 | die "validation of distropref '$abs'[$y] against schema '$info->{path}' failed: $err"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub _clear_cache { | ||||||||||||||||||||||||||
|
Subroutine
_clear_cache: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 110 | 0 | %vcache = (); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub yaml { | ||||||||||||||||||||||||||
|
Subroutine
yaml: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 0 | my($schema_name, %opt) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | my $content = do { | |||||||||||||||||||||||||||
| 116 | 0 | my $path = __FILE__; | |||||||||||||||||||||||||||
| 117 | 0 | $path =~ s/\.pm$//; | |||||||||||||||||||||||||||
| 118 | 0 | $path = File::Spec->catfile($path, "$schema_name.yml"); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($opt{info}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $opt{info}{path} = $path; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 0 | local *FH; | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | open FH, $path or die "Could not open '$path': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | local $/; | |||||||||||||||||||||||||||
| 125 | 0 | <FH>; | |||||||||||||||||||||||||||
| 126 | }; | ||||||||||||||||||||||||||||
| 127 | 0 | return $content; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Local Variables: | ||||||||||||||||||||||||||||
| 133 | # mode: cperl | ||||||||||||||||||||||||||||
| 134 | # cperl-indent-level: 4 | ||||||||||||||||||||||||||||
| 135 | # End: | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||